Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/tools/remote-test-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use std::{env, thread};
const REMOTE_ADDR_ENV: &str = "TEST_DEVICE_ADDR";
const DEFAULT_ADDR: &str = "127.0.0.1:12345";

const CONNECT_TIMEOUT_ENV: &str = "TEST_DEVICE_CONNECT_TIMEOUT_SECONDS";
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_mins(30);

macro_rules! t {
($e:expr) => {
match $e {
Expand Down Expand Up @@ -69,19 +72,38 @@ fn spawn_emulator(target: &str, server: &Path, tmpdir: &Path, rootfs: Option<Pat
}

// Wait for the emulator to come online
loop {
let mut total_dur = Duration::from_secs(0);
let timeout = env::var(CONNECT_TIMEOUT_ENV).ok().map_or(DEFAULT_CONNECT_TIMEOUT, |timeout| {
let seconds = timeout.parse::<u64>().unwrap_or_else(|_| {
panic!("The {CONNECT_TIMEOUT_ENV} env variable is not a valid integer");
});
Duration::from_secs(seconds)
});

let mut timed_out = true;

while total_dur < timeout {
let dur = Duration::from_millis(2000);
if let Ok(mut client) = TcpStream::connect(&device_address) {
t!(client.set_read_timeout(Some(dur)));
t!(client.set_write_timeout(Some(dur)));
if client.write_all(b"ping").is_ok() {
let mut b = [0; 4];
if client.read_exact(&mut b).is_ok() {
timed_out = false;
break;
}
}
}
thread::sleep(dur);
total_dur += dur;
}

if timed_out {
panic!(
"Gave up trying to connect to test device at {device_address} after {} seconds",
total_dur.as_secs()
);
}
}

Expand Down
Loading