Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from all 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
Give remote-test-client a configurable timeout
  • Loading branch information
Hoverbear committed Jun 25, 2024
commit 138448577af23e82a7e83f5e2ae2e7da00eadbe8
19 changes: 16 additions & 3 deletions src/tools/remote-test-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use std::time::Duration;
const REMOTE_ADDR_ENV: &str = "TEST_DEVICE_ADDR";
const DEFAULT_ADDR: &str = "127.0.0.1:12345";

const TCP_TIMEOUT_ENV: &str = "TCP_TIMEOUT";
const DEFAULT_TCP_TIMEOUT: u64 = 100;

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

let timeout = if let Ok(setting) = env::var(TCP_TIMEOUT_ENV) {
t!(setting.parse())
} else {
DEFAULT_TCP_TIMEOUT
};
let dur = Duration::from_millis(timeout);

// Wait for the emulator to come online
loop {
let dur = Duration::from_millis(100);
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() {
break;
match client.read_exact(&mut b) {
Ok(_) => break,
Err(e) if e.kind() == io::ErrorKind::WouldBlock => panic!(
"TCP timeout of {timeout}ms exceeded, set `{TCP_TIMEOUT_ENV}` to a larger value"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a more specific and appropriate error, yes! I wonder why I saw what I did while testing...

),
Err(_) => (),
}
}
}
Expand Down