Skip to content

Commit 7585153

Browse files
committed
Experiment with the language used for actions and assertions in session test cases
1 parent d6eb713 commit 7585153

4 files changed

Lines changed: 37 additions & 34 deletions

File tree

crates/hotfix/tests/common/mock_counterparty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ where
4949
}
5050
}
5151

52-
pub async fn send_logon(&mut self) {
52+
pub async fn when_logon_is_sent(&mut self) {
5353
let logon = Logon::new(
5454
self.session_config.heartbeat_interval,
5555
ResetSeqNumConfig::NoReset(None),
@@ -102,7 +102,7 @@ where
102102
}
103103
}
104104

105-
pub async fn assert_next<F>(&mut self, assertion: F)
105+
pub async fn then_receives<F>(&mut self, assertion: F)
106106
where
107107
F: FnOnce(&Message),
108108
{
@@ -127,7 +127,7 @@ where
127127
}
128128
}
129129

130-
pub async fn assert_disconnected(&mut self) {
130+
pub async fn then_disconnects(&mut self) {
131131
self.assert_disconnected_with_timeout(DEFAULT_TIMEOUT).await;
132132
}
133133

crates/hotfix/tests/common/session_assertions.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use std::time::Duration;
55
pub const DEFAULT_TIMEOUT: Duration = Duration::from_millis(50);
66

77
pub trait SessionAssertions {
8-
async fn assert_status(&self, expected_status: Status);
8+
async fn then_status_changes_to(&self, expected_status: Status);
99
async fn assert_status_with_timeout(&self, expected_status: Status, timeout: Duration);
10+
async fn when_disconnected(&self);
1011
}
1112

1213
impl SessionAssertions for SessionRef<TestMessage> {
13-
async fn assert_status(&self, expected_status: Status) {
14+
async fn then_status_changes_to(&self, expected_status: Status) {
1415
self.assert_status_with_timeout(expected_status, DEFAULT_TIMEOUT)
1516
.await;
1617
}
@@ -33,4 +34,8 @@ impl SessionAssertions for SessionRef<TestMessage> {
3334
"session did not reach expected status within timeout. Expected: {expected_status:?}, Actual: {actual_status:?}"
3435
);
3536
}
37+
38+
async fn when_disconnected(&self) {
39+
self.disconnect("Test Session Finished".to_string()).await;
40+
}
3641
}

crates/hotfix/tests/session_test_cases/heartbeat_tests.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use crate::common::setup::{HEARTBEAT_INTERVAL, setup};
33
use hotfix::session::Status;
44
use hotfix_message::Part;
55
use hotfix_message::fix44::MSG_TYPE;
6+
use std::time::Duration;
7+
8+
async fn when_time_advances(duration: Duration) {
9+
tokio::time::advance(duration).await;
10+
}
611

712
/// Tests the automatic heartbeat mechanism in an active FIX session:
813
/// 1. Establishes a session by exchanging logon messages with the counterparty
@@ -19,22 +24,20 @@ async fn test_heartbeats() {
1924

2025
// assert that a logon message is received (type 'A')
2126
mock_counterparty
22-
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
27+
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
2328
.await;
2429
// counterparty responds with a logon to establish a happy session
25-
mock_counterparty.send_logon().await;
26-
session.assert_status(Status::Active).await;
30+
mock_counterparty.when_logon_is_sent().await;
31+
session.then_status_changes_to(Status::Active).await;
2732

2833
// let's wait enough time for a heartbeat and assert that the heartbeat was sent
29-
tokio::time::advance(std::time::Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
34+
when_time_advances(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
3035
mock_counterparty
31-
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "0"))
36+
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "0"))
3237
.await;
3338

34-
session
35-
.disconnect("Test Session Finished".to_string())
36-
.await;
37-
mock_counterparty.assert_disconnected().await;
39+
session.when_disconnected().await;
40+
mock_counterparty.then_disconnects().await;
3841
}
3942

4043
/// Tests the peer timeout and disconnection mechanism:
@@ -53,31 +56,28 @@ async fn test_peer_timeout() {
5356

5457
// assert that a logon message is received (type 'A')
5558
mock_counterparty
56-
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
59+
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
5760
.await;
58-
session.assert_status(Status::AwaitingLogon).await;
61+
session.then_status_changes_to(Status::AwaitingLogon).await;
5962

6063
// counterparty responds with a logon to establish a happy session
61-
mock_counterparty.send_logon().await;
62-
session.assert_status(Status::Active).await;
64+
mock_counterparty.when_logon_is_sent().await;
65+
session.then_status_changes_to(Status::Active).await;
6366

6467
// let's wait enough time for a heartbeat and assert that the heartbeat was sent
65-
tokio::time::advance(std::time::Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
68+
when_time_advances(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
6669
mock_counterparty
67-
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "0"))
70+
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "0"))
6871
.await;
6972

7073
// we wait enough time for the peer deadline to pass
71-
tokio::time::advance(std::time::Duration::from_secs(
72-
peer_interval - HEARTBEAT_INTERVAL,
73-
))
74-
.await;
74+
when_time_advances(Duration::from_secs(peer_interval - HEARTBEAT_INTERVAL)).await;
7575
// a TestRequest (type '1') is sent to the counterparty
7676
mock_counterparty
77-
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "1"))
77+
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "1"))
7878
.await;
7979

8080
// we wait even longer and the counterparty never responds, so we disconnect from the counterparty
81-
tokio::time::advance(std::time::Duration::from_secs(peer_interval)).await;
82-
mock_counterparty.assert_disconnected().await;
81+
when_time_advances(Duration::from_secs(peer_interval)).await;
82+
mock_counterparty.then_disconnects().await;
8383
}

crates/hotfix/tests/session_test_cases/logon_tests.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ async fn test_happy_logon() {
1313

1414
// assert that a logon message is received (type 'A')
1515
mock_counterparty
16-
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
16+
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
1717
.await;
1818
// counterparty responds with a logon to establish a happy session
19-
mock_counterparty.send_logon().await;
20-
session.assert_status(Status::Active).await;
19+
mock_counterparty.when_logon_is_sent().await;
20+
session.then_status_changes_to(Status::Active).await;
2121

22-
session
23-
.disconnect("Test Session Finished".to_string())
24-
.await;
25-
mock_counterparty.assert_disconnected().await;
22+
session.when_disconnected().await;
23+
mock_counterparty.then_disconnects().await;
2624
}

0 commit comments

Comments
 (0)