Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky wait_closed test #6651

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions talpid-openvpn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,20 +1174,47 @@ mod tests {
}

#[derive(Debug, Copy, Clone)]
struct TestProcessHandle(i32);
struct TestProcessHandle {
exit_code: i32,
forever: bool,
}

#[async_trait::async_trait]
impl ProcessHandle for TestProcessHandle {
#[cfg(unix)]
async fn wait(&mut self) -> io::Result<ExitStatus> {
use std::os::unix::process::ExitStatusExt;
Ok(ExitStatus::from_raw(self.0))
impl TestProcessHandle {
pub fn immediate(exit_code: i32) -> Self {
Self {
exit_code,
forever: false,
}
}

#[cfg(windows)]
pub fn run_forever() -> Self {
Self {
exit_code: 0,
forever: true,
}
}

fn status(&self) -> ExitStatus {
#[cfg(windows)]
{
use std::os::windows::process::ExitStatusExt;
ExitStatus::from_raw(self.exit_code as u32)
}
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;
ExitStatus::from_raw(self.exit_code)
}
}
}

#[async_trait::async_trait]
impl ProcessHandle for TestProcessHandle {
async fn wait(&mut self) -> io::Result<ExitStatus> {
use std::os::windows::process::ExitStatusExt;
Ok(ExitStatus::from_raw(self.0 as u32))
if self.forever {
let _: () = futures::future::pending().await;
}
Ok(self.status())
}

fn kill(&mut self) {}
Expand Down Expand Up @@ -1253,7 +1280,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exit_successfully() {
let builder = TestOpenVpnBuilder {
process_handle: Some(TestProcessHandle(0)),
process_handle: Some(TestProcessHandle::immediate(0)),
..Default::default()
};
let openvpn_init_args = create_init_args();
Expand All @@ -1271,7 +1298,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exit_error() {
let builder = TestOpenVpnBuilder {
process_handle: Some(TestProcessHandle(1)),
process_handle: Some(TestProcessHandle::immediate(1)),
..Default::default()
};
let openvpn_init_args = create_init_args();
Expand All @@ -1286,10 +1313,11 @@ mod tests {
assert!(testee.wait().await.is_err());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
/// Test that the `OpenVpnMonitor` stops when the close handle closes it.
#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn wait_closed() {
let builder = TestOpenVpnBuilder {
process_handle: Some(TestProcessHandle(1)),
process_handle: Some(TestProcessHandle::run_forever()),
..Default::default()
};
let openvpn_init_args = create_init_args();
Expand All @@ -1303,9 +1331,11 @@ mod tests {
.unwrap();

testee.close_handle().close();
let result = testee.wait().await;
println!("[testee.wait(): {:?}]", result);
assert!(result.is_ok());

tokio::time::timeout(std::time::Duration::from_secs(10), testee.wait())
.await
.expect("expected close handle to stop monitor")
.expect("expected successful result");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
Expand Down
Loading