Skip to content

Commit 9c0fadb

Browse files
committed
Add test case for peer timeouts
1 parent b6ee9a4 commit 9c0fadb

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

crates/hotfix/tests/session_tests.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ async fn test_heartbeats() {
5151
mock_counterparty.assert_disconnected().await;
5252
}
5353

54+
#[tokio::test(start_paused = true)]
55+
async fn test_peer_timeout() {
56+
let (_session, mut mock_counterparty) = setup().await;
57+
let peer_interval = (1.2 * HEARTBEAT_INTERVAL as f64) as u64 + 1;
58+
59+
// assert that a logon message is received (type 'A')
60+
mock_counterparty
61+
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "A"))
62+
.await;
63+
// counterparty responds with a logon to establish a happy session
64+
mock_counterparty.send_logon().await;
65+
tokio::task::yield_now().await;
66+
67+
// we wait enough time for the peer deadline to pass
68+
tokio::time::advance(std::time::Duration::from_secs(peer_interval)).await;
69+
// a TestRequest (type '1') is sent to the counterparty
70+
mock_counterparty
71+
.assert_next(|msg| assert_eq!(msg.header().get::<&str>(MSG_TYPE).unwrap(), "1"))
72+
.await;
73+
74+
// we wait even longer and the counterparty never responds, so we disconnect from the counterparty
75+
tokio::time::advance(std::time::Duration::from_secs(peer_interval)).await;
76+
mock_counterparty.assert_disconnected().await;
77+
}
78+
5479
async fn setup() -> (SessionRef<TestMessage>, MockCounterparty<TestMessage>) {
5580
let config = create_session_config();
5681
let counterparty_config = create_counterparty_session_config(config.clone());

0 commit comments

Comments
 (0)