Skip to content

Commit 7aff2e2

Browse files
authored
RUST-610 Test that monitors wait at least minHeartbeatFrequencyMS between failed checks (#273)
1 parent a779891 commit 7aff2e2

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

src/sdam/monitor.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,7 @@ impl HeartbeatMonitor {
210210
}
211211
};
212212

213-
if result
214-
.as_ref()
215-
.err()
216-
.map(|e| e.kind.is_network_error())
217-
.unwrap_or(false)
218-
{
213+
if result.is_err() {
219214
self.connection.take();
220215
}
221216

src/sdam/test.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{sync::Arc, time::Duration};
1+
use std::{
2+
sync::Arc,
3+
time::{Duration, Instant},
4+
};
25

36
use bson::doc;
47
use 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)]

src/test/auth_aws.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
use super::TestClient;
1+
use tokio::sync::RwLockReadGuard;
2+
3+
use super::{TestClient, LOCK};
24

35
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
46
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
57
async fn auth_aws() {
8+
let _guard: RwLockReadGuard<()> = LOCK.run_concurrently().await;
9+
610
let client = TestClient::new().await;
711
let coll = client.database("aws").collection("somecoll");
812

0 commit comments

Comments
 (0)