From 8708d27603e02507f6a0e6c1717d0e24e4377f6d Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 14 Jan 2021 11:02:19 +1100 Subject: [PATCH] WIP - Add highter order function for test init to solve lifetime issues of cli --- swap/tests/happy_path.rs | 14 +++++--------- swap/tests/testutils/mod.rs | 30 ++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index b8b432f0..eb8d6715 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -8,14 +8,10 @@ pub mod testutils; #[tokio::test] async fn happy_path() { - let mut test = Test::new( - bitcoin::Amount::from_sat(1_000_000), - monero::Amount::from_piconero(1_000_000_000_000), - ) - .await; + testutils::test(|alice, bob| async move { + join!(alice.swap(), bob.swap()); - join!(test.alice.swap(), test.bob.swap()); - - test.alice.assert_btc_redeemed(); - test.bob.assert_btc_redeemed(); + alice.assert_btc_redeemed(); + bob.assert_btc_redeemed(); + }).await; } diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 37ff6ca4..65a6719c 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -12,10 +12,28 @@ use monero_harness::{image, Monero}; use std::sync::Arc; use swap::{bitcoin, config::Config, monero, seed::Seed}; +use std::future::Future; use testcontainers::{clients::Cli, Container}; use tracing_core::dispatcher::DefaultGuard; use tracing_log::LogTracer; +pub async fn test(testfn: T) +where + T: Fn(Alice, Bob) -> F, + F: Future, +{ + let cli = Cli::default(); + + let test = Test::new( + bitcoin::Amount::from_sat(1_000_000), + monero::Amount::from_piconero(1_000_000_000_000), + &cli, + ) + .await; + + testfn(test.alice, test.bob).await +} + pub struct Test<'a> { pub alice: Alice, pub bob: Bob, @@ -23,10 +41,13 @@ pub struct Test<'a> { } impl<'a> Test<'a> { - pub async fn new(btc_to_swap: bitcoin::Amount, xmr_to_swap: monero::Amount) -> Test<'a> { + pub async fn new( + btc_to_swap: bitcoin::Amount, + xmr_to_swap: monero::Amount, + cli: &'a Cli, + ) -> Test<'a> { let _guard = init_tracing(); - let cli = Cli::default(); let (monero, containers) = testutils::init_containers(&cli).await; let bob_btc_starting_balance = btc_to_swap * 10; @@ -84,10 +105,7 @@ pub async fn init_containers<'a>(cli: &'a Cli) -> (Monero, Containers<'a>) { .await .unwrap(); - (monero, Containers { - bitcoind, - monerods, - }) + (monero, Containers { bitcoind, monerods }) } pub async fn init_wallets(