Skip to content

Commit 280c87e

Browse files
authored
RUST-827 Improve stability of load-balancing server selection test (#445)
1 parent bbbc867 commit 280c87e

File tree

1 file changed

+48
-19
lines changed
  • src/sdam/description/topology/server_selection/test

1 file changed

+48
-19
lines changed

src/sdam/description/topology/server_selection/test/in_window.rs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, sync::Arc, time::Duration};
22

33
use approx::abs_diff_eq;
4-
use bson::Document;
4+
use bson::{doc, Document};
55
use semver::VersionReq;
66
use serde::Deserialize;
77
use tokio::sync::RwLockWriteGuard;
@@ -114,6 +114,13 @@ async fn load_balancing_test() {
114114
let _guard: RwLockWriteGuard<_> = LOCK.run_exclusively().await;
115115

116116
let mut setup_client_options = CLIENT_OPTIONS.clone();
117+
118+
// TODO: RUST-1004 unskip on auth variants
119+
if setup_client_options.credential.is_some() {
120+
println!("skipping load_balancing_test test due to auth being enabled");
121+
return;
122+
}
123+
117124
setup_client_options.hosts.drain(1..);
118125
setup_client_options.direct_connection = Some(true);
119126
let setup_client = TestClient::with_options(Some(setup_client_options)).await;
@@ -137,21 +144,17 @@ async fn load_balancing_test() {
137144
return;
138145
}
139146

140-
let options = FailCommandOptions::builder()
141-
.block_connection(Duration::from_millis(500))
142-
.build();
143-
let failpoint = FailPoint::fail_command(&["find"], FailPointMode::AlwaysOn, options);
144-
145-
let fp_guard = setup_client
146-
.enable_failpoint(failpoint, None)
147+
// seed the collection with a document so the find commands do some work
148+
setup_client
149+
.database("load_balancing_test")
150+
.collection("load_balancing_test")
151+
.insert_one(doc! {}, None)
147152
.await
148-
.expect("enabling failpoint should succeed");
149-
150-
let mut client = EventClient::new().await;
153+
.unwrap();
151154

152155
/// min_share is the lower bound for the % of times the the less selected server
153156
/// was selected. max_share is the upper bound.
154-
async fn do_test(client: &mut EventClient, min_share: f64, max_share: f64) {
157+
async fn do_test(client: &mut EventClient, min_share: f64, max_share: f64, iterations: usize) {
155158
client.clear_cached_events();
156159

157160
let mut handles: Vec<AsyncJoinHandle<()>> = Vec::new();
@@ -162,7 +165,7 @@ async fn load_balancing_test() {
162165
handles.push(
163166
RUNTIME
164167
.spawn(async move {
165-
for _ in 0..10 {
168+
for _ in 0..iterations {
166169
let _ = collection.find_one(None, None).await;
167170
}
168171
})
@@ -181,15 +184,41 @@ async fn load_balancing_test() {
181184
let mut counts: Vec<_> = tallies.values().collect();
182185
counts.sort();
183186

184-
// verify that the lesser picked server (slower one) was picked less than 25% of the time.
185187
let share_of_selections = (*counts[0] as f64) / ((*counts[0] + *counts[1]) as f64);
186-
assert!(share_of_selections <= max_share);
187-
assert!(share_of_selections >= min_share);
188+
assert!(
189+
share_of_selections <= max_share,
190+
"expected no more than {}% of selections, instead got {}%",
191+
(max_share * 100.0) as u32,
192+
(share_of_selections * 100.0) as u32
193+
);
194+
assert!(
195+
share_of_selections >= min_share,
196+
"expected at least {}% of selections, instead got {}%",
197+
(min_share * 100.0) as u32,
198+
(share_of_selections * 100.0) as u32
199+
);
188200
}
189201

190-
do_test(&mut client, 0.05, 0.25).await;
202+
let mut client = EventClient::new().await;
203+
204+
// saturate pools
205+
do_test(&mut client, 0.0, 0.50, 100).await;
206+
207+
// enable a failpoint on one of the mongoses to slow it down
208+
let options = FailCommandOptions::builder()
209+
.block_connection(Duration::from_millis(500))
210+
.build();
211+
let failpoint = FailPoint::fail_command(&["find"], FailPointMode::AlwaysOn, options);
212+
213+
let fp_guard = setup_client
214+
.enable_failpoint(failpoint, None)
215+
.await
216+
.expect("enabling failpoint should succeed");
217+
218+
// verify that the lesser picked server (slower one) was picked less than 25% of the time.
219+
do_test(&mut client, 0.05, 0.25, 10).await;
191220

192-
// disable failpoint and rerun, should be close to even split
221+
// disable failpoint and rerun, should be back to even split
193222
drop(fp_guard);
194-
do_test(&mut client, 0.40, 0.50).await;
223+
do_test(&mut client, 0.40, 0.50, 100).await;
195224
}

0 commit comments

Comments
 (0)