@@ -15,9 +15,11 @@ use std::time::Duration;
1515use std:: { env, thread} ;
1616
1717const REMOTE_ADDR_ENV : & str = "TEST_DEVICE_ADDR" ;
18- const CONNECT_TIMEOUT : & str = "TEST_DEVICE_CONNECT_TIMEOUT" ;
1918const DEFAULT_ADDR : & str = "127.0.0.1:12345" ;
2019
20+ const CONNECT_TIMEOUT_ENV : & str = "TEST_DEVICE_CONNECT_TIMEOUT_SECONDS" ;
21+ const DEFAULT_CONNECT_TIMEOUT : Duration = Duration :: from_mins ( 30 ) ;
22+
2123macro_rules! t {
2224 ( $e: expr) => {
2325 match $e {
@@ -71,19 +73,14 @@ fn spawn_emulator(target: &str, server: &Path, tmpdir: &Path, rootfs: Option<Pat
7173
7274 // Wait for the emulator to come online
7375 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 ) ) ;
76+ let timeout = env:: var ( CONNECT_TIMEOUT_ENV ) . ok ( ) . map_or ( DEFAULT_CONNECT_TIMEOUT , |timeout| {
77+ let seconds = timeout. parse :: < u64 > ( ) . unwrap_or_else ( |_| {
78+ panic ! ( "The {CONNECT_TIMEOUT_ENV} env variable is not a valid integer" ) ;
79+ } ) ;
80+ Duration :: from_secs ( seconds)
81+ } ) ;
82+
83+ let mut timed_out = true ;
8784
8885 while total_dur < timeout {
8986 let dur = Duration :: from_millis ( 2000 ) ;
@@ -93,15 +90,21 @@ fn spawn_emulator(target: &str, server: &Path, tmpdir: &Path, rootfs: Option<Pat
9390 if client. write_all ( b"ping" ) . is_ok ( ) {
9491 let mut b = [ 0 ; 4 ] ;
9592 if client. read_exact ( & mut b) . is_ok ( ) {
96- return ;
93+ timed_out = false ;
94+ break ;
9795 }
9896 }
9997 }
10098 thread:: sleep ( dur) ;
10199 total_dur += dur;
102100 }
103101
104- panic ! ( "Test device at {device_address} timed out" ) ;
102+ if timed_out {
103+ panic ! (
104+ "Gave up trying to connect to test device at {device_address} after {} seconds" ,
105+ total_dur. as_secs( )
106+ ) ;
107+ }
105108}
106109
107110fn start_android_emulator ( server : & Path ) {
0 commit comments