From acfd43ee7981b6cc320169f02fca850ea6a45627 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Wed, 20 Jan 2021 10:40:40 +1100 Subject: [PATCH] Rename Test to TestContext and argument to ctx --- swap/tests/happy_path.rs | 10 +++++----- swap/tests/happy_path_restart_alice.rs | 12 ++++++------ swap/tests/happy_path_restart_bob_after_comm.rs | 12 ++++++------ swap/tests/happy_path_restart_bob_before_comm.rs | 12 ++++++------ swap/tests/punish.rs | 12 ++++++------ swap/tests/refund_restart_alice.rs | 12 ++++++------ swap/tests/testutils/mod.rs | 8 ++++---- 7 files changed, 39 insertions(+), 39 deletions(-) diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index d3385229..700c5d05 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -7,17 +7,17 @@ pub mod testutils; #[tokio::test] async fn happy_path() { - testutils::setup_test(|test| async move { - let alice_swap = test.new_swap_as_alice().await; - let bob_swap = test.new_swap_as_bob().await; + testutils::setup_test(|ctx| async move { + let alice_swap = ctx.new_swap_as_alice().await; + let bob_swap = ctx.new_swap_as_bob().await; let alice = alice::run(alice_swap); let bob = bob::run(bob_swap); let (alice_state, bob_state) = join!(alice, bob); - test.assert_alice_redeemed(alice_state.unwrap()).await; - test.assert_bob_redeemed(bob_state.unwrap()).await; + ctx.assert_alice_redeemed(alice_state.unwrap()).await; + ctx.assert_bob_redeemed(bob_state.unwrap()).await; }) .await; } diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index 6cce546b..dcd39f8e 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -4,9 +4,9 @@ pub mod testutils; #[tokio::test] async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { - testutils::setup_test(|test| async move { - let alice_swap = test.new_swap_as_alice().await; - let bob_swap = test.new_swap_as_bob().await; + testutils::setup_test(|ctx| async move { + let alice_swap = ctx.new_swap_as_alice().await; + let bob_swap = ctx.new_swap_as_bob().await; let bob = bob::run(bob_swap); let bob_handle = tokio::spawn(bob); @@ -16,15 +16,15 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { .unwrap(); assert!(matches!(alice_state, AliceState::EncSigLearned {..})); - let alice_swap = test.recover_alice_from_db().await; + let alice_swap = ctx.recover_alice_from_db().await; assert!(matches!(alice_swap.state, AliceState::EncSigLearned {..})); let alice_state = alice::run(alice_swap).await.unwrap(); - test.assert_alice_redeemed(alice_state).await; + ctx.assert_alice_redeemed(alice_state).await; let bob_state = bob_handle.await.unwrap(); - test.assert_bob_redeemed(bob_state.unwrap()).await + ctx.assert_bob_redeemed(bob_state.unwrap()).await }) .await; } diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index 194b6349..a1c04034 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -4,9 +4,9 @@ pub mod testutils; #[tokio::test] async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { - testutils::setup_test(|test| async move { - let alice_swap = test.new_swap_as_alice().await; - let bob_swap = test.new_swap_as_bob().await; + testutils::setup_test(|ctx| async move { + let alice_swap = ctx.new_swap_as_alice().await; + let bob_swap = ctx.new_swap_as_bob().await; let alice = alice::run(alice_swap); let alice_handle = tokio::spawn(alice); @@ -17,15 +17,15 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { assert!(matches!(bob_state, BobState::EncSigSent {..})); - let bob_swap = test.recover_bob_from_db().await; + let bob_swap = ctx.recover_bob_from_db().await; assert!(matches!(bob_swap.state, BobState::EncSigSent {..})); let bob_state = bob::run(bob_swap).await.unwrap(); - test.assert_bob_redeemed(bob_state).await; + ctx.assert_bob_redeemed(bob_state).await; let alice_state = alice_handle.await.unwrap(); - test.assert_alice_redeemed(alice_state.unwrap()).await; + ctx.assert_alice_redeemed(alice_state.unwrap()).await; }) .await; } diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index aea07c52..231ed1e5 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -7,9 +7,9 @@ pub mod testutils; #[tokio::test] async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { - testutils::setup_test(|test| async move { - let alice_swap = test.new_swap_as_alice().await; - let bob_swap = test.new_swap_as_bob().await; + testutils::setup_test(|ctx| async move { + let alice_swap = ctx.new_swap_as_alice().await; + let bob_swap = ctx.new_swap_as_bob().await; let alice_handle = alice::run(alice_swap); let alice_swap_handle = tokio::spawn(alice_handle); @@ -18,15 +18,15 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { assert!(matches!(bob_state, BobState::XmrLocked {..})); - let bob_swap = test.recover_bob_from_db().await; + let bob_swap = ctx.recover_bob_from_db().await; assert!(matches!(bob_swap.state, BobState::XmrLocked {..})); let bob_state = bob::run(bob_swap).await.unwrap(); - test.assert_bob_redeemed(bob_state).await; + ctx.assert_bob_redeemed(bob_state).await; let alice_state = alice_swap_handle.await.unwrap(); - test.assert_alice_redeemed(alice_state.unwrap()).await; + ctx.assert_alice_redeemed(alice_state.unwrap()).await; }) .await; } diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index aa4524d4..9aea25bd 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -9,9 +9,9 @@ pub mod testutils; /// 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(|test| async move { - let alice_swap = test.new_swap_as_alice().await; - let bob_swap = test.new_swap_as_bob().await; + testutils::setup_test(|ctx| async move { + let alice_swap = ctx.new_swap_as_alice().await; + let bob_swap = ctx.new_swap_as_bob().await; let alice = alice::run(alice_swap); let alice_handle = tokio::spawn(alice); @@ -21,16 +21,16 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { assert!(matches!(bob_state, BobState::BtcLocked {..})); let alice_state = alice_handle.await.unwrap(); - test.assert_alice_punished(alice_state.unwrap()).await; + ctx.assert_alice_punished(alice_state.unwrap()).await; // Restart Bob after Alice punished to ensure Bob transitions to // punished and does not run indefinitely - let bob_swap = test.recover_bob_from_db().await; + let bob_swap = ctx.recover_bob_from_db().await; assert!(matches!(bob_swap.state, BobState::BtcLocked {..})); let bob_state = bob::run(bob_swap).await.unwrap(); - test.assert_bob_punished(bob_state).await; + ctx.assert_bob_punished(bob_state).await; }) .await; } diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index 48bc09e0..111d1cfa 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -6,9 +6,9 @@ pub mod testutils; /// then also refunds. #[tokio::test] async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { - testutils::setup_test(|test| async move { - let alice_swap = test.new_swap_as_alice().await; - let bob_swap = test.new_swap_as_bob().await; + testutils::setup_test(|ctx| async move { + let alice_swap = ctx.new_swap_as_alice().await; + let bob_swap = ctx.new_swap_as_bob().await; let bob = bob::run(bob_swap); let bob_handle = tokio::spawn(bob); @@ -22,7 +22,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { let bob_state = bob_handle.await.unwrap(); // Once bob has finished Alice is restarted and refunds as well - let alice_swap = test.recover_alice_from_db().await; + let alice_swap = ctx.recover_alice_from_db().await; assert!(matches!(alice_swap.state, AliceState::XmrLocked {..})); let alice_state = alice::run(alice_swap).await.unwrap(); @@ -31,8 +31,8 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { // refunded, not at the end because this can cause side-effects! // We have to properly wait for the refund tx's finality inside the assertion, // which requires storing the refund_tx_id in the the state! - test.assert_bob_refunded(bob_state.unwrap()).await; - test.assert_alice_refunded(alice_state).await; + ctx.assert_bob_refunded(bob_state.unwrap()).await; + ctx.assert_alice_refunded(alice_state).await; }) .await; } diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 24cb9eb5..7466faee 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -19,14 +19,14 @@ use tracing_core::dispatcher::DefaultGuard; use tracing_log::LogTracer; use uuid::Uuid; -pub struct Test { +pub struct TestContext { swap_amounts: SwapAmounts, alice_swap_factory: alice::SwapFactory, bob_swap_factory: bob::SwapFactory, } -impl Test { +impl TestContext { pub async fn new_swap_as_alice(&self) -> alice::Swap { let (swap, mut event_loop) = self .alice_swap_factory @@ -303,7 +303,7 @@ impl Test { pub async fn setup_test(testfn: T) where - T: Fn(Test) -> F, + T: Fn(TestContext) -> F, F: Future, { let cli = Cli::default(); @@ -377,7 +377,7 @@ where alice_swap_factory.peer_id(), ); - let test = Test { + let test = TestContext { swap_amounts, alice_swap_factory, bob_swap_factory,