@@ -3,6 +3,11 @@ use crate::common::setup::{HEARTBEAT_INTERVAL, setup};
33use hotfix:: session:: Status ;
44use hotfix_message:: Part ;
55use 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}
0 commit comments