From a8d1647d69c062a7ceb1a32d43ddd9d7d9cfee64 Mon Sep 17 00:00:00 2001 From: Mariusz Klochowicz Date: Wed, 24 Nov 2021 15:16:58 +1030 Subject: [PATCH] Clean up the test harness before allowing config tailoring Some of the tests will use the same parameters as production code to reproduce and fix issues such as heartbeat timeout during contract setup. --- daemon/tests/happy_path.rs | 20 +++++----- daemon/tests/harness/mod.rs | 75 +++++++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/daemon/tests/happy_path.rs b/daemon/tests/happy_path.rs index a3bca96..edf8bdc 100644 --- a/daemon/tests/happy_path.rs +++ b/daemon/tests/happy_path.rs @@ -1,7 +1,7 @@ use crate::harness::flow::{is_next_none, next, next_cfd, next_order, next_some}; use crate::harness::{ - assert_is_same_order, dummy_new_order, init_tracing, oracle_pk, start_both, Maker, Taker, - HEARTBEAT_INTERVAL_FOR_TEST, + assert_is_same_order, dummy_new_order, init_tracing, start_both, Maker, MakerConfig, Taker, + TakerConfig, }; use daemon::connection::ConnectionStatus; use daemon::model::cfd::CfdState; @@ -119,15 +119,17 @@ async fn taker_takes_order_and_maker_accepts_and_contract_setup() { async fn taker_notices_lack_of_maker() { let _guard = init_tracing(); - let seed = daemon::seed::Seed::default(); - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let local_addr = listener.local_addr().unwrap(); - let maker = Maker::start(oracle_pk(), seed, listener).await; + let maker_config = MakerConfig::default(); + + let maker = Maker::start(&maker_config, listener).await; + + let taker_config = TakerConfig::default(); - let mut taker = Taker::start(oracle_pk(), maker.listen_addr, maker.identity_pk).await; + let mut taker = Taker::start(&taker_config, maker.listen_addr, maker.identity_pk).await; assert_eq!( ConnectionStatus::Online, @@ -136,7 +138,7 @@ async fn taker_notices_lack_of_maker() { std::mem::drop(maker); - sleep(HEARTBEAT_INTERVAL_FOR_TEST * 3).await; + sleep(taker_config.heartbeat_timeout).await; assert_eq!( ConnectionStatus::Offline, @@ -145,9 +147,9 @@ async fn taker_notices_lack_of_maker() { let listener = tokio::net::TcpListener::bind(local_addr).await.unwrap(); - let _maker = Maker::start(oracle_pk(), seed, listener).await; + let _maker = Maker::start(&maker_config, listener).await; - sleep(HEARTBEAT_INTERVAL_FOR_TEST * 3).await; + sleep(taker_config.heartbeat_timeout).await; assert_eq!( ConnectionStatus::Online, diff --git a/daemon/tests/harness/mod.rs b/daemon/tests/harness/mod.rs index 857bc82..2931179 100644 --- a/daemon/tests/harness/mod.rs +++ b/daemon/tests/harness/mod.rs @@ -34,7 +34,7 @@ pub mod mocks; pub const HEARTBEAT_INTERVAL_FOR_TEST: Duration = Duration::from_secs(2); const N_PAYOUTS_FOR_TEST: usize = 5; -pub fn oracle_pk() -> schnorrsig::PublicKey { +fn oracle_pk() -> schnorrsig::PublicKey { schnorrsig::PublicKey::from_str( "ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7", ) @@ -42,13 +42,54 @@ pub fn oracle_pk() -> schnorrsig::PublicKey { } pub async fn start_both() -> (Maker, Taker) { - let maker_seed = Seed::default(); let maker_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let maker = Maker::start(oracle_pk(), maker_seed, maker_listener).await; - let taker = Taker::start(oracle_pk(), maker.listen_addr, maker.identity_pk).await; + let maker = Maker::start(&MakerConfig::default(), maker_listener).await; + let taker = Taker::start( + &TakerConfig::default(), + maker.listen_addr, + maker.identity_pk, + ) + .await; (maker, taker) } +pub struct MakerConfig { + oracle_pk: schnorrsig::PublicKey, + seed: Seed, + pub heartbeat_interval: Duration, + n_payouts: usize, +} + +impl Default for MakerConfig { + fn default() -> Self { + Self { + oracle_pk: oracle_pk(), + seed: Seed::default(), + heartbeat_interval: HEARTBEAT_INTERVAL_FOR_TEST, + n_payouts: N_PAYOUTS_FOR_TEST, + } + } +} + +#[derive(Clone)] +pub struct TakerConfig { + oracle_pk: schnorrsig::PublicKey, + seed: Seed, + pub heartbeat_timeout: Duration, + n_payouts: usize, +} + +impl Default for TakerConfig { + fn default() -> Self { + Self { + oracle_pk: oracle_pk(), + seed: Seed::default(), + heartbeat_timeout: HEARTBEAT_INTERVAL_FOR_TEST * 2, + n_payouts: N_PAYOUTS_FOR_TEST, + } + } +} + /// Maker Test Setup pub struct Maker { pub system: @@ -75,11 +116,7 @@ impl Maker { &mut self.connected_takers_feed_receiver } - pub async fn start( - oracle_pk: schnorrsig::PublicKey, - seed: Seed, - listener: TcpListener, - ) -> Self { + pub async fn start(config: &MakerConfig, listener: TcpListener) -> Self { let db = in_memory_db().await; let mut mocks = mocks::Mocks::default(); @@ -92,7 +129,7 @@ impl Maker { let settlement_interval = time::Duration::hours(24); - let (identity_pk, identity_sk) = seed.derive_identity(); + let (identity_pk, identity_sk) = config.seed.derive_identity(); let (projection_actor, projection_context) = xtra::Context::new(None); @@ -101,7 +138,7 @@ impl Maker { let maker = daemon::MakerActorSystem::new( db, wallet_addr, - oracle_pk, + config.oracle_pk, |_, _| oracle, |_, _| async { Ok(monitor) }, |channel0, channel1, channel2| { @@ -110,11 +147,11 @@ impl Maker { channel1, channel2, identity_sk, - HEARTBEAT_INTERVAL_FOR_TEST, + config.heartbeat_interval, ) }, settlement_interval, - N_PAYOUTS_FOR_TEST, + config.n_payouts, projection_actor.clone(), ) .await @@ -223,13 +260,11 @@ impl Taker { } pub async fn start( - oracle_pk: schnorrsig::PublicKey, + config: &TakerConfig, maker_address: SocketAddr, maker_noise_pub_key: x25519_dalek::PublicKey, ) -> Self { - let seed = Seed::default(); - - let (identity_pk, identity_sk) = seed.derive_identity(); + let (identity_pk, identity_sk) = config.seed.derive_identity(); let db = in_memory_db().await; @@ -248,12 +283,12 @@ impl Taker { let taker = daemon::TakerActorSystem::new( db, wallet_addr, - oracle_pk, + config.oracle_pk, identity_sk, |_, _| oracle, |_, _| async { Ok(monitor) }, - N_PAYOUTS_FOR_TEST, - HEARTBEAT_INTERVAL_FOR_TEST * 2, + config.n_payouts, + config.heartbeat_timeout, projection_actor, ) .await