From 80771694af1fd96a1ba01bd73e10160202d059e8 Mon Sep 17 00:00:00 2001 From: Nobu Maeda Date: Mon, 27 Nov 2023 16:39:25 +0800 Subject: [PATCH] Add Trade Engine Specific message test to test_simple --- tests/common/maker_testers.rs | 41 ++++++++++++++++++++++++++++---- tests/common/mod.rs | 1 + tests/common/taker_testers.rs | 42 +++++++++++++++++++++++++++++---- tests/common/test_trade_msgs.rs | 41 ++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 tests/common/test_trade_msgs.rs diff --git a/tests/common/maker_testers.rs b/tests/common/maker_testers.rs index 4dedd40..e0719d0 100644 --- a/tests/common/maker_testers.rs +++ b/tests/common/maker_testers.rs @@ -5,6 +5,7 @@ use crusty_n3xb::{ manager::Manager, offer::OfferEnvelope, order::Order, + peer_msg::PeerEnvelope, testing::{SomeTestOfferParams, SomeTestTradeRspParams, TESTING_DEFAULT_CHANNEL_SIZE}, }; use tokio::{ @@ -12,6 +13,8 @@ use tokio::{ time::sleep, }; +use crate::common::test_trade_msgs::{AnotherTradeEngMsg, SomeTradeEngMsg}; + pub struct MakerTester { cmpl_rx: oneshot::Receiver>, } @@ -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::>(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::>(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(); @@ -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; @@ -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::() + .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(); diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 3ad14e2..06f36ca 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,3 +1,4 @@ pub mod maker_testers; pub mod relay; pub mod taker_testers; +pub mod test_trade_msgs; diff --git a/tests/common/taker_testers.rs b/tests/common/taker_testers.rs index 5cdb96a..a704f1d 100644 --- a/tests/common/taker_testers.rs +++ b/tests/common/taker_testers.rs @@ -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, @@ -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, N3xbError>>, } @@ -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::>(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::>(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()); @@ -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::() + .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(); diff --git a/tests/common/test_trade_msgs.rs b/tests/common/test_trade_msgs.rs new file mode 100644 index 0000000..972c3e2 --- /dev/null +++ b/tests/common/test_trade_msgs.rs @@ -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() + } +}