Skip to content

Commit

Permalink
fix race conditions in tests with socat
Browse files Browse the repository at this point in the history
  • Loading branch information
estokes committed Dec 31, 2024
1 parent 47f2400 commit c312358
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static LOGGING_INIT: Once = Once::new();

/// Default serial port names used for testing
#[cfg(unix)]
const DEFAULT_TEST_PORT_NAMES: &'static str = "/tty/USB0;/tty/USB1";
const DEFAULT_TEST_PORT_NAMES: &'static str = "USB0;USB1";

/// Default serial port names used for testing
#[cfg(windows)]
Expand Down Expand Up @@ -185,20 +185,25 @@ pub struct Fixture {
impl Drop for Fixture {
fn drop(&mut self) {
log::trace!("stopping socat process (id: {})...", self.process.id());

self.process.kill().ok();
thread::sleep(Duration::from_millis(250));
log::trace!("removing link: {}", self.port_a);
std::fs::remove_file(self.port_a).ok();
log::trace!("removing link: {}", self.port_b);
std::fs::remove_file(self.port_b).ok();
thread::sleep(Duration::from_millis(1000));
log::trace!("removing link: {:?}", self.port_a);
std::fs::remove_file(&self.port_a).ok();
log::trace!("removing link: {:?}", self.port_b);
std::fs::remove_file(&self.port_b).ok();
thread::sleep(Duration::from_millis(1000));
}
}

impl Fixture {
#[cfg(unix)]
pub fn new(port_a: &'static str, port_b: &'static str) -> Self {
use std::sync::atomic::{AtomicUsize, Ordering};
static N: AtomicUsize = AtomicUsize::new(0);
LOGGING_INIT.call_once(|| env_logger::init());
let n = N.fetch_add(1, Ordering::Relaxed);
let port_a = format!("{}{}", port_a, n).leak();
let port_b = format!("{}{}", port_b, n).leak();
let args = [
format!("PTY,link={}", port_a),
format!("PTY,link={}", port_b),
Expand All @@ -210,9 +215,7 @@ impl Fixture {
.spawn()
.expect("unable to spawn socat process");
log::trace!(".... done! (pid: {:?})", process.id());

thread::sleep(Duration::from_millis(500));

thread::sleep(Duration::from_millis(1000));
Self {
process,
port_a,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serialstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn test_read_write_pair() {
}

// Same as test_send_recv but use a cloned receiver
#[cfg(never)]
#[cfg(any())]
#[test]
fn test_try_clone_native() {
const DATA1: &[u8] = b"Here is an example string";
Expand Down

0 comments on commit c312358

Please sign in to comment.