From 108cc1e6cce936b9d84e58b93d1c779dc21846f4 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 29 Jan 2021 13:52:05 +1100 Subject: [PATCH] Use trait instead of passing struct --- swap/tests/happy_path.rs | 7 ++----- swap/tests/happy_path_restart_alice.rs | 7 ++----- swap/tests/happy_path_restart_bob_after_comm.rs | 7 ++----- .../happy_path_restart_bob_after_lock_proof_received.rs | 7 ++----- swap/tests/happy_path_restart_bob_before_comm.rs | 7 ++----- swap/tests/punish.rs | 7 ++----- swap/tests/refund_restart_alice.rs | 7 ++----- swap/tests/refund_restart_alice_cancelled.rs | 3 +-- swap/tests/testutils/mod.rs | 5 ++++- 9 files changed, 19 insertions(+), 38 deletions(-) diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index d0eb665d..548aee86 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -1,9 +1,6 @@ pub mod testutils; -use swap::{ - config::GetConfig, - protocol::{alice, bob}, -}; +use swap::protocol::{alice, bob}; use testutils::SlowCancelConfig; use tokio::join; @@ -11,7 +8,7 @@ use tokio::join; #[tokio::test] async fn happy_path() { - testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { + testutils::setup_test(SlowCancelConfig, |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index fc60ff0b..fe38a09d 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -1,14 +1,11 @@ pub mod testutils; -use swap::{ - config::GetConfig, - protocol::{alice, alice::AliceState, bob}, -}; +use swap::protocol::{alice, alice::AliceState, bob}; use testutils::{alice_run_until::is_encsig_learned, SlowCancelConfig}; #[tokio::test] async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { - testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { + testutils::setup_test(SlowCancelConfig, |mut ctx| async move { let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index a4667a98..09024be6 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -1,14 +1,11 @@ pub mod testutils; -use swap::{ - config::GetConfig, - protocol::{alice, bob, bob::BobState}, -}; +use swap::protocol::{alice, bob, bob::BobState}; use testutils::{bob_run_until::is_encsig_sent, SlowCancelConfig}; #[tokio::test] async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { - testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { + testutils::setup_test(SlowCancelConfig, |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs index ca5e2da3..e742fbed 100644 --- a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs +++ b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs @@ -1,14 +1,11 @@ pub mod testutils; -use swap::{ - config::GetConfig, - protocol::{alice, bob, bob::BobState}, -}; +use swap::protocol::{alice, bob, bob::BobState}; use testutils::{bob_run_until::is_lock_proof_received, SlowCancelConfig}; #[tokio::test] async fn given_bob_restarts_after_lock_proof_received_resume_swap() { - testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { + testutils::setup_test(SlowCancelConfig, |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index f9d35e76..06306cca 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -1,14 +1,11 @@ pub mod testutils; -use swap::{ - config::GetConfig, - protocol::{alice, bob, bob::BobState}, -}; +use swap::protocol::{alice, bob, bob::BobState}; use testutils::{bob_run_until::is_xmr_locked, SlowCancelConfig}; #[tokio::test] async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { - testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { + testutils::setup_test(SlowCancelConfig, |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 9e354a01..7da44631 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -1,16 +1,13 @@ pub mod testutils; -use swap::{ - config::GetConfig, - protocol::{alice, bob, bob::BobState}, -}; +use swap::protocol::{alice, bob, bob::BobState}; use testutils::{bob_run_until::is_btc_locked, FastPunishConfig}; /// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice /// the encsig and fail to refund or redeem. Alice punishes. #[tokio::test] async fn alice_punishes_if_bob_never_acts_after_fund() { - testutils::setup_test(FastPunishConfig::get_config(), |mut ctx| async move { + testutils::setup_test(FastPunishConfig, |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index f550a247..475212f3 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -1,16 +1,13 @@ pub mod testutils; -use swap::{ - config::GetConfig, - protocol::{alice, alice::AliceState, bob}, -}; +use swap::protocol::{alice, alice::AliceState, bob}; use testutils::{alice_run_until::is_xmr_locked, FastCancelConfig}; /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// then also refunds. #[tokio::test] async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { - testutils::setup_test(FastCancelConfig::get_config(), |mut ctx| async move { + testutils::setup_test(FastCancelConfig, |mut ctx| async move { let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/refund_restart_alice_cancelled.rs b/swap/tests/refund_restart_alice_cancelled.rs index 47da9363..7c47c171 100644 --- a/swap/tests/refund_restart_alice_cancelled.rs +++ b/swap/tests/refund_restart_alice_cancelled.rs @@ -2,7 +2,6 @@ pub mod testutils; use swap::{ config, - config::GetConfig, protocol::{alice, alice::AliceState, bob}, }; use testutils::alice_run_until::is_encsig_learned; @@ -12,7 +11,7 @@ use testutils::alice_run_until::is_encsig_learned; /// redeem had the timelock not expired. #[tokio::test] async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_refund_swap() { - testutils::setup_test(config::Regtest::get_config(), |mut ctx| async move { + testutils::setup_test(config::Regtest, |mut ctx| async move { let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 9513a2dd..5d8f364f 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -304,15 +304,18 @@ impl TestContext { } } -pub async fn setup_test(config: Config, testfn: T) +pub async fn setup_test(_config: C, testfn: T) where T: Fn(TestContext) -> F, F: Future, + C: GetConfig, { let cli = Cli::default(); let _guard = init_tracing(); + let config = C::get_config(); + let (monero, containers) = testutils::init_containers(&cli).await; let swap_amounts = SwapAmounts {