Skip to content

Commit

Permalink
Move tests uart port into tests/tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Guggi committed Jul 10, 2024
1 parent cf28ded commit c6057a3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
24 changes: 20 additions & 4 deletions tests/simulation/command_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::simulation::*;

#[test]
fn simulate_archive_is_stored_correctly() -> Result<(), std::io::Error> {
let (mut com, _socat) = SimulationComHandle::with_socat_proc("archive_is_stored_correctly");
let _sched = start_scheduler("archive_is_stored_correctly")?;
let (_sched, mut com, _socat) = start_scheduler("archive_is_stored_correctly").unwrap();

simulate_test_store_archive(&mut com, 1).unwrap();
std::thread::sleep(std::time::Duration::from_millis(400));
Expand All @@ -27,8 +26,7 @@ fn simulate_archive_is_stored_correctly() -> Result<(), std::io::Error> {

#[test]
fn return_result_is_retried_n_times() {
let (mut com, _socat) = SimulationComHandle::with_socat_proc("return_result_retries");
let _sched = start_scheduler("return_result_retries").unwrap();
let (_sched, mut com, _socat) = start_scheduler("return_result_retries").unwrap();

simulate_test_store_archive(&mut com, 8).unwrap();
simulate_execute_program(&mut com, 8, 0, 5).unwrap();
Expand All @@ -41,3 +39,21 @@ fn return_result_is_retried_n_times() {
}
assert_eq!([0u8], *simulate_get_status(&mut com).unwrap());
}

#[test]
fn result_is_deleted_after_transfer() {
let (_sched, mut com, _socat) = start_scheduler("results_deleted").unwrap();

simulate_test_store_archive(&mut com, 8).unwrap();
simulate_execute_program(&mut com, 8, 3, 5).unwrap();
std::thread::sleep(std::time::Duration::from_millis(400));
assert_eq!(simulate_get_status(&mut com).unwrap(), get_status_program_finished(8, 3, 0));
assert_eq!(simulate_get_status(&mut com).unwrap(), get_status_result_ready(8, 3));

simulate_return_result(&mut com, 8, 3).unwrap();
com.send_packet(&CEPPacket::Ack).unwrap();

assert_eq!(simulate_get_status(&mut com).unwrap(), vec![0]);
assert_eq!(std::fs::read_dir("tests/tmp/results_deleted/data").unwrap().count(), 0);
assert_eq!(std::fs::read_dir("tests/tmp/results_deleted/archives/8/results").unwrap().count(), 0);
}
3 changes: 1 addition & 2 deletions tests/simulation/full_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::{io::Cursor, time::Duration};

#[test]
fn full_run() {
let (mut com, _socat) = SimulationComHandle::with_socat_proc("full_run");
let _sched = start_scheduler("full_run").unwrap();
let (_sched, mut com, _socat) = start_scheduler("full_run").unwrap();

// store and execute program
simulate_test_store_archive(&mut com, 1).unwrap();
Expand Down
7 changes: 3 additions & 4 deletions tests/simulation/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::simulation::*;

#[test]
fn logfile_is_created() -> Result<(), std::io::Error> {
let (_, _proc) = SimulationComHandle::with_socat_proc("log_created");
let _sched = start_scheduler("log_created")?;
let (_sched, _com, _socat) = start_scheduler("log_created").unwrap();

std::thread::sleep(std::time::Duration::from_millis(400));

assert!(std::path::Path::new("./tests/tmp/log_created/log").exists());
Expand All @@ -12,8 +12,7 @@ fn logfile_is_created() -> Result<(), std::io::Error> {

#[test]
fn logfile_is_cleared_after_sent() -> std::io::Result<()> {
let (mut com, _socat) = SimulationComHandle::with_socat_proc("log_is_cleared_after_sent");
let _sched = start_scheduler("log_is_cleared_after_sent")?;
let (_sched, mut com, _socat) = start_scheduler("log_is_cleared_after_sent").unwrap();

simulate_test_store_archive(&mut com, 1).unwrap();
com.send_packet(&CEPPacket::Data(execute_program(1, 0, 3))).unwrap();
Expand Down
13 changes: 7 additions & 6 deletions tests/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ pub struct SimulationComHandle<T: Read, U: Write> {
}

impl SimulationComHandle<ChildStdout, ChildStdin> {
fn with_socat_proc(unique: &str) -> (Self, PoisonedChild) {
fn with_socat_proc(socket_path: &str) -> (Self, PoisonedChild) {
let mut proc = std::process::Command::new("socat")
.arg("stdio")
.arg(format!("pty,raw,echo=0,link=/tmp/ttySTS1-{},b921600,wait-slave", unique))
.arg(format!("pty,raw,echo=0,link={},b921600,wait-slave", socket_path))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();

loop {
if std::path::Path::new(&format!("/tmp/ttySTS1-{unique}")).exists() {
if std::path::Path::new(socket_path).exists() {
break;
}
std::thread::sleep(Duration::from_millis(50));
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<T: Read, U: Write> CommunicationHandle for SimulationComHandle<T, U> {
fn get_config_str(unique: &str) -> String {
format!(
"
uart = \"/tmp/ttySTS1-{unique}\"
uart = \"uart\"
baudrate = 921600
heartbeat_pin = 34
update_pin = 35
Expand All @@ -85,17 +85,18 @@ impl Drop for PoisonedChild {
}
}

fn start_scheduler(unique: &str) -> Result<PoisonedChild, std::io::Error> {
fn start_scheduler(unique: &str) -> Result<(PoisonedChild, SimulationComHandle<ChildStdout, ChildStdin>, PoisonedChild), std::io::Error> {
let test_dir = format!("./tests/tmp/{}", unique);
let scheduler_bin = std::fs::canonicalize("./target/release/STS1_EDU_Scheduler")?;
let _ = std::fs::remove_dir_all(&test_dir);
std::fs::create_dir_all(&test_dir)?;
std::fs::write(format!("{}/config.toml", &test_dir), get_config_str(unique))?;
let (handle, socat) = SimulationComHandle::with_socat_proc(&format!("{}/uart", test_dir));

let scheduler =
std::process::Command::new(scheduler_bin).current_dir(test_dir).spawn().unwrap();

Ok(PoisonedChild(scheduler))
Ok((PoisonedChild(scheduler), handle, socat))
}

pub fn simulate_test_store_archive(
Expand Down
12 changes: 6 additions & 6 deletions tests/simulation/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::io::Write;
use std::os::unix::net::UnixStream;
use std::time::Duration;

use super::{simulate_get_status, start_scheduler, SimulationComHandle};
use super::{simulate_get_status, start_scheduler};

#[test]
fn dosimeter_events_are_added() {
let (mut com, _socat) = SimulationComHandle::with_socat_proc("dosimeter");
let _sched = start_scheduler("dosimeter").unwrap();
let (_sched, mut com, _socat) = start_scheduler("dosimeter").unwrap();

std::thread::sleep(Duration::from_millis(200));

{
Expand All @@ -21,11 +21,11 @@ fn dosimeter_events_are_added() {

#[test]
fn multiple_dosimeter_events() {
let (mut com, _socat) = SimulationComHandle::with_socat_proc("dosimeter-multi");
let _sched = start_scheduler("dosimeter-multi").unwrap();
let (_sched, mut com, _socat) = start_scheduler("dosimeter_multi").unwrap();

std::thread::sleep(Duration::from_millis(200));

let mut socket = UnixStream::connect("/tmp/STS1_EDU_Scheduler_SIM_dosimeter-multi").unwrap();
let mut socket = UnixStream::connect("/tmp/STS1_EDU_Scheduler_SIM_dosimeter_multi").unwrap();
for _ in 0..10 {
writeln!(socket, "dosimeter/on").unwrap();
writeln!(socket, "dosimeter/off").unwrap();
Expand Down
5 changes: 2 additions & 3 deletions tests/simulation/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::{get_status, start_scheduler, SimulationComHandle};
use super::{get_status, start_scheduler};
use std::time::Duration;
use STS1_EDU_Scheduler::communication::{CEPPacket, CommunicationHandle};

#[test]
fn integrity_ack_timeout_is_honored() {
let (mut cobc, _socat) = SimulationComHandle::with_socat_proc("integrity_timeout");
let _sched = start_scheduler("integrity_timeout").unwrap();
let (_sched, mut cobc, _socat) = start_scheduler("integrity_timeout").unwrap();

// Check that delayed ACK is allowed
cobc.send_packet(&CEPPacket::Data(get_status())).unwrap();
Expand Down

0 comments on commit c6057a3

Please sign in to comment.