Skip to content

Commit 8a74da4

Browse files
committed
Refactor setup into helpers using the wording 'given'
1 parent 4641340 commit 8a74da4

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

crates/hotfix/tests/common/setup.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
use crate::common::mock_application::MockApplication;
22
use crate::common::mock_counterparty::MockCounterparty;
3+
use crate::common::session_assertions::SessionAssertions;
34
use crate::common::test_messages::TestMessage;
45
use hotfix::application::ApplicationRef;
56
use hotfix::config::SessionConfig;
6-
use hotfix::session::SessionRef;
7+
use hotfix::session::{SessionRef, Status};
78
use hotfix::store::in_memory::InMemoryMessageStore;
9+
use hotfix_message::Part;
10+
use hotfix_message::fix44::MSG_TYPE;
811

912
pub const HEARTBEAT_INTERVAL: u64 = 30;
1013

11-
pub async fn setup() -> (SessionRef<TestMessage>, MockCounterparty<TestMessage>) {
14+
pub async fn given_a_connected_session() -> (SessionRef<TestMessage>, MockCounterparty<TestMessage>)
15+
{
1216
let config = create_session_config();
1317
let counterparty_config = create_counterparty_session_config(config.clone());
1418

@@ -21,6 +25,18 @@ pub async fn setup() -> (SessionRef<TestMessage>, MockCounterparty<TestMessage>)
2125
(session, mock_counterparty)
2226
}
2327

28+
pub async fn given_an_active_session() -> (SessionRef<TestMessage>, MockCounterparty<TestMessage>) {
29+
let (session, mut mock_counterparty) = given_a_connected_session().await;
30+
31+
mock_counterparty
32+
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
33+
.await;
34+
mock_counterparty.when_logon_is_sent().await;
35+
session.then_status_changes_to(Status::Active).await;
36+
37+
(session, mock_counterparty)
38+
}
39+
2440
pub fn create_session_config() -> SessionConfig {
2541
SessionConfig {
2642
begin_string: "FIX.4.4".to_string(),

crates/hotfix/tests/session_test_cases/heartbeat_tests.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::common::session_actions::SessionActions;
2-
use crate::common::session_assertions::SessionAssertions;
3-
use crate::common::setup::{HEARTBEAT_INTERVAL, setup};
4-
use hotfix::session::Status;
2+
use crate::common::setup::{HEARTBEAT_INTERVAL, given_an_active_session};
53
use hotfix_message::Part;
64
use hotfix_message::fix44::MSG_TYPE;
75
use std::time::Duration;
@@ -17,15 +15,7 @@ use std::time::Duration;
1715
/// as required by the FIX protocol to prevent timeout disconnections.
1816
#[tokio::test(start_paused = true)]
1917
async fn test_heartbeats() {
20-
let (session, mut mock_counterparty) = setup().await;
21-
22-
// assert that a logon message is received (type 'A')
23-
mock_counterparty
24-
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
25-
.await;
26-
// counterparty responds with a logon to establish a happy session
27-
mock_counterparty.when_logon_is_sent().await;
28-
session.then_status_changes_to(Status::Active).await;
18+
let (session, mut mock_counterparty) = given_an_active_session().await;
2919

3020
// let's wait enough time for a heartbeat and assert that the heartbeat was sent
3121
when_time_elapses(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;
@@ -48,18 +38,8 @@ async fn test_heartbeats() {
4838
/// if no response is received within the timeout period.
4939
#[tokio::test(start_paused = true)]
5040
async fn test_peer_timeout() {
51-
let (session, mut mock_counterparty) = setup().await;
5241
let peer_interval = (1.2 * HEARTBEAT_INTERVAL as f64) as u64 + 1;
53-
54-
// assert that a logon message is received (type 'A')
55-
mock_counterparty
56-
.then_receives(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
57-
.await;
58-
session.then_status_changes_to(Status::AwaitingLogon).await;
59-
60-
// counterparty responds with a logon to establish a happy session
61-
mock_counterparty.when_logon_is_sent().await;
62-
session.then_status_changes_to(Status::Active).await;
42+
let (_session, mut mock_counterparty) = given_an_active_session().await;
6343

6444
// let's wait enough time for a heartbeat and assert that the heartbeat was sent
6545
when_time_elapses(Duration::from_secs(HEARTBEAT_INTERVAL + 1)).await;

crates/hotfix/tests/session_test_cases/logon_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::common::session_actions::SessionActions;
22
use crate::common::session_assertions::SessionAssertions;
3-
use crate::common::setup::setup;
3+
use crate::common::setup::given_a_connected_session;
44
use hotfix::session::Status;
55
use hotfix_message::Part;
66
use hotfix_message::fix44::MSG_TYPE;
@@ -10,12 +10,14 @@ use hotfix_message::fix44::MSG_TYPE;
1010
/// transitions to Active status, and disconnects cleanly.
1111
#[tokio::test]
1212
async fn test_happy_logon() {
13-
let (session, mut mock_counterparty) = setup().await;
13+
let (session, mut mock_counterparty) = given_a_connected_session().await;
1414

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

0 commit comments

Comments
 (0)