Skip to content

Commit

Permalink
Remove Box<dyn Any>
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Apr 9, 2024
1 parent f0b3099 commit 705c121
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
15 changes: 9 additions & 6 deletions test/test-manager/src/mullvad_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (TODO: Link to test macro) need 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,
}
}

Expand Down
14 changes: 7 additions & 7 deletions test/test-manager/src/run_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
logging::{panic_as_string, TestOutput},
mullvad_daemon,
mullvad_daemon::{self, MullvadClientArgument},
summary::{self, maybe_log_test_result},
tests,
tests::{config::TEST_CONFIG, TestContext},
Expand Down Expand Up @@ -82,9 +82,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);
Expand All @@ -96,7 +96,7 @@ pub async fn run(

let test_result = run_test(
client.clone(),
mclient,
mullvad_client,
&test.func,
test.name,
test_context.clone(),
Expand Down Expand Up @@ -177,15 +177,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;
Expand Down
9 changes: 3 additions & 6 deletions test/test-manager/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
20 changes: 8 additions & 12 deletions test/test-manager/test_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand All @@ -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)
})
Expand Down Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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,
}
}
Expand Down

0 comments on commit 705c121

Please sign in to comment.