Skip to content

Commit 88478f6

Browse files
authored
RUST-865 Verify correct cursor behavior with SBE (#456)
1 parent 81f1602 commit 88478f6

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

src/test/cursor.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::time::Duration;
22

3-
use futures::{future::Either, StreamExt};
3+
use futures::{future::Either, StreamExt, TryStreamExt};
44
use tokio::sync::RwLockReadGuard;
55

66
use crate::{
77
bson::doc,
88
options::{CreateCollectionOptions, CursorType, FindOptions},
9-
test::{TestClient, LOCK},
9+
test::{util::EventClient, TestClient, LOCK},
1010
RUNTIME,
1111
};
1212

@@ -109,3 +109,50 @@ async fn session_cursor_next() {
109109
);
110110
}
111111
}
112+
113+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
114+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
115+
async fn batch_exhaustion() {
116+
let _guard: RwLockReadGuard<()> = LOCK.run_concurrently().await;
117+
let client = EventClient::new().await;
118+
119+
let coll = client
120+
.create_fresh_collection(
121+
"cursor_batch_exhaustion_db",
122+
"cursor_batch_exhaustion_coll",
123+
None,
124+
)
125+
.await;
126+
coll.insert_many(
127+
vec![
128+
doc! { "foo": 1 },
129+
doc! { "foo": 2 },
130+
doc! { "foo": 3 },
131+
doc! { "foo": 4 },
132+
doc! { "foo": 5 },
133+
doc! { "foo": 6 },
134+
],
135+
None,
136+
)
137+
.await
138+
.unwrap();
139+
140+
// Start a find where batch size will line up with limit.
141+
let cursor = coll
142+
.find(None, FindOptions::builder().batch_size(2).limit(4).build())
143+
.await
144+
.unwrap();
145+
let v: Vec<_> = cursor.try_collect().await.unwrap();
146+
assert_eq!(4, v.len());
147+
148+
// Assert that the last `getMore` response always has id 0, i.e. is exhausted.
149+
let replies: Vec<_> = client
150+
.get_command_events(&["getMore"])
151+
.into_iter()
152+
.filter_map(|e| e.as_command_succeeded().map(|e| e.reply.clone()))
153+
.collect();
154+
let last = replies.last().unwrap();
155+
let cursor = last.get_document("cursor").unwrap();
156+
let id = cursor.get_i64("id").unwrap();
157+
assert_eq!(0, id);
158+
}

src/test/spec/command_monitoring/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async fn run_command_monitoring_test(test_file: TestFile) {
4747
"A successful mixed bulk write",
4848
"A successful unordered bulk write with an unacknowledged write concern",
4949
// We can't pass this test since it relies on old OP_QUERY behavior (SPEC-1519)
50-
"A successful find event with a getmore and the server kills the cursor",
50+
"A successful find event with a getmore and the server kills the cursor (<= 4.4)",
5151
];
5252

5353
for test_case in test_file.tests {

src/test/spec/json/command-monitoring/legacy/find.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@
413413
]
414414
},
415415
{
416-
"description": "A successful find event with a getmore and the server kills the cursor",
416+
"description": "A successful find event with a getmore and the server kills the cursor (<= 4.4)",
417417
"ignore_if_server_version_less_than": "3.1",
418+
"ignore_if_server_version_greater_than": "4.4",
418419
"ignore_if_topology_type": [
419420
"sharded"
420421
],

src/test/spec/json/command-monitoring/legacy/find.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ tests:
4343
filter: { _id: { $gt: 1 } }
4444
sort: { _id: 1 }
4545
skip: {"$numberLong": "2"}
46-
modifiers:
47-
$comment: "test"
48-
$hint: { _id: 1 }
49-
$max: { _id: 6 }
50-
$maxTimeMS: 6000
51-
$min: { _id: 0 }
52-
$returnKey: false
53-
$showDiskLoc: false
46+
comment: "test"
47+
hint: { _id: 1 }
48+
max: { _id: 6 }
49+
maxTimeMS: 6000
50+
min: { _id: 0 }
51+
returnKey: false
52+
showRecordId: false
5453
expectations:
5554
-
5655
command_started_event:
@@ -195,8 +194,9 @@ tests:
195194
- { $numberLong: "42" }
196195
command_name: "killCursors"
197196
-
198-
description: "A successful find event with a getmore and the server kills the cursor"
197+
description: "A successful find event with a getmore and the server kills the cursor (<= 4.4)"
199198
ignore_if_server_version_less_than: "3.1"
199+
ignore_if_server_version_greater_than: "4.4"
200200
ignore_if_topology_type:
201201
- "sharded"
202202
operation:

src/test/util/event.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ impl CommandEvent {
100100
}
101101
}
102102

103-
fn as_command_started(&self) -> Option<&CommandStartedEvent> {
103+
pub fn as_command_started(&self) -> Option<&CommandStartedEvent> {
104104
match self {
105105
CommandEvent::Started(e) => Some(e),
106106
_ => None,
107107
}
108108
}
109109

110-
fn as_command_succeeded(&self) -> Option<&CommandSucceededEvent> {
110+
pub fn as_command_succeeded(&self) -> Option<&CommandSucceededEvent> {
111111
match self {
112112
CommandEvent::Succeeded(e) => Some(e),
113113
_ => None,

0 commit comments

Comments
 (0)