Skip to content

Commit

Permalink
Refactor test cases which use a tokio runtime
Browse files Browse the repository at this point in the history
Prefer to use the `tokio::test` attribute which ships with `tokio`
instead of manually creating a runtime for each test which needs it.
  • Loading branch information
MarkusPettersson98 committed Oct 10, 2023
1 parent 4f15f07 commit 34bf607
Showing 1 changed file with 59 additions and 90 deletions.
149 changes: 59 additions & 90 deletions talpid-openvpn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,14 +1236,6 @@ mod tests {
}
}

fn new_runtime() -> Result<tokio::runtime::Runtime> {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.map_err(Error::RuntimeError)
}

fn create_init_args_plugin_log(
plugin_path: PathBuf,
log_path: Option<PathBuf>,
Expand All @@ -1268,134 +1260,111 @@ mod tests {
create_init_args_plugin_log("".into(), None)
}

#[test]
fn sets_plugin() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn sets_plugin() {
let builder = TestOpenVpnBuilder::default();
let runtime = new_runtime().unwrap();
let openvpn_init_args = create_init_args_plugin_log("./my_test_plugin".into(), None);
let _ = runtime.block_on(async {
OpenVpnMonitor::new_internal(
builder.clone(),
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
});
let _ = OpenVpnMonitor::new_internal(
builder.clone(),
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
);
assert_eq!(
Some(PathBuf::from("./my_test_plugin")),
*builder.plugin.lock()
);
}

#[test]
fn sets_log() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn sets_log() {
let builder = TestOpenVpnBuilder::default();
let runtime = new_runtime().unwrap();
let openvpn_init_args =
create_init_args_plugin_log("".into(), Some(PathBuf::from("./my_test_log_file")));
let _ = runtime.block_on(async {
OpenVpnMonitor::new_internal(
builder.clone(),
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
});
let _ = OpenVpnMonitor::new_internal(
builder.clone(),
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
);
assert_eq!(
Some(PathBuf::from("./my_test_log_file")),
*builder.log.lock()
);
}

#[test]
fn exit_successfully() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exit_successfully() {
let builder = TestOpenVpnBuilder {
process_handle: Some(TestProcessHandle(0)),
..Default::default()
};
let runtime = new_runtime().unwrap();
let openvpn_init_args = create_init_args();
let testee = runtime
.block_on(async {
OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
})
.unwrap();
let testee = OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
.unwrap();
assert!(testee.wait().is_ok());
}

#[test]
fn exit_error() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exit_error() {
let builder = TestOpenVpnBuilder {
process_handle: Some(TestProcessHandle(1)),
..Default::default()
};
let runtime = new_runtime().unwrap();
let openvpn_init_args = create_init_args();
let testee = runtime
.block_on(async move {
OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
})
.unwrap();
let testee = OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
.unwrap();
assert!(testee.wait().is_err());
}

#[test]
fn wait_closed() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn wait_closed() {
let builder = TestOpenVpnBuilder {
process_handle: Some(TestProcessHandle(1)),
..Default::default()
};
let runtime = new_runtime().unwrap();
let openvpn_init_args = create_init_args();
let testee = runtime
.block_on(async {
OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
})
.unwrap();
let testee = OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
.unwrap();

// TODO: Remove this?
runtime.block_on(testee.close_handle().close()).unwrap();
testee.close_handle().close().await.unwrap();
let result = testee.wait();
println!("[testee.wait(): {:?}]", result);
assert!(result.is_ok());
}

#[test]
fn failed_process_start() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn failed_process_start() {
let builder = TestOpenVpnBuilder::default();
let runtime = new_runtime().unwrap();
let openvpn_init_args = create_init_args();
let result = runtime
.block_on(async {
OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
})
.unwrap();
let result = OpenVpnMonitor::new_internal(
builder,
openvpn_init_args,
TestOpenvpnEventProxy {},
#[cfg(windows)]
Box::new(TestWintunContext {}),
)
.unwrap();
match result.wait() {
Err(Error::StartProcessError) => (),
_ => panic!("Wrong error"),
Expand Down

0 comments on commit 34bf607

Please sign in to comment.