diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2024-04-09 13:14:49 +0200 |
|---|---|---|
| committer | Markus Pettersson <markus.pettersson@mullvad.net> | 2024-04-12 15:01:18 +0200 |
| commit | 39cac2951a0a63814541cade4f0f80b33bbbd18d (patch) | |
| tree | 5d83a31ece1d56e9e6623714c2a470d6c72bfa6a | |
| parent | 94c3ce99aebd53f8c740fe093d03f06f483ea692 (diff) | |
| download | mullvadvpn-39cac2951a0a63814541cade4f0f80b33bbbd18d.tar.xz mullvadvpn-39cac2951a0a63814541cade4f0f80b33bbbd18d.zip | |
Get rid of type casting for test function argument
Replace the `Box<dyn Any>` type for the third test function argument
'mullvad client' - replace it with a dedicated enum type
`MullvadClientArgument`. This change got rid of the type casting from
`Box<dyn Any>` to `MullvadProxyClient` done in the `test_function` macro.
| -rw-r--r-- | test/test-manager/src/mullvad_daemon.rs | 15 | ||||
| -rw-r--r-- | test/test-manager/src/run_tests.rs | 14 | ||||
| -rw-r--r-- | test/test-manager/src/tests/mod.rs | 9 | ||||
| -rw-r--r-- | test/test-manager/test_macro/src/lib.rs | 20 |
4 files changed, 27 insertions, 31 deletions
diff --git a/test/test-manager/src/mullvad_daemon.rs b/test/test-manager/src/mullvad_daemon.rs index e1a7680cb4..115a09e049 100644 --- a/test/test-manager/src/mullvad_daemon.rs +++ b/test/test-manager/src/mullvad_daemon.rs @@ -51,14 +51,17 @@ pub struct RpcClientProvider { service: DummyService, } +pub enum MullvadClientArgument { + WithClient(MullvadProxyClient), + None, +} + impl RpcClientProvider { - pub async fn as_type( - &self, - client_type: MullvadClientVersion, - ) -> Box<dyn std::any::Any + Send> { + /// Whether a [test case](test_macro::test_function) needs a [`MullvadProxyClient`]. + pub async fn mullvad_client(&self, client_type: MullvadClientVersion) -> MullvadClientArgument { match client_type { - MullvadClientVersion::New => Box::new(self.new_client().await), - MullvadClientVersion::None => Box::new(()), + MullvadClientVersion::New => MullvadClientArgument::WithClient(self.new_client().await), + MullvadClientVersion::None => MullvadClientArgument::None, } } diff --git a/test/test-manager/src/run_tests.rs b/test/test-manager/src/run_tests.rs index 0c7868e529..005d0c89d4 100644 --- a/test/test-manager/src/run_tests.rs +++ b/test/test-manager/src/run_tests.rs @@ -1,6 +1,6 @@ use crate::{ logging::{panic_as_string, TestOutput}, - mullvad_daemon, + mullvad_daemon::{self, MullvadClientArgument}, summary::{self, maybe_log_test_result}, tests::{self, config::TEST_CONFIG, get_tests, TestContext}, vm, @@ -80,9 +80,9 @@ pub async fn run( let logger = super::logging::Logger::get_or_init(); for test in tests { - let mclient = test_context + let mullvad_client = test_context .rpc_provider - .as_type(test.mullvad_client_version) + .mullvad_client(test.mullvad_client_version) .await; log::info!("Running {}", test.name); @@ -94,7 +94,7 @@ pub async fn run( let test_result = run_test( client.clone(), - mclient, + mullvad_client, &test.func, test.name, test_context.clone(), @@ -175,15 +175,15 @@ pub async fn run( final_result } -pub async fn run_test<F, R, MullvadClient>( +pub async fn run_test<F, R>( runner_rpc: ServiceClient, - mullvad_rpc: MullvadClient, + mullvad_rpc: MullvadClientArgument, test: &F, test_name: &'static str, test_context: super::tests::TestContext, ) -> TestOutput where - F: Fn(super::tests::TestContext, ServiceClient, MullvadClient) -> R, + F: Fn(super::tests::TestContext, ServiceClient, MullvadClientArgument) -> R, R: Future<Output = anyhow::Result<()>>, { let _flushed = runner_rpc.try_poll_output().await; diff --git a/test/test-manager/src/tests/mod.rs b/test/test-manager/src/tests/mod.rs index f4c4563bde..b89b4f0fd0 100644 --- a/test/test-manager/src/tests/mod.rs +++ b/test/test-manager/src/tests/mod.rs @@ -13,7 +13,7 @@ mod tunnel; mod tunnel_state; mod ui; -use crate::mullvad_daemon::RpcClientProvider; +use crate::mullvad_daemon::{MullvadClientArgument, RpcClientProvider}; use anyhow::Context; pub use test_metadata::TestMetadata; use test_rpc::ServiceClient; @@ -30,11 +30,8 @@ pub struct TestContext { pub rpc_provider: RpcClientProvider, } -pub type TestWrapperFunction = fn( - TestContext, - ServiceClient, - Box<dyn std::any::Any + Send>, -) -> BoxFuture<'static, anyhow::Result<()>>; +pub type TestWrapperFunction = + fn(TestContext, ServiceClient, MullvadClientArgument) -> BoxFuture<'static, anyhow::Result<()>>; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/test/test-manager/test_macro/src/lib.rs b/test/test-manager/test_macro/src/lib.rs index 161605e8ca..7e19990b97 100644 --- a/test/test-manager/test_macro/src/lib.rs +++ b/test/test-manager/test_macro/src/lib.rs @@ -199,20 +199,18 @@ fn create_test(test_function: TestFunction) -> proc_macro2::TokenStream { let func_name = test_function.name; let function_mullvad_version = test_function.function_parameters.mullvad_client.version(); let wrapper_closure = match test_function.function_parameters.mullvad_client { - MullvadClient::New { - mullvad_client_type, - .. - } => { - let mullvad_client_type = *mullvad_client_type; + MullvadClient::New { .. } => { quote! { |test_context: crate::tests::TestContext, rpc: test_rpc::ServiceClient, - mullvad_client: Box<dyn std::any::Any + Send>,| + mullvad_client: crate::mullvad_daemon::MullvadClientArgument| { - use std::any::Any; - let mullvad_client = mullvad_client.downcast::<#mullvad_client_type>().expect("invalid mullvad client"); + let mullvad_client = match mullvad_client { + crate::mullvad_daemon::MullvadClientArgument::WithClient(client) => client, + crate::mullvad_daemon::MullvadClientArgument::None => unreachable!("invalid mullvad client") + }; Box::pin(async move { - #func_name(test_context, rpc, *mullvad_client).await.map_err(Into::into) + #func_name(test_context, rpc, mullvad_client).await.map_err(Into::into) }) } } @@ -221,7 +219,7 @@ fn create_test(test_function: TestFunction) -> proc_macro2::TokenStream { quote! { |test_context: crate::tests::TestContext, rpc: test_rpc::ServiceClient, - _mullvad_client: Box<dyn std::any::Any + Send>| { + _mullvad_client: crate::mullvad_daemon::MullvadClientArgument| { Box::pin(async move { #func_name(test_context, rpc).await.map_err(Into::into) }) @@ -264,7 +262,6 @@ enum MullvadClient { mullvad_client_version: proc_macro2::TokenStream, }, New { - mullvad_client_type: Box<syn::Type>, mullvad_client_version: proc_macro2::TokenStream, }, } @@ -314,7 +311,6 @@ fn get_test_function_parameters( let mullvad_client_version = quote! { test_rpc::mullvad_daemon::MullvadClientVersion::New }; MullvadClient::New { - mullvad_client_type: pat_type.ty, mullvad_client_version, } } |
