Skip to content

Commit 4641340

Browse files
committed
Take idea of when/then wording further
1 parent 7585153 commit 4641340

6 files changed

Lines changed: 36 additions & 26 deletions

File tree

crates/hotfix/tests/common/mock_counterparty.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ where
5454
self.session_config.heartbeat_interval,
5555
ResetSeqNumConfig::NoReset(None),
5656
);
57-
self.send_message(logon).await;
57+
self.when_message_is_sent(logon).await;
5858
}
5959

60-
pub async fn send_message(&mut self, message: impl FixMessage) {
60+
pub async fn when_message_is_sent(&mut self, message: impl FixMessage) {
6161
let raw_message = generate_message(
6262
&self.session_config.sender_comp_id,
6363
&self.session_config.target_comp_id,
@@ -110,7 +110,7 @@ where
110110
.await;
111111
}
112112

113-
pub async fn assert_next_with_timeout<F>(&mut self, assertion: F, timeout: Duration)
113+
async fn assert_next_with_timeout<F>(&mut self, assertion: F, timeout: Duration)
114114
where
115115
F: FnOnce(&Message),
116116
{
@@ -127,11 +127,11 @@ where
127127
}
128128
}
129129

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

134-
pub async fn assert_disconnected_with_timeout(&mut self, timeout: Duration) {
134+
async fn assert_disconnected_with_timeout(&mut self, timeout: Duration) {
135135
if tokio::time::timeout(timeout, async {
136136
// keep consuming messages until a disconnect occurs
137137
while self.get_next().await.is_some() {}

crates/hotfix/tests/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod mock_application;
22
pub mod mock_counterparty;
3+
pub mod session_actions;
34
pub mod session_assertions;
45
pub mod setup;
56
pub mod test_messages;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::common::test_messages::TestMessage;
2+
use hotfix::session::SessionRef;
3+
4+
pub trait SessionActions {
5+
async fn when_disconnect_is_requested(&self);
6+
}
7+
8+
impl SessionActions for SessionRef<TestMessage> {
9+
async fn when_disconnect_is_requested(&self) {
10+
self.disconnect("Test Session Finished".to_string()).await;
11+
}
12+
}

crates/hotfix/tests/common/session_assertions.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ pub const DEFAULT_TIMEOUT: Duration = Duration::from_millis(50);
66

77
pub trait SessionAssertions {
88
async fn then_status_changes_to(&self, expected_status: Status);
9-
async fn assert_status_with_timeout(&self, expected_status: Status, timeout: Duration);
10-
async fn when_disconnected(&self);
9+
async fn then_status_changes_within_time(&self, expected_status: Status, timeout: Duration);
1110
}
1211

1312
impl SessionAssertions for SessionRef<TestMessage> {
1413
async fn then_status_changes_to(&self, expected_status: Status) {
15-
self.assert_status_with_timeout(expected_status, DEFAULT_TIMEOUT)
14+
self.then_status_changes_within_time(expected_status, DEFAULT_TIMEOUT)
1615
.await;
1716
}
1817

19-
async fn assert_status_with_timeout(&self, expected_status: Status, timeout: Duration) {
18+
async fn then_status_changes_within_time(&self, expected_status: Status, timeout: Duration) {
2019
let deadline = tokio::time::Instant::now() + timeout;
2120
let retry_interval = Duration::from_millis(5);
2221

@@ -34,8 +33,4 @@ impl SessionAssertions for SessionRef<TestMessage> {
3433
"session did not reach expected status within timeout. Expected: {expected_status:?}, Actual: {actual_status:?}"
3534
);
3635
}
37-
38-
async fn when_disconnected(&self) {
39-
self.disconnect("Test Session Finished".to_string()).await;
40-
}
4136
}

crates/hotfix/tests/session_test_cases/heartbeat_tests.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
use crate::common::session_actions::SessionActions;
12
use crate::common::session_assertions::SessionAssertions;
23
use crate::common::setup::{HEARTBEAT_INTERVAL, setup};
34
use hotfix::session::Status;
45
use hotfix_message::Part;
56
use hotfix_message::fix44::MSG_TYPE;
67
use std::time::Duration;
78

8-
async fn when_time_advances(duration: Duration) {
9-
tokio::time::advance(duration).await;
10-
}
11-
129
/// Tests the automatic heartbeat mechanism in an active FIX session:
1310
/// 1. Establishes a session by exchanging logon messages with the counterparty
1411
/// 2. Advances time beyond the configured heartbeat interval
@@ -31,13 +28,13 @@ async fn test_heartbeats() {
3128
session.then_status_changes_to(Status::Active).await;
3229

3330
// let's wait enough time for a heartbeat and assert that the heartbeat was sent
34-
when_time_advances(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
31+
when_time_elapses(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
3532
mock_counterparty
3633
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "0"))
3734
.await;
3835

39-
session.when_disconnected().await;
40-
mock_counterparty.then_disconnects().await;
36+
session.when_disconnect_is_requested().await;
37+
mock_counterparty.then_gets_disconnected().await;
4138
}
4239

4340
/// Tests the peer timeout and disconnection mechanism:
@@ -65,19 +62,23 @@ async fn test_peer_timeout() {
6562
session.then_status_changes_to(Status::Active).await;
6663

6764
// let's wait enough time for a heartbeat and assert that the heartbeat was sent
68-
when_time_advances(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
65+
when_time_elapses(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
6966
mock_counterparty
7067
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "0"))
7168
.await;
7269

7370
// we wait enough time for the peer deadline to pass
74-
when_time_advances(Duration::from_secs(peer_interval - HEARTBEAT_INTERVAL)).await;
71+
when_time_elapses(Duration::from_secs(peer_interval - HEARTBEAT_INTERVAL)).await;
7572
// a TestRequest (type '1') is sent to the counterparty
7673
mock_counterparty
7774
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "1"))
7875
.await;
7976

8077
// we wait even longer and the counterparty never responds, so we disconnect from the counterparty
81-
when_time_advances(Duration::from_secs(peer_interval)).await;
82-
mock_counterparty.then_disconnects().await;
78+
when_time_elapses(Duration::from_secs(peer_interval)).await;
79+
mock_counterparty.then_gets_disconnected().await;
80+
}
81+
82+
async fn when_time_elapses(duration: Duration) {
83+
tokio::time::advance(duration).await;
8384
}

crates/hotfix/tests/session_test_cases/logon_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::common::session_actions::SessionActions;
12
use crate::common::session_assertions::SessionAssertions;
23
use crate::common::setup::setup;
34
use hotfix::session::Status;
@@ -19,6 +20,6 @@ async fn test_happy_logon() {
1920
mock_counterparty.when_logon_is_sent().await;
2021
session.then_status_changes_to(Status::Active).await;
2122

22-
session.when_disconnected().await;
23-
mock_counterparty.then_disconnects().await;
23+
session.when_disconnect_is_requested().await;
24+
mock_counterparty.then_gets_disconnected().await;
2425
}

0 commit comments

Comments
 (0)