Use builder in tests

This commit is contained in:
Franck Royer 2021-01-20 21:42:35 +11:00
parent c44b867d01
commit 94045b9a69
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -2,9 +2,9 @@ use crate::testutils;
use bitcoin_harness::Bitcoind; use bitcoin_harness::Bitcoind;
use futures::Future; use futures::Future;
use get_port::get_port; use get_port::get_port;
use libp2p::core::Multiaddr; use libp2p::{core::Multiaddr, PeerId};
use monero_harness::{image, Monero}; use monero_harness::{image, Monero};
use std::sync::Arc; use std::{path::PathBuf, sync::Arc};
use swap::{ use swap::{
bitcoin, bitcoin,
config::Config, config::Config,
@ -25,15 +25,68 @@ pub struct StartingBalances {
pub btc: bitcoin::Amount, pub btc: bitcoin::Amount,
} }
struct AliceParams {
seed: Seed,
config: Config,
swap_id: Uuid,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
db_path: PathBuf,
listen_address: Multiaddr,
}
impl AliceParams {
pub async fn builder(&self) -> alice::Builder {
alice::Builder::new(
self.seed,
self.config,
self.swap_id,
self.bitcoin_wallet.clone(),
self.monero_wallet.clone(),
self.db_path.clone(),
self.listen_address.clone(),
)
.await
}
async fn peer_id(&self) -> PeerId {
self.builder().await.peer_id()
}
}
struct BobParams {
seed: Seed,
db_path: PathBuf,
swap_id: Uuid,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
alice_address: Multiaddr,
alice_peer_id: PeerId,
}
impl BobParams {
pub fn builder(&self) -> bob::Builder {
bob::Builder::new(
self.seed,
self.db_path.clone(),
self.swap_id,
self.bitcoin_wallet.clone(),
self.monero_wallet.clone(),
self.alice_address.clone(),
self.alice_peer_id.clone(),
)
}
}
pub struct TestContext { pub struct TestContext {
swap_amounts: SwapAmounts, swap_amounts: SwapAmounts,
alice_swap_factory: Option<alice::Builder>, alice_params: AliceParams,
alice_starting_balances: StartingBalances, alice_starting_balances: StartingBalances,
alice_bitcoin_wallet: Arc<bitcoin::Wallet>, alice_bitcoin_wallet: Arc<bitcoin::Wallet>,
alice_monero_wallet: Arc<monero::Wallet>, alice_monero_wallet: Arc<monero::Wallet>,
bob_swap_factory: Option<bob::Builder>, bob_params: BobParams,
bob_starting_balances: StartingBalances, bob_starting_balances: StartingBalances,
bob_bitcoin_wallet: Arc<bitcoin::Wallet>, bob_bitcoin_wallet: Arc<bitcoin::Wallet>,
bob_monero_wallet: Arc<monero::Wallet>, bob_monero_wallet: Arc<monero::Wallet>,
@ -42,9 +95,9 @@ pub struct TestContext {
impl TestContext { impl TestContext {
pub async fn new_swap_as_alice(&mut self) -> alice::Swap { pub async fn new_swap_as_alice(&mut self) -> alice::Swap {
let (swap, mut event_loop) = self let (swap, mut event_loop) = self
.alice_swap_factory .alice_params
.take() .builder()
.unwrap() .await
.with_init_params(self.swap_amounts) .with_init_params(self.swap_amounts)
.build() .build()
.await .await
@ -57,9 +110,8 @@ impl TestContext {
pub async fn new_swap_as_bob(&mut self) -> bob::Swap { pub async fn new_swap_as_bob(&mut self) -> bob::Swap {
let (swap, event_loop) = self let (swap, event_loop) = self
.bob_swap_factory .bob_params
.take() .builder()
.unwrap()
.with_init_params(self.swap_amounts, Config::regtest()) .with_init_params(self.swap_amounts, Config::regtest())
.build() .build()
.await .await
@ -71,13 +123,7 @@ impl TestContext {
} }
pub async fn recover_alice_from_db(&mut self) -> alice::Swap { pub async fn recover_alice_from_db(&mut self) -> alice::Swap {
let (swap, mut event_loop) = self let (swap, mut event_loop) = self.alice_params.builder().await.build().await.unwrap();
.alice_swap_factory
.take()
.unwrap()
.build()
.await
.unwrap();
tokio::spawn(async move { event_loop.run().await }); tokio::spawn(async move { event_loop.run().await });
@ -85,7 +131,7 @@ impl TestContext {
} }
pub async fn recover_bob_from_db(&mut self) -> bob::Swap { pub async fn recover_bob_from_db(&mut self) -> bob::Swap {
let (swap, event_loop) = self.bob_swap_factory.take().unwrap().build().await.unwrap(); let (swap, event_loop) = self.bob_params.builder().build().await.unwrap();
tokio::spawn(async move { event_loop.run().await }); tokio::spawn(async move { event_loop.run().await });
@ -279,16 +325,15 @@ where
) )
.await; .await;
let alice_swap_factory = alice::Builder::new( let alice_params = AliceParams {
Seed::random().unwrap(), seed: Seed::random().unwrap(),
config, config,
Uuid::new_v4(), swap_id: Uuid::new_v4(),
alice_bitcoin_wallet.clone(), bitcoin_wallet: alice_bitcoin_wallet.clone(),
alice_monero_wallet.clone(), monero_wallet: alice_monero_wallet.clone(),
tempdir().unwrap().path().to_path_buf(), db_path: tempdir().unwrap().path().to_path_buf(),
listen_address, listen_address,
) };
.await;
let bob_starting_balances = StartingBalances { let bob_starting_balances = StartingBalances {
xmr: monero::Amount::ZERO, xmr: monero::Amount::ZERO,
@ -304,23 +349,23 @@ where
) )
.await; .await;
let bob_swap_factory = bob::Builder::new( let bob_params = BobParams {
Seed::random().unwrap(), seed: Seed::random().unwrap(),
tempdir().unwrap().path().to_path_buf(), db_path: tempdir().unwrap().path().to_path_buf(),
Uuid::new_v4(), swap_id: Uuid::new_v4(),
bob_bitcoin_wallet.clone(), bitcoin_wallet: bob_bitcoin_wallet.clone(),
bob_monero_wallet.clone(), monero_wallet: bob_monero_wallet.clone(),
alice_swap_factory.listen_address(), alice_address: alice_params.listen_address.clone(),
alice_swap_factory.peer_id(), alice_peer_id: alice_params.peer_id().await,
); };
let test = TestContext { let test = TestContext {
swap_amounts, swap_amounts,
alice_swap_factory: Some(alice_swap_factory), alice_params,
alice_starting_balances, alice_starting_balances,
alice_bitcoin_wallet, alice_bitcoin_wallet,
alice_monero_wallet, alice_monero_wallet,
bob_swap_factory: Some(bob_swap_factory), bob_params,
bob_starting_balances, bob_starting_balances,
bob_bitcoin_wallet, bob_bitcoin_wallet,
bob_monero_wallet, bob_monero_wallet,