From dc96b3d1e9531ee69bc8dc6f61c5cfd0e51985b7 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 18 Dec 2020 12:15:53 +1100 Subject: [PATCH] Stop running first event loop after Alice restarts --- swap/tests/refund_restart_alice.rs | 33 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index a397c37b..840bd473 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -7,6 +7,7 @@ use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Da use tempfile::tempdir; use testcontainers::clients::Cli; use testutils::init_tracing; +use tokio::select; use uuid::Uuid; use xmr_btc::{bitcoin, config::Config}; @@ -43,8 +44,8 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { let ( alice_state, - mut alice_event_loop, - alice_event_loop_handle, + mut alice_event_loop_1, + alice_event_loop_handle_1, alice_btc_wallet, alice_xmr_wallet, _, @@ -81,8 +82,6 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { Uuid::new_v4(), ); - tokio::spawn(async move { bob_event_loop.run().await }); - let alice_swap_id = Uuid::new_v4(); let alice_db_datadir = tempdir().unwrap(); let alice_db = Database::open(alice_db_datadir.path()).unwrap(); @@ -90,7 +89,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { let alice_xmr_locked_fut = alice::swap::run_until( alice_state, alice::swap::is_xmr_locked, - alice_event_loop_handle, + alice_event_loop_handle_1, alice_btc_wallet.clone(), alice_xmr_wallet.clone(), Config::regtest(), @@ -98,10 +97,14 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { alice_db, ); - tokio::spawn(async move { alice_event_loop.run().await }); + tokio::spawn(async move { bob_event_loop.run().await }); - // Wait until alice has locked xmr and bob has locked btc - let (bob_state, alice_state) = try_join(bob_fut, alice_xmr_locked_fut).await.unwrap(); + // We are selecting with alice_event_loop_1 so that we stop polling on it once + // the try_join is finished. + let (bob_state, alice_restart_state) = select! { + res = try_join(bob_fut, alice_xmr_locked_fut) => res.unwrap(), + _ = alice_event_loop_1.run() => panic!("The event loop should never finish") + }; let bob_state4 = if let BobState::BtcRefunded(state4) = bob_state { state4 @@ -110,12 +113,12 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { }; let alice_db = Database::open(alice_db_datadir.path()).unwrap(); - let (mut alice_event_loop, alice_event_loop_handle) = + let (mut alice_event_loop_2, alice_event_loop_handle_2) = testutils::init_alice_event_loop(alice_multiaddr); - let alice_state = alice::swap::swap( - alice_state, - alice_event_loop_handle, + let alice_final_state = alice::swap::swap( + alice_restart_state, + alice_event_loop_handle_2, alice_btc_wallet.clone(), alice_xmr_wallet.clone(), Config::regtest(), @@ -124,15 +127,13 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { ) .await .unwrap(); - tokio::spawn(async move { alice_event_loop.run().await }); + tokio::spawn(async move { alice_event_loop_2.run().await }); - assert!(matches!(alice_state, AliceState::XmrRefunded)); + assert!(matches!(alice_final_state, AliceState::XmrRefunded)); let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap(); let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap(); - // lock_tx_bitcoin_fee is determined by the wallet, it is not necessarily equal - // to TX_FEE let lock_tx_bitcoin_fee = bob_btc_wallet .transaction_fee(bob_state4.tx_lock_id()) .await