Skip to content

Commit

Permalink
Add Trade Engine Specific message test to test_simple
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu-maeda committed Nov 27, 2023
1 parent e35818c commit 8077169
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 8 deletions.
41 changes: 37 additions & 4 deletions tests/common/maker_testers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use crusty_n3xb::{
manager::Manager,
offer::OfferEnvelope,
order::Order,
peer_msg::PeerEnvelope,
testing::{SomeTestOfferParams, SomeTestTradeRspParams, TESTING_DEFAULT_CHANNEL_SIZE},
};
use tokio::{
sync::{mpsc, oneshot},
time::sleep,
};

use crate::common::test_trade_msgs::{AnotherTradeEngMsg, SomeTradeEngMsg};

pub struct MakerTester {
cmpl_rx: oneshot::Receiver<Result<(), N3xbError>>,
}
Expand Down Expand Up @@ -57,9 +60,14 @@ impl MakerTesterActor {
let maker = self.manager.new_maker(order).await.unwrap();

// Register Maker for Offer notificaitons
let (notif_tx, mut notif_rx) =
let (offer_notif_tx, mut offer_notif_rx) =
mpsc::channel::<Result<OfferEnvelope, N3xbError>>(TESTING_DEFAULT_CHANNEL_SIZE);
maker.register_offer_notif_tx(notif_tx).await.unwrap();
maker.register_offer_notif_tx(offer_notif_tx).await.unwrap();

// Register Maker for Trade Engine specific Peer Messages
let (peer_notif_tx, mut peer_notif_rx) =
mpsc::channel::<Result<PeerEnvelope, N3xbError>>(TESTING_DEFAULT_CHANNEL_SIZE);
maker.register_peer_notif_tx(peer_notif_tx).await.unwrap();

// The whole thing kicks off by sending a Maker Order Note
let maker = maker.post_new_order().await.unwrap();
Expand All @@ -73,8 +81,8 @@ impl MakerTesterActor {
}

// Wait for Offer notifications - This can be made into a loop if wanted, or to wait for a particular offer
let notif_result = notif_rx.recv().await.unwrap();
let offer_envelope = notif_result.unwrap();
let offer_notif_result = offer_notif_rx.recv().await.unwrap();
let offer_envelope = offer_notif_result.unwrap();

// Query Offer
let offer_envelopes = maker.query_offers().await;
Expand All @@ -98,6 +106,31 @@ impl MakerTesterActor {
let trade_rsp = trade_rsp_builder.build().unwrap();
let maker = maker.accept_offer(trade_rsp).await.unwrap();

// Wait for a Trade Engine speicifc Peer Message
let peer_notif_result = peer_notif_rx.recv().await.unwrap();
let peer_envelope = peer_notif_result.unwrap();

// Check Peer Message that its SomeTradeEngSpeicficMsg
let some_trade_eng_msg = peer_envelope
.message
.downcast_ref::<SomeTradeEngMsg>()
.unwrap();

assert_eq!(
some_trade_eng_msg.some_trade_specific_field,
SomeTradeEngMsg::some_trade_specific_string()
);

// Respond with another Trade Engine specific Peer Message
let another_trade_eng_msg = AnotherTradeEngMsg {
another_trade_specific_field: AnotherTradeEngMsg::another_trade_specific_string(),
};

maker
.send_peer_message(Box::new(another_trade_eng_msg))
.await
.unwrap();

maker.trade_complete().await.unwrap();
self.manager.shutdown().await.unwrap();

Expand Down
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod maker_testers;
pub mod relay;
pub mod taker_testers;
pub mod test_trade_msgs;
42 changes: 38 additions & 4 deletions tests/common/taker_testers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crusty_n3xb::{
common::error::N3xbError,
manager::Manager,
order::{FilterTag, OrderEnvelope},
peer_msg::PeerEnvelope,
testing::{
SomeTestOfferParams, SomeTestOrderParams, SomeTestTradeRspParams,
TESTING_DEFAULT_CHANNEL_SIZE,
Expand All @@ -11,6 +12,10 @@ use crusty_n3xb::{
use tokio::sync::{mpsc, oneshot};
use uuid::Uuid;

use crate::common::test_trade_msgs::AnotherTradeEngMsg;

use super::test_trade_msgs::SomeTradeEngMsg;

pub struct TakerTester {
cmpl_rx: oneshot::Receiver<Result<Vec<OrderEnvelope>, N3xbError>>,
}
Expand Down Expand Up @@ -98,16 +103,20 @@ impl TakerTesterActor {
let taker = self.manager.new_taker(order_envelope, offer).await.unwrap();

// Register Taker for Trade Response notifications
let (notif_tx, mut notif_rx) =
let (trade_notif_tx, mut trade_notif_rx) =
mpsc::channel::<Result<TradeResponseEnvelope, N3xbError>>(TESTING_DEFAULT_CHANNEL_SIZE);
taker.register_trade_notif_tx(notif_tx).await.unwrap();
taker.register_trade_notif_tx(trade_notif_tx).await.unwrap();

let (peer_notif_tx, mut peer_notif_rx) =
mpsc::channel::<Result<PeerEnvelope, N3xbError>>(TESTING_DEFAULT_CHANNEL_SIZE);
taker.register_peer_notif_tx(peer_notif_tx).await.unwrap();

// Take Order with configured Offer
let taker = taker.take_order().await.unwrap();

// Wait for Trade Response notifications
let notif_result = notif_rx.recv().await.unwrap();
let trade_rsp_envelope = notif_result.unwrap();
let trade_notif_result = trade_notif_rx.recv().await.unwrap();
let trade_rsp_envelope = trade_notif_result.unwrap();

let mut expected_trade_rsp_builder = SomeTestTradeRspParams::default_builder();
expected_trade_rsp_builder.offer_event_id("".to_string());
Expand All @@ -120,6 +129,31 @@ impl TakerTesterActor {
TradeResponseStatus::Accepted
);

// Send a Trade Engine specific Peer Message
let some_trade_eng_msg = SomeTradeEngMsg {
some_trade_specific_field: SomeTradeEngMsg::some_trade_specific_string(),
};

taker
.send_peer_message(Box::new(some_trade_eng_msg))
.await
.unwrap();

// Wait for another Trade Engine specific Peer Message
let peer_notif_result = peer_notif_rx.recv().await.unwrap();
let peer_envelope = peer_notif_result.unwrap();

// Check Peer Message
let another_trade_eng_msg = peer_envelope
.message
.downcast_ref::<AnotherTradeEngMsg>()
.unwrap();

assert_eq!(
another_trade_eng_msg.another_trade_specific_field,
AnotherTradeEngMsg::another_trade_specific_string()
);

taker.trade_complete().await.unwrap();
self.manager.shutdown().await.unwrap();

Expand Down
41 changes: 41 additions & 0 deletions tests/common/test_trade_msgs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::any::Any;

use serde::{Deserialize, Serialize};

use crusty_n3xb::common::types::SerdeGenericTrait;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct SomeTradeEngMsg {
pub(crate) some_trade_specific_field: String,
}

#[typetag::serde(name = "some-trade-eng-msg")]
impl SerdeGenericTrait for SomeTradeEngMsg {
fn any_ref(&self) -> &dyn Any {
self
}
}

impl SomeTradeEngMsg {
pub(crate) fn some_trade_specific_string() -> String {
"SomeTradeSpecificString".to_string()
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) struct AnotherTradeEngMsg {
pub(crate) another_trade_specific_field: String,
}

#[typetag::serde(name = "another-trade-eng-msg")]
impl SerdeGenericTrait for AnotherTradeEngMsg {
fn any_ref(&self) -> &dyn Any {
self
}
}

impl AnotherTradeEngMsg {
pub(crate) fn another_trade_specific_string() -> String {
"AnotherTradeSpecificString".to_string()
}
}

0 comments on commit 8077169

Please sign in to comment.