1- use std:: { sync:: Arc , time:: Duration } ;
1+ use std:: {
2+ sync:: Arc ,
3+ time:: { Duration , Instant } ,
4+ } ;
25
36use bson:: doc;
47use semver:: VersionReq ;
@@ -22,6 +25,63 @@ use crate::{
2225 RUNTIME ,
2326} ;
2427
28+ #[ cfg_attr( feature = "tokio-runtime" , tokio:: test( threaded_scheduler) ) ]
29+ #[ cfg_attr( feature = "async-std-runtime" , async_std:: test) ]
30+ async fn min_heartbeat_frequency ( ) {
31+ let _guard: RwLockWriteGuard < _ > = LOCK . run_exclusively ( ) . await ;
32+
33+ let mut setup_client_options = CLIENT_OPTIONS . clone ( ) ;
34+ setup_client_options. hosts . drain ( 1 ..) ;
35+ setup_client_options. direct_connection = Some ( true ) ;
36+
37+ let setup_client = TestClient :: with_options ( Some ( setup_client_options. clone ( ) ) , true ) . await ;
38+
39+ if !setup_client. supports_fail_command ( ) . await {
40+ println ! ( "skipping min_heartbeat_frequency test due to server not supporting fail points" ) ;
41+ return ;
42+ }
43+
44+ if setup_client. server_version_lt ( 4 , 9 ) {
45+ println ! ( "skipping min_heartbeat_frequency test due to server version being less than 4.9" ) ;
46+ return ;
47+ }
48+
49+ let fp_options = FailCommandOptions :: builder ( )
50+ . app_name ( "SDAMMinHeartbeatFrequencyTest" . to_string ( ) )
51+ . error_code ( 1234 )
52+ . build ( ) ;
53+ let failpoint = FailPoint :: fail_command ( & [ "isMaster" ] , FailPointMode :: Times ( 5 ) , fp_options) ;
54+
55+ let _fp_guard = setup_client
56+ . enable_failpoint ( failpoint, None )
57+ . await
58+ . expect ( "enabling failpoint should succeed" ) ;
59+
60+ let mut options = setup_client_options;
61+ options. app_name = Some ( "SDAMMinHeartbeatFrequencyTest" . to_string ( ) ) ;
62+ options. server_selection_timeout = Some ( Duration :: from_secs ( 5 ) ) ;
63+ let client = Client :: with_options ( options) . expect ( "client creation succeeds" ) ;
64+
65+ let start = Instant :: now ( ) ;
66+ client
67+ . database ( "admin" )
68+ . run_command ( doc ! { "ping" : 1 } , None )
69+ . await
70+ . expect ( "ping should eventually succeed" ) ;
71+
72+ let elapsed = Instant :: now ( ) . duration_since ( start) . as_millis ( ) ;
73+ assert ! (
74+ elapsed >= 2000 ,
75+ "expected to take at least 2 seconds, instead took {}ms" ,
76+ elapsed
77+ ) ;
78+ assert ! (
79+ elapsed <= 3500 ,
80+ "expected to take at most 3.5 seconds, instead took {}ms" ,
81+ elapsed
82+ ) ;
83+ }
84+
2585// TODO: RUST-232 update this test to incorporate SDAM events
2686#[ cfg_attr( feature = "tokio-runtime" , tokio:: test( threaded_scheduler) ) ]
2787#[ cfg_attr( feature = "async-std-runtime" , async_std:: test) ]
0 commit comments