Skip to content

Commit da185b0

Browse files
committed
Merge branch 'fix-flaky-openvpn-test'
2 parents 55caa17 + e20719f commit da185b0

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

talpid-openvpn/src/lib.rs

+47-17
Original file line numberDiff line numberDiff line change
@@ -1174,20 +1174,47 @@ mod tests {
11741174
}
11751175

11761176
#[derive(Debug, Copy, Clone)]
1177-
struct TestProcessHandle(i32);
1177+
struct TestProcessHandle {
1178+
exit_code: i32,
1179+
forever: bool,
1180+
}
11781181

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+
}
11851188
}
11861189

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 {
11881213
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())
11911218
}
11921219

11931220
fn kill(&mut self) {}
@@ -1253,7 +1280,7 @@ mod tests {
12531280
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
12541281
async fn exit_successfully() {
12551282
let builder = TestOpenVpnBuilder {
1256-
process_handle: Some(TestProcessHandle(0)),
1283+
process_handle: Some(TestProcessHandle::immediate(0)),
12571284
..Default::default()
12581285
};
12591286
let openvpn_init_args = create_init_args();
@@ -1271,7 +1298,7 @@ mod tests {
12711298
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
12721299
async fn exit_error() {
12731300
let builder = TestOpenVpnBuilder {
1274-
process_handle: Some(TestProcessHandle(1)),
1301+
process_handle: Some(TestProcessHandle::immediate(1)),
12751302
..Default::default()
12761303
};
12771304
let openvpn_init_args = create_init_args();
@@ -1286,10 +1313,11 @@ mod tests {
12861313
assert!(testee.wait().await.is_err());
12871314
}
12881315

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)]
12901318
async fn wait_closed() {
12911319
let builder = TestOpenVpnBuilder {
1292-
process_handle: Some(TestProcessHandle(1)),
1320+
process_handle: Some(TestProcessHandle::run_forever()),
12931321
..Default::default()
12941322
};
12951323
let openvpn_init_args = create_init_args();
@@ -1303,9 +1331,11 @@ mod tests {
13031331
.unwrap();
13041332

13051333
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");
13091339
}
13101340

13111341
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]

0 commit comments

Comments
 (0)