From 9d266fc6483149f40bcdbccf6c0ab99c876132b5 Mon Sep 17 00:00:00 2001 From: rishflab Date: Tue, 15 Dec 2020 13:11:16 +1100 Subject: [PATCH] Add balance assertions to restart tests --- swap/tests/happy_path_restart_alice.rs | 25 ++++++++++++++++++++--- swap/tests/happy_path_restart_bob.rs | 28 ++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index 9e2e2f01..e6ff04b0 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -66,6 +66,11 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { ) .await; + // TODO: we are making a clone of Bob's wallets here to keep them in scope after + // Bob's wallets are moved into an async task. + let bob_btc_wallet_clone = bob_btc_wallet.clone(); + let bob_xmr_wallet_clone = bob_xmr_wallet.clone(); + let _ = tokio::spawn(async move { bob::swap::swap( bob_state, @@ -116,8 +121,8 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { let alice_state = alice::swap::resume_from_database( event_loop_handle_after_restart, - alice_btc_wallet, - alice_xmr_wallet, + alice_btc_wallet.clone(), + alice_xmr_wallet.clone(), config, alice_swap_id, alice_db, @@ -127,5 +132,19 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { assert!(matches!(alice_state, AliceState::BtcRedeemed {..})); - // TODO: Additionally assert balances + let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap(); + let btc_bob_final = bob_btc_wallet_clone.as_ref().balance().await.unwrap(); + + assert_eq!( + btc_alice_final, + btc_to_swap - bitcoin::Amount::from_sat(bitcoin::TX_FEE) + ); + assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap); + + let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap(); + bob_xmr_wallet_clone.as_ref().0.refresh().await.unwrap(); + let xmr_bob_final = bob_xmr_wallet_clone.as_ref().get_balance().await.unwrap(); + + assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap); + assert_eq!(xmr_bob_final, xmr_to_swap); } diff --git a/swap/tests/happy_path_restart_bob.rs b/swap/tests/happy_path_restart_bob.rs index 79c4bcec..80ea9723 100644 --- a/swap/tests/happy_path_restart_bob.rs +++ b/swap/tests/happy_path_restart_bob.rs @@ -67,11 +67,21 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { ) .await; + // TODO: we are making a clone of Alices's wallets here to keep them in scope + // after Alices's wallets are moved into an async task. + let alice_btc_wallet_clone = alice_btc_wallet.clone(); + let alice_xmr_wallet_clone = alice_xmr_wallet.clone(); + + // TODO: we are making a clone of Bob's wallets here to keep them in scope after + // Bob's wallets are moved into an async task. + let bob_btc_wallet_clone = bob_btc_wallet.clone(); + let bob_xmr_wallet_clone = bob_xmr_wallet.clone(); + let _ = tokio::spawn(async move { alice::swap::swap( alice_state, alice_event_loop_handle, - alice_btc_wallet, + alice_btc_wallet.clone(), alice_xmr_wallet.clone(), config, Uuid::new_v4(), @@ -127,5 +137,19 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { assert!(matches!(alice_state, BobState::XmrRedeemed {..})); - // TODO: Additionally assert balances + let btc_alice_final = alice_btc_wallet_clone.as_ref().balance().await.unwrap(); + let btc_bob_final = bob_btc_wallet_clone.as_ref().balance().await.unwrap(); + + assert_eq!( + btc_alice_final, + btc_to_swap - bitcoin::Amount::from_sat(bitcoin::TX_FEE) + ); + assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap); + + let xmr_alice_final = alice_xmr_wallet_clone.as_ref().get_balance().await.unwrap(); + bob_xmr_wallet_clone.as_ref().0.refresh().await.unwrap(); + let xmr_bob_final = bob_xmr_wallet_clone.as_ref().get_balance().await.unwrap(); + + assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap); + assert_eq!(xmr_bob_final, xmr_to_swap); }