From 1fe6b06c3d3a88bc8d6227b259089021027d37eb Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Wed, 5 Feb 2025 18:20:27 -0300 Subject: [PATCH] Remove the shared Mint instance between tests Since the SignatoryManager spawns a worker (with `tokio::spawn`) to execute the Signatory (which implements the `Signatory` trait), a mpsc channel is used to exchange messages. In side Tokio tests, each test has its own Tokio runtime. When the first test is done, since `instantiate` shares all the Mint instances, their Tokio runtime is dropped, and the tokio::spawn's task gets killed, breaking the channel. Since the SignatoryManager has no way to respawn, understandably so, since the task runs in an infinite loop, receiving messages and spawning new tasks to non-blocking. There are two choices: either create one Mint per test (which is what this PR does) or compile a modified version of the SignatoryManager for tests, which performs the calls to the Signatory trait in place instead of converting it to a message and passing to the signatory Tokio task. The first option is more straightforward and fixes other instances of the code where `tokio::spawn` is used, such as subscriptions. A third option could be explored, which would imply spawning a Tokio runtime in another OS-level thread, creating the Mint in that context, and sharing it with everything else; perhaps this should explored further if the idea is to test Mint this way. --- crates/cdk-integration-tests/tests/mint.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/crates/cdk-integration-tests/tests/mint.rs b/crates/cdk-integration-tests/tests/mint.rs index 03c0ceaa9..615beb9d8 100644 --- a/crates/cdk-integration-tests/tests/mint.rs +++ b/crates/cdk-integration-tests/tests/mint.rs @@ -22,13 +22,10 @@ use cdk::subscription::{IndexableParams, Params}; use cdk::util::unix_time; use cdk::Mint; use cdk_fake_wallet::FakeWallet; -use tokio::sync::OnceCell; use tokio::time::sleep; pub const MINT_URL: &str = "http://127.0.0.1:8088"; -static INSTANCE: OnceCell> = OnceCell::const_new(); - async fn new_mint(fee: u64) -> Mint { let mut supported_units = HashMap::new(); supported_units.insert(CurrencyUnit::Sat, (fee, 32)); @@ -70,13 +67,6 @@ async fn new_mint(fee: u64) -> Mint { .unwrap() } -async fn initialize() -> Arc { - INSTANCE - .get_or_init(|| async { Arc::new(new_mint(0).await) }) - .await - .clone() -} - async fn mint_proofs( mint: &Mint, amount: Amount, @@ -121,7 +111,7 @@ async fn mint_proofs( #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_mint_double_spend() -> Result<()> { - let mint = initialize().await; + let mint = new_mint(0).await; let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys; let keyset_id = Id::from(&keys); @@ -132,9 +122,7 @@ async fn test_mint_double_spend() -> Result<()> { let swap_request = SwapRequest::new(proofs.clone(), preswap.blinded_messages()); - let swap = mint.process_swap_request(swap_request).await; - - assert!(swap.is_ok()); + mint.process_swap_request(swap_request).await?; let preswap_two = PreMintSecrets::random(keyset_id, 100.into(), &SplitTarget::default())?; @@ -155,7 +143,7 @@ async fn test_mint_double_spend() -> Result<()> { /// This will work if the mint does not check for outputs amounts overflowing #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_attempt_to_swap_by_overflowing() -> Result<()> { - let mint = initialize().await; + let mint = new_mint(0).await; let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys; let keyset_id = Id::from(&keys); @@ -192,7 +180,7 @@ async fn test_attempt_to_swap_by_overflowing() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] pub async fn test_p2pk_swap() -> Result<()> { - let mint = initialize().await; + let mint = new_mint(0).await; let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys; let keyset_id = Id::from(&keys); @@ -310,7 +298,7 @@ pub async fn test_p2pk_swap() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_swap_unbalanced() -> Result<()> { - let mint = initialize().await; + let mint = new_mint(0).await; let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys; let keyset_id = Id::from(&keys);