Browse Source

Merge #694

694: Clean up the test harness before allowing config tailoring r=klochowicz a=klochowicz

Some of the tests will use the same parameters as production code to reproduce
and fix issues such as heartbeat timeout during contract setup.

Co-authored-by: Mariusz Klochowicz <mariusz@klochowicz.com>
debug-collab-settlement
bors[bot] 3 years ago
committed by GitHub
parent
commit
a7917f1e86
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      daemon/tests/happy_path.rs
  2. 75
      daemon/tests/harness/mod.rs

20
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::flow::{is_next_none, next, next_cfd, next_order, next_some};
use crate::harness::{ use crate::harness::{
assert_is_same_order, dummy_new_order, init_tracing, oracle_pk, start_both, Maker, Taker, assert_is_same_order, dummy_new_order, init_tracing, start_both, Maker, MakerConfig, Taker,
HEARTBEAT_INTERVAL_FOR_TEST, TakerConfig,
}; };
use daemon::connection::ConnectionStatus; use daemon::connection::ConnectionStatus;
use daemon::model::cfd::CfdState; 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() { async fn taker_notices_lack_of_maker() {
let _guard = init_tracing(); let _guard = init_tracing();
let seed = daemon::seed::Seed::default();
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let local_addr = listener.local_addr().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!( assert_eq!(
ConnectionStatus::Online, ConnectionStatus::Online,
@ -136,7 +138,7 @@ async fn taker_notices_lack_of_maker() {
std::mem::drop(maker); std::mem::drop(maker);
sleep(HEARTBEAT_INTERVAL_FOR_TEST * 3).await; sleep(taker_config.heartbeat_timeout).await;
assert_eq!( assert_eq!(
ConnectionStatus::Offline, ConnectionStatus::Offline,
@ -145,9 +147,9 @@ async fn taker_notices_lack_of_maker() {
let listener = tokio::net::TcpListener::bind(local_addr).await.unwrap(); 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!( assert_eq!(
ConnectionStatus::Online, ConnectionStatus::Online,

75
daemon/tests/harness/mod.rs

@ -33,7 +33,7 @@ pub mod mocks;
pub const HEARTBEAT_INTERVAL_FOR_TEST: Duration = Duration::from_secs(2); pub const HEARTBEAT_INTERVAL_FOR_TEST: Duration = Duration::from_secs(2);
const N_PAYOUTS_FOR_TEST: usize = 5; const N_PAYOUTS_FOR_TEST: usize = 5;
pub fn oracle_pk() -> schnorrsig::PublicKey { fn oracle_pk() -> schnorrsig::PublicKey {
schnorrsig::PublicKey::from_str( schnorrsig::PublicKey::from_str(
"ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7", "ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7",
) )
@ -41,13 +41,54 @@ pub fn oracle_pk() -> schnorrsig::PublicKey {
} }
pub async fn start_both() -> (Maker, Taker) { 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_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let maker = Maker::start(oracle_pk(), maker_seed, maker_listener).await; let maker = Maker::start(&MakerConfig::default(), maker_listener).await;
let taker = Taker::start(oracle_pk(), maker.listen_addr, maker.identity_pk).await; let taker = Taker::start(
&TakerConfig::default(),
maker.listen_addr,
maker.identity_pk,
)
.await;
(maker, taker) (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 /// Maker Test Setup
pub struct Maker { pub struct Maker {
pub system: pub system:
@ -74,11 +115,7 @@ impl Maker {
&mut self.connected_takers_feed_receiver &mut self.connected_takers_feed_receiver
} }
pub async fn start( pub async fn start(config: &MakerConfig, listener: TcpListener) -> Self {
oracle_pk: schnorrsig::PublicKey,
seed: Seed,
listener: TcpListener,
) -> Self {
let db = in_memory_db().await; let db = in_memory_db().await;
let mut mocks = mocks::Mocks::default(); let mut mocks = mocks::Mocks::default();
@ -91,7 +128,7 @@ impl Maker {
let settlement_interval = time::Duration::hours(24); 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); let (projection_actor, projection_context) = xtra::Context::new(None);
@ -100,7 +137,7 @@ impl Maker {
let maker = daemon::MakerActorSystem::new( let maker = daemon::MakerActorSystem::new(
db, db,
wallet_addr, wallet_addr,
oracle_pk, config.oracle_pk,
|_, _| oracle, |_, _| oracle,
|_, _| async { Ok(monitor) }, |_, _| async { Ok(monitor) },
|channel0, channel1, channel2| { |channel0, channel1, channel2| {
@ -109,11 +146,11 @@ impl Maker {
channel1, channel1,
channel2, channel2,
identity_sk, identity_sk,
HEARTBEAT_INTERVAL_FOR_TEST, config.heartbeat_interval,
) )
}, },
settlement_interval, settlement_interval,
N_PAYOUTS_FOR_TEST, config.n_payouts,
projection_actor.clone(), projection_actor.clone(),
) )
.await .await
@ -222,13 +259,11 @@ impl Taker {
} }
pub async fn start( pub async fn start(
oracle_pk: schnorrsig::PublicKey, config: &TakerConfig,
maker_address: SocketAddr, maker_address: SocketAddr,
maker_noise_pub_key: x25519_dalek::PublicKey, maker_noise_pub_key: x25519_dalek::PublicKey,
) -> Self { ) -> Self {
let seed = Seed::default(); let (identity_pk, identity_sk) = config.seed.derive_identity();
let (identity_pk, identity_sk) = seed.derive_identity();
let db = in_memory_db().await; let db = in_memory_db().await;
@ -247,12 +282,12 @@ impl Taker {
let taker = daemon::TakerActorSystem::new( let taker = daemon::TakerActorSystem::new(
db, db,
wallet_addr, wallet_addr,
oracle_pk, config.oracle_pk,
identity_sk, identity_sk,
|_, _| oracle, |_, _| oracle,
|_, _| async { Ok(monitor) }, |_, _| async { Ok(monitor) },
N_PAYOUTS_FOR_TEST, config.n_payouts,
HEARTBEAT_INTERVAL_FOR_TEST * 2, config.heartbeat_timeout,
projection_actor, projection_actor,
) )
.await .await

Loading…
Cancel
Save