Rename Test to TestContext and argument to ctx

This commit is contained in:
Daniel Karzel 2021-01-20 10:40:40 +11:00
parent e91987e23f
commit acfd43ee79
7 changed files with 39 additions and 39 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<T, F>(testfn: T)
where
T: Fn(Test) -> F,
T: Fn(TestContext) -> F,
F: Future<Output = ()>,
{
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,