Skip to content

Commit a497db1

Browse files
authored
RUST-482 Fix race in session tests (#210)
1 parent 6e1feb9 commit a497db1

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/test/util/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use self::{
88
matchable::{assert_matches, Matchable},
99
};
1010

11-
use std::{collections::HashMap, fmt::Debug, sync::Arc};
11+
use std::{collections::HashMap, fmt::Debug, sync::Arc, time::Duration};
1212

1313
use crate::bson::{doc, oid::ObjectId, Bson};
1414
use semver::Version;
@@ -18,6 +18,7 @@ use self::event::EventHandler;
1818
use super::CLIENT_OPTIONS;
1919
use crate::{
2020
error::{CommandError, ErrorKind, Result},
21+
operation::RunCommand,
2122
options::{AuthMechanism, ClientOptions, CreateCollectionOptions},
2223
Client,
2324
Collection,
@@ -61,18 +62,27 @@ impl TestClient {
6162

6263
let client = Client::with_options(options.clone()).unwrap();
6364

65+
// To avoid populating the session pool with leftover implicit sessions, we check out a
66+
// session here and immediately mark it as dirty, then use it with any operations we need.
67+
let mut session = client
68+
.start_implicit_session_with_timeout(Duration::from_secs(60 * 60))
69+
.await;
70+
session.mark_dirty();
71+
72+
let is_master = RunCommand::new("admin".into(), doc! { "isMaster": 1 }, None).unwrap();
73+
6474
let server_info = bson::from_bson(Bson::Document(
6575
client
66-
.database("admin")
67-
.run_command(doc! { "isMaster": 1 }, None)
76+
.execute_operation_with_session(is_master, &mut session)
6877
.await
6978
.unwrap(),
7079
))
7180
.unwrap();
7281

82+
let build_info = RunCommand::new("test".into(), doc! { "buildInfo": 1 }, None).unwrap();
83+
7384
let response = client
74-
.database("test")
75-
.run_command(doc! { "buildInfo": 1 }, None)
85+
.execute_operation_with_session(build_info, &mut session)
7686
.await
7787
.unwrap();
7888

0 commit comments

Comments
 (0)