From 49ff22fc9c822c26d1fe139f354c483a85fb4f21 Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Wed, 6 Dec 2023 20:13:05 -0500 Subject: [PATCH] remove manual setup calls, use ctor to run before test setup --- Cargo.lock | 11 +++++++++++ xmtp_mls/Cargo.toml | 1 + xmtp_mls/src/api_client_wrapper.rs | 2 -- xmtp_mls/src/groups/mod.rs | 3 --- xmtp_mls/src/lib.rs | 4 +++- xmtp_mls/src/retry.rs | 12 ------------ xmtp_mls/src/storage/encrypted_store/mod.rs | 1 - 7 files changed, 15 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a354030e7..152e0102c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -947,6 +947,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctor" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e366bff8cd32dd8754b0991fb66b279dc48f598c3a18914852a6673deef583" +dependencies = [ + "quote", + "syn 2.0.39", +] + [[package]] name = "ctr" version = "0.9.2" @@ -6581,6 +6591,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "ctor", "diesel", "diesel_migrations", "ethers", diff --git a/xmtp_mls/Cargo.toml b/xmtp_mls/Cargo.toml index ff70743b4..f4407d8d4 100644 --- a/xmtp_mls/Cargo.toml +++ b/xmtp_mls/Cargo.toml @@ -57,3 +57,4 @@ tempfile = "3.5.0" tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } xmtp_api_grpc = { path = "../xmtp_api_grpc" } flume = "0.11" +ctor = "0.2" diff --git a/xmtp_mls/src/api_client_wrapper.rs b/xmtp_mls/src/api_client_wrapper.rs index f5795278f..0194d0da0 100644 --- a/xmtp_mls/src/api_client_wrapper.rs +++ b/xmtp_mls/src/api_client_wrapper.rs @@ -650,8 +650,6 @@ mod tests { #[tokio::test] async fn it_retries_twice_then_succeeds() { - crate::tests::setup(); - let mut mock_api = MockApiClient::new(); let topic = "topic"; let start_time_ns = 10; diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index 2010a112e..4164ff94b 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -798,8 +798,6 @@ mod tests { #[tokio::test] async fn test_add_members() { - crate::tests::setup(); - let client = ClientBuilder::new_test_client(generate_local_wallet().into()).await; let client_2 = ClientBuilder::new_test_client(generate_local_wallet().into()).await; client_2.register_identity().await.unwrap(); @@ -823,7 +821,6 @@ mod tests { #[tokio::test] async fn test_add_invalid_member() { - crate::tests::setup(); let client = ClientBuilder::new_test_client(generate_local_wallet().into()).await; let group = client.create_group().expect("create group"); diff --git a/xmtp_mls/src/lib.rs b/xmtp_mls/src/lib.rs index 910c0b31d..a82cb7b69 100644 --- a/xmtp_mls/src/lib.rs +++ b/xmtp_mls/src/lib.rs @@ -43,10 +43,12 @@ pub trait Delete { #[cfg(test)] mod tests { use std::sync::Once; + static INIT: Once = Once::new(); /// Setup for tests - pub fn setup() { + #[ctor::ctor] + fn setup() { INIT.call_once(|| { tracing_subscriber::fmt::init(); }) diff --git a/xmtp_mls/src/retry.rs b/xmtp_mls/src/retry.rs index cd0837c41..8abf02c5a 100644 --- a/xmtp_mls/src/retry.rs +++ b/xmtp_mls/src/retry.rs @@ -292,8 +292,6 @@ mod tests { #[test] fn it_retries_twice_and_succeeds() { - crate::tests::setup(); - let mut i = 0; let mut test_fn = || -> Result<(), SomeError> { if i == 2 { @@ -309,8 +307,6 @@ mod tests { #[test] fn it_works_with_random_args() { - crate::tests::setup(); - let mut i = 0; let list = vec!["String".into(), "Foo".into()]; let mut test_fn = || -> Result<(), SomeError> { @@ -326,8 +322,6 @@ mod tests { #[test] fn it_fails_on_three_retries() { - crate::tests::setup(); - let result: Result<(), SomeError> = retry!( Retry::default(), (|| -> Result<(), SomeError> { @@ -341,8 +335,6 @@ mod tests { #[test] fn it_only_runs_non_retryable_once() { - crate::tests::setup(); - let mut attempts = 0; let mut test_fn = || -> Result<(), SomeError> { attempts += 1; @@ -356,8 +348,6 @@ mod tests { #[tokio::test] async fn it_works_async() { - crate::tests::setup(); - async fn retryable_async_fn(rx: &flume::Receiver) -> Result<(), SomeError> { let val = rx.recv_async().await.unwrap(); if val == 2 { @@ -383,8 +373,6 @@ mod tests { #[tokio::test] async fn it_works_async_mut() { - crate::tests::setup(); - async fn retryable_async_fn(data: &mut usize) -> Result<(), SomeError> { if *data == 2 { return Ok(()); diff --git a/xmtp_mls/src/storage/encrypted_store/mod.rs b/xmtp_mls/src/storage/encrypted_store/mod.rs index fff68c864..8b68d2e84 100644 --- a/xmtp_mls/src/storage/encrypted_store/mod.rs +++ b/xmtp_mls/src/storage/encrypted_store/mod.rs @@ -279,7 +279,6 @@ mod tests { where F: FnOnce(&DbConnection) -> R, { - crate::tests::setup(); let store = EncryptedMessageStore::new( StorageOption::Ephemeral, EncryptedMessageStore::generate_enc_key(),