Browse Source

Clean up tests

- Remove dummy transaction and txid (use the transactions received in the
  messages instead
- extract helpers for quote and price to prevent misalignments in tests
- extend contract setup to finish at Open state of the cfd
feature/reconnect-button
Mariusz Klochowicz 3 years ago
parent
commit
0a8969ed63
No known key found for this signature in database GPG Key ID: 470C865699C8D4D
  1. 9
      daemon/tests/happy_path.rs
  2. 29
      daemon/tests/harness/bdk.rs
  3. 11
      daemon/tests/harness/mocks/mod.rs
  4. 49
      daemon/tests/harness/mod.rs

9
daemon/tests/happy_path.rs

@ -2,7 +2,8 @@ use std::time::Duration;
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::{
dummy_new_order, init_tracing, start_both, Maker, MakerConfig, Taker, TakerConfig, deliver_lock_finality_event, dummy_new_order, init_tracing, start_both, Maker, MakerConfig,
Taker, TakerConfig,
}; };
use daemon::connection::ConnectionStatus; use daemon::connection::ConnectionStatus;
use daemon::model::Usd; use daemon::model::Usd;
@ -116,6 +117,12 @@ async fn taker_takes_order_and_maker_accepts_and_contract_setup() {
assert_eq!(maker_cfd.order_id, received.id); assert_eq!(maker_cfd.order_id, received.id);
assert!(matches!(taker_cfd.state, CfdState::PendingOpen { .. })); assert!(matches!(taker_cfd.state, CfdState::PendingOpen { .. }));
assert!(matches!(maker_cfd.state, CfdState::PendingOpen { .. })); assert!(matches!(maker_cfd.state, CfdState::PendingOpen { .. }));
deliver_lock_finality_event(&maker, &taker, received.id).await;
let (taker_cfd, maker_cfd) = next_cfd(taker.cfd_feed(), maker.cfd_feed()).await.unwrap();
assert!(matches!(taker_cfd.state, CfdState::Open { .. }));
assert!(matches!(maker_cfd.state, CfdState::Open { .. }));
} }
#[tokio::test] #[tokio::test]

29
daemon/tests/harness/bdk.rs

@ -1,29 +0,0 @@
use bdk::bitcoin::util::psbt::{Global, PartiallySignedTransaction};
use bdk::bitcoin::{Transaction, Txid};
use std::collections::BTreeMap;
pub fn dummy_partially_signed_transaction() -> PartiallySignedTransaction {
// very simple dummy psbt that does not contain anything
// pulled in from github.com-1ecc6299db9ec823/bitcoin-0.27.1/src/util/psbt/mod.rs:238
PartiallySignedTransaction {
global: Global {
unsigned_tx: Transaction {
version: 2,
lock_time: 0,
input: vec![],
output: vec![],
},
xpub: Default::default(),
version: 0,
proprietary: BTreeMap::new(),
unknown: BTreeMap::new(),
},
inputs: vec![],
outputs: vec![],
}
}
pub fn dummy_tx_id() -> Txid {
dummy_partially_signed_transaction().extract_tx().txid()
}

11
daemon/tests/harness/mocks/mod.rs

@ -6,8 +6,6 @@ use self::monitor::MonitorActor;
use self::oracle::OracleActor; use self::oracle::OracleActor;
use self::wallet::WalletActor; use self::wallet::WalletActor;
use super::bdk::{dummy_partially_signed_transaction, dummy_tx_id};
pub mod monitor; pub mod monitor;
pub mod oracle; pub mod oracle;
pub mod wallet; pub mod wallet;
@ -39,19 +37,14 @@ impl Mocks {
// Helper function setting up a "happy path" wallet mock // Helper function setting up a "happy path" wallet mock
pub async fn mock_wallet_sign_and_broadcast(&mut self) { pub async fn mock_wallet_sign_and_broadcast(&mut self) {
let mut seq = mockall::Sequence::new();
self.wallet() self.wallet()
.await .await
.expect_sign() .expect_sign()
.times(1) .returning(|sign_msg| Ok(sign_msg.psbt));
.returning(|_| Ok(dummy_partially_signed_transaction()))
.in_sequence(&mut seq);
self.wallet() self.wallet()
.await .await
.expect_broadcast() .expect_broadcast()
.times(1) .returning(|broadcast_msg| Ok(broadcast_msg.tx.txid()));
.returning(|_| Ok(dummy_tx_id()))
.in_sequence(&mut seq);
} }
pub async fn mock_oracle_announcement(&mut self) { pub async fn mock_oracle_announcement(&mut self) {

49
daemon/tests/harness/mod.rs

@ -5,7 +5,7 @@ use crate::schnorrsig;
use ::bdk::bitcoin::Network; use ::bdk::bitcoin::Network;
use daemon::bitmex_price_feed::Quote; use daemon::bitmex_price_feed::Quote;
use daemon::connection::{connect, ConnectionStatus}; use daemon::connection::{connect, ConnectionStatus};
use daemon::model::cfd::Role; use daemon::model::cfd::{OrderId, Role};
use daemon::model::{self, Price, Timestamp, Usd}; use daemon::model::{self, Price, Timestamp, Usd};
use daemon::projection::{Cfd, CfdOrder, Feeds, Identity}; use daemon::projection::{Cfd, CfdOrder, Feeds, Identity};
use daemon::seed::Seed; use daemon::seed::Seed;
@ -27,7 +27,6 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
use xtra::Actor; use xtra::Actor;
pub mod bdk;
pub mod flow; pub mod flow;
pub mod maia; pub mod maia;
pub mod mocks; pub mod mocks;
@ -174,14 +173,8 @@ impl Maker {
.await .await
.unwrap(); .unwrap();
let dummy_quote = Quote {
timestamp: Timestamp::now(),
bid: Price::new(dec!(10000)).unwrap(),
ask: Price::new(dec!(10000)).unwrap(),
};
let (proj_actor, feeds) = let (proj_actor, feeds) =
projection::Actor::new(Role::Maker, Network::Testnet, vec![], dummy_quote); projection::Actor::new(Role::Maker, Network::Testnet, vec![], dummy_quote());
tasks.add(projection_context.run(proj_actor)); tasks.add(projection_context.run(proj_actor));
let address = listener.local_addr().unwrap(); let address = listener.local_addr().unwrap();
@ -297,14 +290,8 @@ impl Taker {
.await .await
.unwrap(); .unwrap();
let dummy_quote = Quote {
timestamp: Timestamp::now(),
bid: Price::new(dec!(10000)).unwrap(),
ask: Price::new(dec!(10000)).unwrap(),
};
let (proj_actor, feeds) = let (proj_actor, feeds) =
projection::Actor::new(Role::Taker, Network::Testnet, vec![], dummy_quote); projection::Actor::new(Role::Taker, Network::Testnet, vec![], dummy_quote());
tasks.add(projection_context.run(proj_actor)); tasks.add(projection_context.run(proj_actor));
tasks.add(connect( tasks.add(connect(
@ -336,6 +323,22 @@ impl Taker {
} }
} }
/// Deliver the event that provokes the transition to cfd's "Open" state
pub async fn deliver_lock_finality_event(maker: &Maker, taker: &Taker, id: OrderId) {
maker
.system
.cfd_actor_addr
.send(daemon::monitor::Event::LockFinality(id))
.await
.unwrap();
taker
.system
.cfd_actor_addr
.send(daemon::monitor::Event::LockFinality(id))
.await
.unwrap();
}
async fn in_memory_db() -> SqlitePool { async fn in_memory_db() -> SqlitePool {
// Note: Every :memory: database is distinct from every other. So, opening two database // Note: Every :memory: database is distinct from every other. So, opening two database
// connections each with the filename ":memory:" will create two independent in-memory // connections each with the filename ":memory:" will create two independent in-memory
@ -347,9 +350,21 @@ async fn in_memory_db() -> SqlitePool {
pool pool
} }
pub fn dummy_price() -> Price {
Price::new(dec!(50_000)).expect("to not fail")
}
pub fn dummy_quote() -> Quote {
Quote {
timestamp: Timestamp::now(),
bid: dummy_price(),
ask: dummy_price(),
}
}
pub fn dummy_new_order() -> maker_cfd::NewOrder { pub fn dummy_new_order() -> maker_cfd::NewOrder {
maker_cfd::NewOrder { maker_cfd::NewOrder {
price: Price::new(dec!(50_000)).expect("unexpected failure"), price: dummy_price(),
min_quantity: Usd::new(dec!(5)), min_quantity: Usd::new(dec!(5)),
max_quantity: Usd::new(dec!(100)), max_quantity: Usd::new(dec!(100)),
} }

Loading…
Cancel
Save