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;
use swap::{
config::GetConfig,
protocol::{alice, bob},
};
use swap::protocol::{alice, bob};
use testutils::SlowCancelConfig;
use tokio::join;
@ -11,7 +8,7 @@ use tokio::join;
#[tokio::test]
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 (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils;
use swap::{
config::GetConfig,
protocol::{alice, alice::AliceState, bob},
};
use swap::protocol::{alice, alice::AliceState, bob};
use testutils::{alice_run_until::is_encsig_learned, SlowCancelConfig};
#[tokio::test]
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 (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils;
use swap::{
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use swap::protocol::{alice, bob, bob::BobState};
use testutils::{bob_run_until::is_encsig_sent, SlowCancelConfig};
#[tokio::test]
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 (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils;
use swap::{
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use swap::protocol::{alice, bob, bob::BobState};
use testutils::{bob_run_until::is_lock_proof_received, SlowCancelConfig};
#[tokio::test]
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 (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,14 +1,11 @@
pub mod testutils;
use swap::{
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use swap::protocol::{alice, bob, bob::BobState};
use testutils::{bob_run_until::is_xmr_locked, SlowCancelConfig};
#[tokio::test]
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 (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,16 +1,13 @@
pub mod testutils;
use swap::{
config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use swap::protocol::{alice, bob, bob::BobState};
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
/// the encsig and fail to refund or redeem. Alice punishes.
#[tokio::test]
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 (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,16 +1,13 @@
pub mod testutils;
use swap::{
config::GetConfig,
protocol::{alice, alice::AliceState, bob},
};
use swap::protocol::{alice, alice::AliceState, bob};
use testutils::{alice_run_until::is_xmr_locked, FastCancelConfig};
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
/// then also refunds.
#[tokio::test]
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 (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -2,7 +2,6 @@ pub mod testutils;
use swap::{
config,
config::GetConfig,
protocol::{alice, alice::AliceState, bob},
};
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.
#[tokio::test]
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 (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
T: Fn(TestContext) -> F,
F: Future<Output = ()>,
C: GetConfig,
{
let cli = Cli::default();
let _guard = init_tracing();
let config = C::get_config();
let (monero, containers) = testutils::init_containers(&cli).await;
let swap_amounts = SwapAmounts {