Use trait instead of passing struct

This commit is contained in:
Franck Royer 2021-01-29 13:52:05 +11:00
parent 80810e3605
commit 108cc1e6cc
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
9 changed files with 19 additions and 38 deletions

View File

@ -1,9 +1,6 @@
pub mod testutils; pub mod testutils;
use swap::{ use swap::protocol::{alice, bob};
config::GetConfig,
protocol::{alice, bob},
};
use testutils::SlowCancelConfig; use testutils::SlowCancelConfig;
use tokio::join; use tokio::join;
@ -11,7 +8,7 @@ use tokio::join;
#[tokio::test] #[tokio::test]
async fn happy_path() { async fn happy_path() {
testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
let (alice_swap, _) = ctx.new_swap_as_alice().await; let (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().await; let (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils; pub mod testutils;
use swap::{ use swap::protocol::{alice, alice::AliceState, bob};
config::GetConfig,
protocol::{alice, alice::AliceState, bob},
};
use testutils::{alice_run_until::is_encsig_learned, SlowCancelConfig}; use testutils::{alice_run_until::is_encsig_learned, SlowCancelConfig};
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().await; let (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils; pub mod testutils;
use swap::{ use swap::protocol::{alice, bob, bob::BobState};
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use testutils::{bob_run_until::is_encsig_sent, SlowCancelConfig}; use testutils::{bob_run_until::is_encsig_sent, SlowCancelConfig};
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
let (alice_swap, _) = ctx.new_swap_as_alice().await; let (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils; pub mod testutils;
use swap::{ use swap::protocol::{alice, bob, bob::BobState};
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use testutils::{bob_run_until::is_lock_proof_received, SlowCancelConfig}; use testutils::{bob_run_until::is_lock_proof_received, SlowCancelConfig};
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_lock_proof_received_resume_swap() { async fn given_bob_restarts_after_lock_proof_received_resume_swap() {
testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
let (alice_swap, _) = ctx.new_swap_as_alice().await; let (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils; pub mod testutils;
use swap::{ use swap::protocol::{alice, bob, bob::BobState};
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use testutils::{bob_run_until::is_xmr_locked, SlowCancelConfig}; use testutils::{bob_run_until::is_xmr_locked, SlowCancelConfig};
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {
testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
let (alice_swap, _) = ctx.new_swap_as_alice().await; let (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,16 +1,13 @@
pub mod testutils; pub mod testutils;
use swap::{ use swap::protocol::{alice, bob, bob::BobState};
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use testutils::{bob_run_until::is_btc_locked, FastPunishConfig}; use testutils::{bob_run_until::is_btc_locked, FastPunishConfig};
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice /// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
/// the encsig and fail to refund or redeem. Alice punishes. /// the encsig and fail to refund or redeem. Alice punishes.
#[tokio::test] #[tokio::test]
async fn alice_punishes_if_bob_never_acts_after_fund() { async fn alice_punishes_if_bob_never_acts_after_fund() {
testutils::setup_test(FastPunishConfig::get_config(), |mut ctx| async move { testutils::setup_test(FastPunishConfig, |mut ctx| async move {
let (alice_swap, _) = ctx.new_swap_as_alice().await; let (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,16 +1,13 @@
pub mod testutils; pub mod testutils;
use swap::{ use swap::protocol::{alice, alice::AliceState, bob};
config::GetConfig,
protocol::{alice, alice::AliceState, bob},
};
use testutils::{alice_run_until::is_xmr_locked, FastCancelConfig}; use testutils::{alice_run_until::is_xmr_locked, FastCancelConfig};
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
/// then also refunds. /// then also refunds.
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { async fn given_alice_restarts_after_xmr_is_locked_refund_swap() {
testutils::setup_test(FastCancelConfig::get_config(), |mut ctx| async move { testutils::setup_test(FastCancelConfig, |mut ctx| async move {
let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().await; let (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -2,7 +2,6 @@ pub mod testutils;
use swap::{ use swap::{
config, config,
config::GetConfig,
protocol::{alice, alice::AliceState, bob}, protocol::{alice, alice::AliceState, bob},
}; };
use testutils::alice_run_until::is_encsig_learned; use testutils::alice_run_until::is_encsig_learned;
@ -12,7 +11,7 @@ use testutils::alice_run_until::is_encsig_learned;
/// redeem had the timelock not expired. /// redeem had the timelock not expired.
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_refund_swap() { async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_refund_swap() {
testutils::setup_test(config::Regtest::get_config(), |mut ctx| async move { testutils::setup_test(config::Regtest, |mut ctx| async move {
let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().await; let (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -304,15 +304,18 @@ impl TestContext {
} }
} }
pub async fn setup_test<T, F>(config: Config, testfn: T) pub async fn setup_test<T, F, C>(_config: C, testfn: T)
where where
T: Fn(TestContext) -> F, T: Fn(TestContext) -> F,
F: Future<Output = ()>, F: Future<Output = ()>,
C: GetConfig,
{ {
let cli = Cli::default(); let cli = Cli::default();
let _guard = init_tracing(); let _guard = init_tracing();
let config = C::get_config();
let (monero, containers) = testutils::init_containers(&cli).await; let (monero, containers) = testutils::init_containers(&cli).await;
let swap_amounts = SwapAmounts { let swap_amounts = SwapAmounts {