Skip to content

Commit 015c198

Browse files
authored
RUST-812 Reduce flakiness of in_window::load_balancing_test (#568)
1 parent 2f6a9e3 commit 015c198

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

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

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ use crate::{
1313
selection_criteria::ReadPreference,
1414
test::{
1515
run_spec_test,
16-
EventClient,
16+
Event,
17+
EventHandler,
1718
FailCommandOptions,
1819
FailPoint,
1920
FailPointMode,
21+
SdamEvent,
2022
TestClient,
2123
CLIENT_OPTIONS,
2224
LOCK,
2325
},
26+
ServerType,
2427
RUNTIME,
2528
};
2629

@@ -114,14 +117,13 @@ async fn load_balancing_test() {
114117

115118
let mut setup_client_options = CLIENT_OPTIONS.clone();
116119

117-
// TODO: RUST-1004 unskip on auth variants
118-
if setup_client_options.credential.is_some() {
119-
println!("skipping load_balancing_test test due to auth being enabled");
120+
if setup_client_options.load_balanced.unwrap_or(false) {
121+
println!("skipping load_balancing_test test due to load-balanced topology");
120122
return;
121123
}
122124

123-
if setup_client_options.load_balanced.unwrap_or(false) {
124-
println!("skipping load_balancing_test test due to load-balanced topology");
125+
if setup_client_options.credential.is_some() {
126+
println!("skipping load_balancing_test test due to auth being enabled");
125127
return;
126128
}
127129

@@ -158,8 +160,14 @@ async fn load_balancing_test() {
158160

159161
/// min_share is the lower bound for the % of times the the less selected server
160162
/// was selected. max_share is the upper bound.
161-
async fn do_test(client: &mut EventClient, min_share: f64, max_share: f64, iterations: usize) {
162-
client.clear_cached_events();
163+
async fn do_test(
164+
client: &TestClient,
165+
handler: &mut EventHandler,
166+
min_share: f64,
167+
max_share: f64,
168+
iterations: usize,
169+
) {
170+
handler.clear_cached_events();
163171

164172
let mut handles: Vec<AsyncJoinHandle<()>> = Vec::new();
165173
for _ in 0..10 {
@@ -180,7 +188,7 @@ async fn load_balancing_test() {
180188
futures::future::join_all(handles).await;
181189

182190
let mut tallies: HashMap<ServerAddress, u32> = HashMap::new();
183-
for event in client.get_command_started_events(&["find"]) {
191+
for event in handler.get_command_started_events(&["find"]) {
184192
*tallies.entry(event.connection.address.clone()).or_insert(0) += 1;
185193
}
186194

@@ -203,10 +211,33 @@ async fn load_balancing_test() {
203211
);
204212
}
205213

206-
let mut client = EventClient::new().await;
214+
let mut handler = EventHandler::new();
215+
let mut subscriber = handler.subscribe();
216+
let mut options = CLIENT_OPTIONS.clone();
217+
options.local_threshold = Duration::from_secs(30).into();
218+
let client = TestClient::with_handler(Some(Arc::new(handler.clone())), options).await;
219+
220+
// wait for both servers to be discovered.
221+
subscriber
222+
.wait_for_event(Duration::from_secs(30), |event| {
223+
if let Event::Sdam(SdamEvent::TopologyDescriptionChanged(event)) = event {
224+
event
225+
.new_description
226+
.servers()
227+
.into_iter()
228+
.filter(|s| matches!(s.1.server_type(), ServerType::Mongos))
229+
.count()
230+
== 2
231+
} else {
232+
false
233+
}
234+
})
235+
.await
236+
.expect("timed out waiting for both mongoses to be discovered");
237+
drop(subscriber);
207238

208239
// saturate pools
209-
do_test(&mut client, 0.0, 0.50, 100).await;
240+
do_test(&client, &mut handler, 0.0, 0.50, 100).await;
210241

211242
// enable a failpoint on one of the mongoses to slow it down
212243
let options = FailCommandOptions::builder()
@@ -220,9 +251,9 @@ async fn load_balancing_test() {
220251
.expect("enabling failpoint should succeed");
221252

222253
// verify that the lesser picked server (slower one) was picked less than 25% of the time.
223-
do_test(&mut client, 0.05, 0.25, 10).await;
254+
do_test(&client, &mut handler, 0.05, 0.25, 10).await;
224255

225256
// disable failpoint and rerun, should be back to even split
226257
drop(fp_guard);
227-
do_test(&mut client, 0.40, 0.50, 100).await;
258+
do_test(&client, &mut handler, 0.40, 0.50, 100).await;
228259
}

src/test/util/event.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ impl EventHandler {
219219
pub fn connections_checked_out(&self) -> u32 {
220220
*self.connections_checked_out.lock().unwrap()
221221
}
222+
223+
pub fn clear_cached_events(&self) {
224+
self.command_events.write().unwrap().clear();
225+
self.cmap_events.write().unwrap().clear();
226+
self.sdam_events.write().unwrap().clear();
227+
}
222228
}
223229

224230
impl CmapEventHandler for EventHandler {
@@ -538,8 +544,7 @@ impl EventClient {
538544
}
539545

540546
pub fn clear_cached_events(&self) {
541-
self.handler.command_events.write().unwrap().clear();
542-
self.handler.cmap_events.write().unwrap().clear();
547+
self.handler.clear_cached_events()
543548
}
544549
}
545550

src/test/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl TestClient {
6161
Self::with_handler(None, options).await
6262
}
6363

64-
async fn with_handler(
64+
pub async fn with_handler(
6565
event_handler: Option<Arc<EventHandler>>,
6666
options: impl Into<Option<ClientOptions>>,
6767
) -> Self {

0 commit comments

Comments
 (0)