@@ -1174,20 +1174,47 @@ mod tests {
1174
1174
}
1175
1175
1176
1176
#[ derive( Debug , Copy , Clone ) ]
1177
- struct TestProcessHandle ( i32 ) ;
1177
+ struct TestProcessHandle {
1178
+ exit_code : i32 ,
1179
+ forever : bool ,
1180
+ }
1178
1181
1179
- # [ async_trait :: async_trait ]
1180
- impl ProcessHandle for TestProcessHandle {
1181
- # [ cfg ( unix ) ]
1182
- async fn wait ( & mut self ) -> io :: Result < ExitStatus > {
1183
- use std :: os :: unix :: process :: ExitStatusExt ;
1184
- Ok ( ExitStatus :: from_raw ( self . 0 ) )
1182
+ impl TestProcessHandle {
1183
+ pub fn immediate ( exit_code : i32 ) -> Self {
1184
+ Self {
1185
+ exit_code ,
1186
+ forever : false ,
1187
+ }
1185
1188
}
1186
1189
1187
- #[ cfg( windows) ]
1190
+ pub fn run_forever ( ) -> Self {
1191
+ Self {
1192
+ exit_code : 0 ,
1193
+ forever : true ,
1194
+ }
1195
+ }
1196
+
1197
+ fn status ( & self ) -> ExitStatus {
1198
+ #[ cfg( windows) ]
1199
+ {
1200
+ use std:: os:: windows:: process:: ExitStatusExt ;
1201
+ ExitStatus :: from_raw ( self . exit_code as u32 )
1202
+ }
1203
+ #[ cfg( unix) ]
1204
+ {
1205
+ use std:: os:: unix:: process:: ExitStatusExt ;
1206
+ ExitStatus :: from_raw ( self . exit_code )
1207
+ }
1208
+ }
1209
+ }
1210
+
1211
+ #[ async_trait:: async_trait]
1212
+ impl ProcessHandle for TestProcessHandle {
1188
1213
async fn wait ( & mut self ) -> io:: Result < ExitStatus > {
1189
- use std:: os:: windows:: process:: ExitStatusExt ;
1190
- Ok ( ExitStatus :: from_raw ( self . 0 as u32 ) )
1214
+ if self . forever {
1215
+ let _: ( ) = futures:: future:: pending ( ) . await ;
1216
+ }
1217
+ Ok ( self . status ( ) )
1191
1218
}
1192
1219
1193
1220
fn kill ( & mut self ) { }
@@ -1253,7 +1280,7 @@ mod tests {
1253
1280
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
1254
1281
async fn exit_successfully ( ) {
1255
1282
let builder = TestOpenVpnBuilder {
1256
- process_handle : Some ( TestProcessHandle ( 0 ) ) ,
1283
+ process_handle : Some ( TestProcessHandle :: immediate ( 0 ) ) ,
1257
1284
..Default :: default ( )
1258
1285
} ;
1259
1286
let openvpn_init_args = create_init_args ( ) ;
@@ -1271,7 +1298,7 @@ mod tests {
1271
1298
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
1272
1299
async fn exit_error ( ) {
1273
1300
let builder = TestOpenVpnBuilder {
1274
- process_handle : Some ( TestProcessHandle ( 1 ) ) ,
1301
+ process_handle : Some ( TestProcessHandle :: immediate ( 1 ) ) ,
1275
1302
..Default :: default ( )
1276
1303
} ;
1277
1304
let openvpn_init_args = create_init_args ( ) ;
@@ -1286,10 +1313,11 @@ mod tests {
1286
1313
assert ! ( testee. wait( ) . await . is_err( ) ) ;
1287
1314
}
1288
1315
1289
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
1316
+ /// Test that the `OpenVpnMonitor` stops when the close handle closes it.
1317
+ #[ tokio:: test( flavor = "current_thread" , start_paused = true ) ]
1290
1318
async fn wait_closed ( ) {
1291
1319
let builder = TestOpenVpnBuilder {
1292
- process_handle : Some ( TestProcessHandle ( 1 ) ) ,
1320
+ process_handle : Some ( TestProcessHandle :: run_forever ( ) ) ,
1293
1321
..Default :: default ( )
1294
1322
} ;
1295
1323
let openvpn_init_args = create_init_args ( ) ;
@@ -1303,9 +1331,11 @@ mod tests {
1303
1331
. unwrap ( ) ;
1304
1332
1305
1333
testee. close_handle ( ) . close ( ) ;
1306
- let result = testee. wait ( ) . await ;
1307
- println ! ( "[testee.wait(): {:?}]" , result) ;
1308
- assert ! ( result. is_ok( ) ) ;
1334
+
1335
+ tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 10 ) , testee. wait ( ) )
1336
+ . await
1337
+ . expect ( "expected close handle to stop monitor" )
1338
+ . expect ( "expected successful result" ) ;
1309
1339
}
1310
1340
1311
1341
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
0 commit comments