Skip to content

Commit 1d1f5fd

Browse files
committed
Add a timeout to the remote-test-client connection
1 parent 695857b commit 1d1f5fd

File tree

1 file changed

+13
-2
lines changed
  • src/tools/remote-test-client/src

1 file changed

+13
-2
lines changed

src/tools/remote-test-client/src/main.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::time::Duration;
1515
use std::{env, thread};
1616

1717
const REMOTE_ADDR_ENV: &str = "TEST_DEVICE_ADDR";
18+
const CONNECT_TIMEOUT: &str = "TEST_DEVICE_CONNECT_TIMEOUT";
1819
const DEFAULT_ADDR: &str = "127.0.0.1:12345";
1920

2021
macro_rules! t {
@@ -69,20 +70,30 @@ fn spawn_emulator(target: &str, server: &Path, tmpdir: &Path, rootfs: Option<Pat
6970
}
7071

7172
// Wait for the emulator to come online
72-
loop {
73+
let mut total_dur = Duration::from_secs(0);
74+
let timeout = env::var(CONNECT_TIMEOUT)
75+
.ok()
76+
.and_then(|timeout| timeout.parse::<u64>().ok())
77+
.map(Duration::from_secs)
78+
.unwrap_or_else(|| Duration::from_secs(300));
79+
80+
while total_dur < timeout {
7381
let dur = Duration::from_millis(2000);
7482
if let Ok(mut client) = TcpStream::connect(&device_address) {
7583
t!(client.set_read_timeout(Some(dur)));
7684
t!(client.set_write_timeout(Some(dur)));
7785
if client.write_all(b"ping").is_ok() {
7886
let mut b = [0; 4];
7987
if client.read_exact(&mut b).is_ok() {
80-
break;
88+
return;
8189
}
8290
}
8391
}
8492
thread::sleep(dur);
93+
total_dur += dur;
8594
}
95+
96+
panic!("Test device at {device_address} timed out");
8697
}
8798

8899
fn start_android_emulator(server: &Path) {

0 commit comments

Comments
 (0)