WIP - Add highter order function for test init to solve lifetime issues of cli

This commit is contained in:
Daniel Karzel 2021-01-14 11:02:19 +11:00
parent 9a260df97d
commit 8708d27603
2 changed files with 29 additions and 15 deletions

View File

@ -8,14 +8,10 @@ pub mod testutils;
#[tokio::test] #[tokio::test]
async fn happy_path() { async fn happy_path() {
let mut test = Test::new( testutils::test(|alice, bob| async move {
bitcoin::Amount::from_sat(1_000_000), join!(alice.swap(), bob.swap());
monero::Amount::from_piconero(1_000_000_000_000),
)
.await;
join!(test.alice.swap(), test.bob.swap()); alice.assert_btc_redeemed();
bob.assert_btc_redeemed();
test.alice.assert_btc_redeemed(); }).await;
test.bob.assert_btc_redeemed();
} }

View File

@ -12,10 +12,28 @@ use monero_harness::{image, Monero};
use std::sync::Arc; use std::sync::Arc;
use swap::{bitcoin, config::Config, monero, seed::Seed}; use swap::{bitcoin, config::Config, monero, seed::Seed};
use std::future::Future;
use testcontainers::{clients::Cli, Container}; use testcontainers::{clients::Cli, Container};
use tracing_core::dispatcher::DefaultGuard; use tracing_core::dispatcher::DefaultGuard;
use tracing_log::LogTracer; use tracing_log::LogTracer;
pub async fn test<T, F>(testfn: T)
where
T: Fn(Alice, Bob) -> F,
F: Future<Output = ()>,
{
let cli = Cli::default();
let test = Test::new(
bitcoin::Amount::from_sat(1_000_000),
monero::Amount::from_piconero(1_000_000_000_000),
&cli,
)
.await;
testfn(test.alice, test.bob).await
}
pub struct Test<'a> { pub struct Test<'a> {
pub alice: Alice, pub alice: Alice,
pub bob: Bob, pub bob: Bob,
@ -23,10 +41,13 @@ pub struct Test<'a> {
} }
impl<'a> Test<'a> { impl<'a> Test<'a> {
pub async fn new(btc_to_swap: bitcoin::Amount, xmr_to_swap: monero::Amount) -> Test<'a> { pub async fn new(
btc_to_swap: bitcoin::Amount,
xmr_to_swap: monero::Amount,
cli: &'a Cli,
) -> Test<'a> {
let _guard = init_tracing(); let _guard = init_tracing();
let cli = Cli::default();
let (monero, containers) = testutils::init_containers(&cli).await; let (monero, containers) = testutils::init_containers(&cli).await;
let bob_btc_starting_balance = btc_to_swap * 10; let bob_btc_starting_balance = btc_to_swap * 10;
@ -84,10 +105,7 @@ pub async fn init_containers<'a>(cli: &'a Cli) -> (Monero, Containers<'a>) {
.await .await
.unwrap(); .unwrap();
(monero, Containers { (monero, Containers { bitcoind, monerods })
bitcoind,
monerods,
})
} }
pub async fn init_wallets( pub async fn init_wallets(