Skip to content

Commit 7385687

Browse files
cr feedback
1 parent d154470 commit 7385687

File tree

9 files changed

+55
-94
lines changed

9 files changed

+55
-94
lines changed

src/client/session/cluster_time.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use crate::bson::{Document, Timestamp};
1010
#[derive(Debug, Deserialize, Clone, Serialize, Derivative)]
1111
#[derivative(PartialEq, Eq)]
1212
#[serde(rename_all = "camelCase")]
13-
pub(crate) struct ClusterTime {
14-
cluster_time: Timestamp,
13+
pub struct ClusterTime {
14+
pub cluster_time: Timestamp,
1515

1616
#[derivative(PartialEq = "ignore")]
17-
signature: Document,
17+
pub signature: Document,
1818
}
1919

2020
impl std::cmp::Ord for ClusterTime {

src/client/session/mod.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ lazy_static! {
2929
};
3030
}
3131

32-
/// Session to be used with client operations. This acts as a handle to a server session.
33-
/// This keeps the details of how server sessions are pooled opaque to users.
32+
/// A MongoDB client session. This struct represents a logical session used for ordering sequential
33+
/// operations. To create a `ClientSession`, call `start_session` on a `Client`.
34+
///
35+
/// `ClientSession` instances are not thread safe or fork safe. They can only be used by one thread
36+
/// or process at a time.
3437
#[derive(Debug)]
3538
pub struct ClientSession {
3639
cluster_time: Option<ClusterTime>,
@@ -57,8 +60,13 @@ impl ClientSession {
5760
}
5861
}
5962

63+
/// The client used to create this session.
64+
pub fn client(&self) -> Client {
65+
self.client.clone()
66+
}
67+
6068
/// The id of this session.
61-
pub(crate) fn id(&self) -> &Document {
69+
pub fn id(&self) -> &Document {
6270
&self.server_session.id
6371
}
6472

@@ -69,13 +77,18 @@ impl ClientSession {
6977

7078
/// The highest seen cluster time this session has seen so far.
7179
/// This will be `None` if this session has not been used in an operation yet.
72-
pub(crate) fn cluster_time(&self) -> Option<&ClusterTime> {
80+
pub fn cluster_time(&self) -> Option<&ClusterTime> {
7381
self.cluster_time.as_ref()
7482
}
7583

84+
/// The options used to create this session.
85+
pub fn options(&self) -> Option<&SessionOptions> {
86+
self.options.as_ref()
87+
}
88+
7689
/// Set the cluster time to the provided one if it is greater than this session's highest seen
7790
/// cluster time or if this session's cluster time is `None`.
78-
pub(crate) fn advance_cluster_time(&mut self, to: &ClusterTime) {
91+
pub fn advance_cluster_time(&mut self, to: &ClusterTime) {
7992
if self.cluster_time().map(|ct| ct < to).unwrap_or(true) {
8093
self.cluster_time = Some(to.clone());
8194
}
@@ -103,13 +116,6 @@ impl ClientSession {
103116
pub(crate) fn is_dirty(&self) -> bool {
104117
self.server_session.dirty
105118
}
106-
107-
/// Returns a mutable reference to this session wrapped in an option. This method is necessary
108-
/// to bypass the construction of an option taking ownership of a mutable reference.
109-
#[allow(clippy::unnecessary_wraps)]
110-
pub(crate) fn as_mut_ref_option(&mut self) -> Option<&mut Self> {
111-
Some(self)
112-
}
113119
}
114120

115121
impl Drop for ClientSession {

src/coll/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,10 @@ where
460460
filter: impl Into<Option<Document>>,
461461
options: impl Into<Option<FindOneOptions>>,
462462
) -> Result<Option<T>> {
463-
let mut options: FindOptions = options
463+
let options: FindOptions = options
464464
.into()
465465
.map(Into::into)
466466
.unwrap_or_else(Default::default);
467-
options.limit = Some(-1);
468467
let mut cursor = self.find(filter, Some(options)).await?;
469468
cursor.next().await.transpose()
470469
}
@@ -477,11 +476,10 @@ where
477476
options: impl Into<Option<FindOneOptions>>,
478477
session: &mut ClientSession,
479478
) -> Result<Option<T>> {
480-
let mut options: FindOptions = options
479+
let options: FindOptions = options
481480
.into()
482481
.map(Into::into)
483482
.unwrap_or_else(Default::default);
484-
options.limit = Some(-1);
485483
let mut cursor = self
486484
.find_with_session(filter, Some(options), session)
487485
.await?;

src/cursor/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'session> GetMoreProvider for ExplicitSessionGetMoreProvider<'session> {
199199
let future = Box::pin(async move {
200200
let get_more = GetMore::new(info);
201201
let get_more_result = client
202-
.execute_operation(get_more, session.reference.as_mut_ref_option())
202+
.execute_operation(get_more, Some(&mut *session.reference))
203203
.await;
204204
ExecutionResult {
205205
get_more_result,

src/db/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl Database {
292292
ListCollections::new(self.name().to_string(), filter.into(), true, None);
293293
let mut cursor: SessionCursor<Document> = self
294294
.client()
295-
.execute_operation(list_collections, session.as_mut_ref_option())
295+
.execute_operation(list_collections, Some(&mut *session))
296296
.await
297297
.map(|spec| SessionCursor::new(self.client().clone(), spec))?;
298298

src/test/spec/v2_runner/mod.rs

+7-17
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@ pub mod test_file;
55
use std::{ops::Deref, time::Duration};
66

77
use semver::VersionReq;
8-
use tokio::sync::RwLockWriteGuard;
98

109
use crate::{
1110
bson::doc,
1211
coll::options::DropCollectionOptions,
1312
concern::{Acknowledgment, WriteConcern},
1413
options::{CreateCollectionOptions, InsertManyOptions},
15-
test::{assert_matches, util::get_default_name, EventClient, TestClient, LOCK},
14+
test::{assert_matches, util::get_default_name, EventClient, TestClient},
1615
RUNTIME,
1716
};
1817

1918
use operation::{OperationObject, OperationResult};
2019
use test_event::CommandStartedEvent;
2120
use test_file::{TestData, TestFile};
2221

23-
use super::run_spec_test;
24-
2522
const SKIPPED_OPERATIONS: &[&str] = &[
2623
"bulkWrite",
2724
"count",
@@ -35,13 +32,6 @@ const SKIPPED_OPERATIONS: &[&str] = &[
3532
"watch",
3633
];
3734

38-
#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))]
39-
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
40-
async fn run_crud_v2_tests() {
41-
let _guard: RwLockWriteGuard<()> = LOCK.run_exclusively().await;
42-
run_spec_test(&["crud", "v2"], run_v2_test).await;
43-
}
44-
4535
pub async fn run_v2_test(test_file: TestFile) {
4636
let client = TestClient::new().await;
4737

@@ -300,15 +290,15 @@ fn majority_write_concern() -> WriteConcern {
300290
}
301291

302292
fn assert_different_lsid_on_last_two_commands(client: &EventClient) {
303-
let (e1, e2) = client.get_last_two_command_started_events();
304-
let lsid1 = e1.command.get("lsid").unwrap();
305-
let lsid2 = e2.command.get("lsid").unwrap();
293+
let events = client.get_all_command_started_events();
294+
let lsid1 = events[events.len() - 1].command.get("lsid").unwrap();
295+
let lsid2 = events[events.len() - 2].command.get("lsid").unwrap();
306296
assert_ne!(lsid1, lsid2);
307297
}
308298

309299
fn assert_same_lsid_on_last_two_commands(client: &EventClient) {
310-
let (e1, e2) = client.get_last_two_command_started_events();
311-
let lsid1 = e1.command.get("lsid").unwrap();
312-
let lsid2 = e2.command.get("lsid").unwrap();
300+
let events = client.get_all_command_started_events();
301+
let lsid1 = events[events.len() - 1].command.get("lsid").unwrap();
302+
let lsid2 = events[events.len() - 2].command.get("lsid").unwrap();
313303
assert_eq!(lsid1, lsid2);
314304
}

src/test/spec/v2_runner/test_event.rs

+9-27
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,19 @@ impl CommandStartedEvent {
2323
if expected.database_name.is_some() && self.database_name != expected.database_name {
2424
return false;
2525
}
26-
for (k, v) in &expected.command {
27-
if k == "lsid" {
28-
match v.as_str().unwrap() {
29-
"session0" => {
30-
if self.command.get(k).unwrap().as_document().unwrap() != session0_lsid {
31-
return false;
32-
}
33-
}
34-
"session1" => {
35-
if self.command.get(k).unwrap().as_document().unwrap() != session1_lsid {
36-
return false;
37-
}
38-
}
39-
other => panic!("unknown session name: {}", other),
26+
let mut expected = expected.command.clone();
27+
if let Some(Bson::String(session)) = expected.remove("lsid") {
28+
match session.as_str() {
29+
"session0" => {
30+
expected.insert("lsid", session0_lsid.clone());
4031
}
41-
} else {
42-
match self.command.get(k) {
43-
Some(actual_v) => {
44-
if !actual_v.matches(v) {
45-
return false;
46-
}
47-
}
48-
None => {
49-
if v != &Bson::Null {
50-
return false;
51-
}
52-
}
32+
"session1" => {
33+
expected.insert("lsid", session1_lsid.clone());
5334
}
35+
other => panic!("unknown session name: {}", other),
5436
}
5537
}
56-
true
38+
self.command.content_matches(&expected)
5739
}
5840
}
5941

src/test/util/event.rs

+4-24
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,10 @@ impl EventClient {
330330
events
331331
.iter()
332332
.filter_map(|event| match event {
333-
CommandEvent::CommandStartedEvent(event) => {
334-
if event.command_name != "configureFailPoint" {
335-
Some(event.clone())
336-
} else {
337-
None
338-
}
333+
CommandEvent::CommandStartedEvent(event)
334+
if event.command_name != "configureFailPoint" =>
335+
{
336+
Some(event.clone())
339337
}
340338
_ => None,
341339
})
@@ -414,24 +412,6 @@ impl EventClient {
414412
self.handler.command_events.write().unwrap().clear();
415413
self.handler.pool_cleared_events.write().unwrap().clear();
416414
}
417-
418-
/// Gets the last two command started events.
419-
pub fn get_last_two_command_started_events(
420-
&self,
421-
) -> (CommandStartedEvent, CommandStartedEvent) {
422-
let events = self.handler.command_events.read().unwrap();
423-
let events: Vec<CommandStartedEvent> = events
424-
.iter()
425-
.filter_map(|event| match event {
426-
CommandEvent::CommandStartedEvent(event) => Some(event.clone()),
427-
_ => None,
428-
})
429-
.collect();
430-
(
431-
events[events.len() - 2].clone(),
432-
events[events.len() - 1].clone(),
433-
)
434-
}
435415
}
436416

437417
#[cfg_attr(feature = "tokio-runtime", tokio::test)]

src/test/util/matchable.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ impl Matchable for Document {
6262
if k == "upsertedCount" {
6363
continue;
6464
}
65-
if let Some(actual_v) = self.get(k) {
66-
if !actual_v.matches(v) {
67-
return false;
65+
match self.get(k) {
66+
Some(actual_v) => {
67+
if !actual_v.matches(v) {
68+
return false;
69+
}
70+
}
71+
None => {
72+
if v != &Bson::Null {
73+
return false;
74+
}
6875
}
69-
} else {
70-
return false;
7176
}
7277
}
7378
true

0 commit comments

Comments
 (0)