Skip to content

Commit 252187c

Browse files
committed
Move setup utilities into common module to share between test modules
1 parent 7598479 commit 252187c

3 files changed

Lines changed: 50 additions & 47 deletions

File tree

crates/hotfix/tests/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod mock_application;
22
pub mod mock_counterparty;
33
pub mod session_assertions;
4+
pub mod setup;
45
pub mod test_messages;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::common::mock_application::MockApplication;
2+
use crate::common::mock_counterparty::MockCounterparty;
3+
use crate::common::test_messages::TestMessage;
4+
use hotfix::application::ApplicationRef;
5+
use hotfix::config::SessionConfig;
6+
use hotfix::session::SessionRef;
7+
use hotfix::store::in_memory::InMemoryMessageStore;
8+
9+
pub const HEARTBEAT_INTERVAL: u64 = 30;
10+
11+
pub async fn setup() -> (SessionRef<TestMessage>, MockCounterparty<TestMessage>) {
12+
let config = create_session_config();
13+
let counterparty_config = create_counterparty_session_config(config.clone());
14+
15+
let application_ref = ApplicationRef::new(MockApplication {});
16+
let message_store = InMemoryMessageStore::default();
17+
18+
let session = SessionRef::new(config, application_ref, message_store);
19+
let mock_counterparty = MockCounterparty::start(session.clone(), counterparty_config).await;
20+
21+
(session, mock_counterparty)
22+
}
23+
24+
pub fn create_session_config() -> SessionConfig {
25+
SessionConfig {
26+
begin_string: "FIX.4.4".to_string(),
27+
sender_comp_id: "dummy-initiator".to_string(),
28+
target_comp_id: "dummy-acceptor".to_string(),
29+
data_dictionary_path: None,
30+
connection_host: "".to_string(),
31+
connection_port: 0,
32+
tls_config: None,
33+
heartbeat_interval: HEARTBEAT_INTERVAL,
34+
reconnect_interval: 30,
35+
reset_on_logon: false,
36+
schedule: None,
37+
}
38+
}
39+
40+
/// Create a session configuration for the counterparty from our configuration.
41+
pub fn create_counterparty_session_config(session_config: SessionConfig) -> SessionConfig {
42+
SessionConfig {
43+
sender_comp_id: session_config.target_comp_id.clone(),
44+
target_comp_id: session_config.sender_comp_id.clone(),
45+
..session_config
46+
}
47+
}

crates/hotfix/tests/session_test_cases/heartbeat_tests.rs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
use crate::common::mock_application::MockApplication;
2-
use crate::common::mock_counterparty::MockCounterparty;
31
use crate::common::session_assertions::SessionAssertions;
4-
use crate::common::test_messages::TestMessage;
5-
use hotfix::application::ApplicationRef;
6-
use hotfix::config::SessionConfig;
7-
use hotfix::session::{SessionRef, Status};
8-
use hotfix::store::in_memory::InMemoryMessageStore;
2+
use crate::common::setup::{HEARTBEAT_INTERVAL, setup};
3+
use hotfix::session::Status;
94
use hotfix_message::Part;
105
use hotfix_message::fix44::MSG_TYPE;
116

12-
const HEARTBEAT_INTERVAL: u64 = 30;
13-
147
/// Tests the automatic heartbeat mechanism in an active FIX session:
158
/// 1. Establishes a session by exchanging logon messages with the counterparty
169
/// 2. Advances time beyond the configured heartbeat interval
@@ -88,41 +81,3 @@ async fn test_peer_timeout() {
8881
tokio::time::advance(std::time::Duration::from_secs(peer_interval)).await;
8982
mock_counterparty.assert_disconnected().await;
9083
}
91-
92-
async fn setup() -> (SessionRef<TestMessage>, MockCounterparty<TestMessage>) {
93-
let config = create_session_config();
94-
let counterparty_config = create_counterparty_session_config(config.clone());
95-
96-
let application_ref = ApplicationRef::new(MockApplication {});
97-
let message_store = InMemoryMessageStore::default();
98-
99-
let session = SessionRef::new(config, application_ref, message_store);
100-
let mock_counterparty = MockCounterparty::start(session.clone(), counterparty_config).await;
101-
102-
(session, mock_counterparty)
103-
}
104-
105-
fn create_session_config() -> SessionConfig {
106-
SessionConfig {
107-
begin_string: "FIX.4.4".to_string(),
108-
sender_comp_id: "dummy-initiator".to_string(),
109-
target_comp_id: "dummy-acceptor".to_string(),
110-
data_dictionary_path: None,
111-
connection_host: "".to_string(),
112-
connection_port: 0,
113-
tls_config: None,
114-
heartbeat_interval: HEARTBEAT_INTERVAL,
115-
reconnect_interval: 30,
116-
reset_on_logon: false,
117-
schedule: None,
118-
}
119-
}
120-
121-
/// Create a session configuration for the counterparty from our configuration.
122-
fn create_counterparty_session_config(session_config: SessionConfig) -> SessionConfig {
123-
SessionConfig {
124-
sender_comp_id: session_config.target_comp_id.clone(),
125-
target_comp_id: session_config.sender_comp_id.clone(),
126-
..session_config
127-
}
128-
}

0 commit comments

Comments
 (0)