Skip to content

Commit 799df1c

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

File tree

1 file changed

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

1 file changed

+21
-2
lines changed

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

Lines changed: 21 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,38 @@ 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| {
77+
match timeout.parse::<u64>() {
78+
Ok(n) => Some(n),
79+
Err(_) => {
80+
println!("The {CONNECT_TIMEOUT} env variable is not a valid integer, using default timeout");
81+
None
82+
}
83+
}
84+
})
85+
.map(Duration::from_secs)
86+
.unwrap_or_else(|| Duration::from_secs(300));
87+
88+
while total_dur < timeout {
7389
let dur = Duration::from_millis(2000);
7490
if let Ok(mut client) = TcpStream::connect(&device_address) {
7591
t!(client.set_read_timeout(Some(dur)));
7692
t!(client.set_write_timeout(Some(dur)));
7793
if client.write_all(b"ping").is_ok() {
7894
let mut b = [0; 4];
7995
if client.read_exact(&mut b).is_ok() {
80-
break;
96+
return;
8197
}
8298
}
8399
}
84100
thread::sleep(dur);
101+
total_dur += dur;
85102
}
103+
104+
panic!("Test device at {device_address} timed out");
86105
}
87106

88107
fn start_android_emulator(server: &Path) {

0 commit comments

Comments
 (0)