Join all futures to avoid hanging tests

This commit is contained in:
Franck Royer 2020-12-21 11:21:53 +11:00
parent 2abf65f3b6
commit e366d3a73b
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 37 additions and 24 deletions

View File

@ -1,5 +1,8 @@
use crate::testutils::{init_alice, init_bob}; use crate::testutils::{init_alice, init_bob};
use futures::future::try_join; use futures::{
future::{join, select},
FutureExt,
};
use get_port::get_port; use get_port::get_port;
use libp2p::Multiaddr; use libp2p::Multiaddr;
use rand::rngs::OsRng; use rand::rngs::OsRng;
@ -82,11 +85,11 @@ async fn happy_path() {
config, config,
Uuid::new_v4(), Uuid::new_v4(),
alice_db, alice_db,
); )
.boxed();
let alice_peer_id = alice_event_loop.peer_id(); let alice_peer_id = alice_event_loop.peer_id();
let alice_fut = select(alice_swap_fut, alice_event_loop.run().boxed());
let _alice_swarm_fut = tokio::spawn(async move { alice_event_loop.run().await });
let bob_swap_fut = bob::swap::swap( let bob_swap_fut = bob::swap::swap(
bob_state, bob_state,
@ -98,11 +101,12 @@ async fn happy_path() {
Uuid::new_v4(), Uuid::new_v4(),
alice_peer_id, alice_peer_id,
alice_multiaddr, alice_multiaddr,
); )
.boxed();
let _bob_swarm_fut = tokio::spawn(async move { bob_event_loop.run().await }); let bob_fut = select(bob_swap_fut, bob_event_loop.run().boxed());
try_join(alice_swap_fut, bob_swap_fut).await.unwrap(); join(alice_fut, bob_fut).await;
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap(); let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap(); let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap();

View File

@ -76,7 +76,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
let bob_btc_wallet_clone = bob_btc_wallet.clone(); let bob_btc_wallet_clone = bob_btc_wallet.clone();
let bob_xmr_wallet_clone = bob_xmr_wallet.clone(); let bob_xmr_wallet_clone = bob_xmr_wallet.clone();
let _ = tokio::spawn(bob::swap::swap( let bob_fut = bob::swap::swap(
bob_state, bob_state,
bob_event_loop_handle, bob_event_loop_handle,
bob_db, bob_db,
@ -86,14 +86,14 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
Uuid::new_v4(), Uuid::new_v4(),
alice_peer_id, alice_peer_id,
alice_multiaddr.clone(), alice_multiaddr.clone(),
)); );
let _bob_swarm_fut = tokio::spawn(async move { bob_event_loop.run().await });
let alice_db_datadir = tempdir().unwrap(); let alice_db_datadir = tempdir().unwrap();
let alice_db = Database::open(alice_db_datadir.path()).unwrap(); let alice_db = Database::open(alice_db_datadir.path()).unwrap();
let _alice_swarm_fut = tokio::spawn(async move { alice_event_loop.run().await }); tokio::spawn(async move { alice_event_loop.run().await });
tokio::spawn(bob_fut);
tokio::spawn(bob_event_loop.run());
let alice_swap_id = Uuid::new_v4(); let alice_swap_id = Uuid::new_v4();

View File

@ -261,12 +261,6 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
_ = bob_event_loop_1.run() => panic!("The event loop should never finish") _ = bob_event_loop_1.run() => panic!("The event loop should never finish")
}; };
// let tx_lock_id = if let AliceState::BtcRefunded { state3, .. } = alice_state
// { state3.tx_lock.txid()
// } else {
// panic!(format!("Alice in unexpected state: {}", alice_state));
// };
let (bob_event_loop_2, bob_event_loop_handle_2) = testutils::init_bob_event_loop(); let (bob_event_loop_2, bob_event_loop_handle_2) = testutils::init_bob_event_loop();
let bob_fut = bob::swap::swap( let bob_fut = bob::swap::swap(

View File

@ -1,5 +1,8 @@
use crate::testutils::{init_alice, init_bob}; use crate::testutils::{init_alice, init_bob};
use futures::future::try_join; use futures::{
future::{join, select, Either},
FutureExt,
};
use get_port::get_port; use get_port::get_port;
use libp2p::Multiaddr; use libp2p::Multiaddr;
use rand::rngs::OsRng; use rand::rngs::OsRng;
@ -81,9 +84,10 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
bob_xmr_wallet.clone(), bob_xmr_wallet.clone(),
OsRng, OsRng,
Uuid::new_v4(), Uuid::new_v4(),
); )
.boxed();
let _bob_swarm_fut = tokio::spawn(async move { bob_event_loop.run().await }); let bob_fut = select(bob_btc_locked_fut, bob_event_loop.run().boxed());
let alice_fut = alice::swap::swap( let alice_fut = alice::swap::swap(
alice_state, alice_state,
@ -93,12 +97,23 @@ async fn alice_punishes_if_bob_never_acts_after_fund() {
Config::regtest(), Config::regtest(),
Uuid::new_v4(), Uuid::new_v4(),
alice_db, alice_db,
); )
.boxed();
let _alice_swarm_fut = tokio::spawn(async move { alice_event_loop.run().await }); let alice_fut = select(alice_fut, alice_event_loop.run().boxed());
// Wait until alice has locked xmr and bob has locked btc // Wait until alice has locked xmr and bob has locked btc
let (alice_state, bob_state) = try_join(alice_fut, bob_btc_locked_fut).await.unwrap(); let (alice_state, bob_state) = join(alice_fut, bob_fut).await;
let alice_state = match alice_state {
Either::Left((state, _)) => state.unwrap(),
Either::Right(_) => panic!("Alice event loop should not terminate."),
};
let bob_state = match bob_state {
Either::Left((state, _)) => state.unwrap(),
Either::Right(_) => panic!("Bob event loop should not terminate."),
};
assert!(matches!(alice_state, AliceState::Punished)); assert!(matches!(alice_state, AliceState::Punished));
let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state { let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state {