@@ -6,7 +6,7 @@ describe('AbstractCursor', function () {
6
6
before (
7
7
withClientV2 ( ( client , done ) => {
8
8
const docs = [ { a : 1 } , { a : 2 } , { a : 3 } , { a : 4 } , { a : 5 } , { a : 6 } ] ;
9
- const coll = client . db ( ) . collection ( 'find_cursor ' ) ;
9
+ const coll = client . db ( ) . collection ( 'abstract_cursor ' ) ;
10
10
const tryNextColl = client . db ( ) . collection ( 'try_next' ) ;
11
11
coll . drop ( ( ) => tryNextColl . drop ( ( ) => coll . insertMany ( docs , done ) ) ) ;
12
12
} )
@@ -19,7 +19,7 @@ describe('AbstractCursor', function () {
19
19
const commands = [ ] ;
20
20
client . on ( 'commandStarted' , filterForCommands ( [ 'getMore' ] , commands ) ) ;
21
21
22
- const coll = client . db ( ) . collection ( 'find_cursor ' ) ;
22
+ const coll = client . db ( ) . collection ( 'abstract_cursor ' ) ;
23
23
const cursor = coll . find ( { } , { batchSize : 2 } ) ;
24
24
this . defer ( ( ) => cursor . close ( ) ) ;
25
25
@@ -40,7 +40,7 @@ describe('AbstractCursor', function () {
40
40
const commands = [ ] ;
41
41
client . on ( 'commandStarted' , filterForCommands ( [ 'killCursors' ] , commands ) ) ;
42
42
43
- const coll = client . db ( ) . collection ( 'find_cursor ' ) ;
43
+ const coll = client . db ( ) . collection ( 'abstract_cursor ' ) ;
44
44
const cursor = coll . find ( { } , { batchSize : 2 } ) ;
45
45
cursor . next ( err => {
46
46
expect ( err ) . to . not . exist ;
@@ -59,7 +59,7 @@ describe('AbstractCursor', function () {
59
59
const commands = [ ] ;
60
60
client . on ( 'commandStarted' , filterForCommands ( [ 'killCursors' ] , commands ) ) ;
61
61
62
- const coll = client . db ( ) . collection ( 'find_cursor ' ) ;
62
+ const coll = client . db ( ) . collection ( 'abstract_cursor ' ) ;
63
63
const cursor = coll . find ( { } , { batchSize : 2 } ) ;
64
64
cursor . toArray ( err => {
65
65
expect ( err ) . to . not . exist ;
@@ -79,7 +79,7 @@ describe('AbstractCursor', function () {
79
79
const commands = [ ] ;
80
80
client . on ( 'commandStarted' , filterForCommands ( [ 'killCursors' ] , commands ) ) ;
81
81
82
- const coll = client . db ( ) . collection ( 'find_cursor ' ) ;
82
+ const coll = client . db ( ) . collection ( 'abstract_cursor ' ) ;
83
83
const cursor = coll . find ( { } , { batchSize : 2 } ) ;
84
84
cursor . close ( err => {
85
85
expect ( err ) . to . not . exist ;
@@ -94,7 +94,7 @@ describe('AbstractCursor', function () {
94
94
it (
95
95
'should iterate each document in a cursor' ,
96
96
withClientV2 ( function ( client , done ) {
97
- const coll = client . db ( ) . collection ( 'find_cursor ' ) ;
97
+ const coll = client . db ( ) . collection ( 'abstract_cursor ' ) ;
98
98
const cursor = coll . find ( { } , { batchSize : 2 } ) ;
99
99
100
100
const bag = [ ] ;
@@ -143,4 +143,123 @@ describe('AbstractCursor', function () {
143
143
} )
144
144
) ;
145
145
} ) ;
146
+
147
+ context ( '#clone' , function ( ) {
148
+ it (
149
+ 'should clone a find cursor' ,
150
+ withClientV2 ( function ( client , done ) {
151
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
152
+ const cursor = coll . find ( { } ) ;
153
+ this . defer ( ( ) => cursor . close ( ) ) ;
154
+
155
+ cursor . toArray ( ( err , docs ) => {
156
+ expect ( err ) . to . not . exist ;
157
+ expect ( docs ) . to . have . length ( 6 ) ;
158
+ expect ( cursor ) . property ( 'closed' ) . to . be . true ;
159
+
160
+ const clonedCursor = cursor . clone ( ) ;
161
+ this . defer ( ( ) => clonedCursor . close ( ) ) ;
162
+
163
+ clonedCursor . toArray ( ( err , docs ) => {
164
+ expect ( err ) . to . not . exist ;
165
+ expect ( docs ) . to . have . length ( 6 ) ;
166
+ expect ( clonedCursor ) . property ( 'closed' ) . to . be . true ;
167
+ done ( ) ;
168
+ } ) ;
169
+ } ) ;
170
+ } )
171
+ ) ;
172
+
173
+ it (
174
+ 'should clone an aggregate cursor' ,
175
+ withClientV2 ( function ( client , done ) {
176
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
177
+ const cursor = coll . aggregate ( [ { $match : { } } ] ) ;
178
+ this . defer ( ( ) => cursor . close ( ) ) ;
179
+
180
+ cursor . toArray ( ( err , docs ) => {
181
+ expect ( err ) . to . not . exist ;
182
+ expect ( docs ) . to . have . length ( 6 ) ;
183
+ expect ( cursor ) . property ( 'closed' ) . to . be . true ;
184
+
185
+ const clonedCursor = cursor . clone ( ) ;
186
+ this . defer ( ( ) => clonedCursor . close ( ) ) ;
187
+
188
+ clonedCursor . toArray ( ( err , docs ) => {
189
+ expect ( err ) . to . not . exist ;
190
+ expect ( docs ) . to . have . length ( 6 ) ;
191
+ expect ( clonedCursor ) . property ( 'closed' ) . to . be . true ;
192
+ done ( ) ;
193
+ } ) ;
194
+ } ) ;
195
+ } )
196
+ ) ;
197
+ } ) ;
198
+
199
+ context ( '#rewind' , function ( ) {
200
+ it (
201
+ 'should rewind a cursor' ,
202
+ withClientV2 ( function ( client , done ) {
203
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
204
+ const cursor = coll . find ( { } ) ;
205
+ this . defer ( ( ) => cursor . close ( ) ) ;
206
+
207
+ cursor . toArray ( ( err , docs ) => {
208
+ expect ( err ) . to . not . exist ;
209
+ expect ( docs ) . to . have . length ( 6 ) ;
210
+
211
+ cursor . rewind ( ) ;
212
+ cursor . toArray ( ( err , docs ) => {
213
+ expect ( err ) . to . not . exist ;
214
+ expect ( docs ) . to . have . length ( 6 ) ;
215
+
216
+ done ( ) ;
217
+ } ) ;
218
+ } ) ;
219
+ } )
220
+ ) ;
221
+
222
+ it ( 'should end an implicit session on rewind' , {
223
+ metadata : { requires : { mongodb : '>=3.6' } } ,
224
+ test : withClientV2 ( function ( client , done ) {
225
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
226
+ const cursor = coll . find ( { } , { batchSize : 1 } ) ;
227
+ this . defer ( ( ) => cursor . close ( ) ) ;
228
+
229
+ cursor . next ( ( err , doc ) => {
230
+ expect ( err ) . to . not . exist ;
231
+ expect ( doc ) . to . exist ;
232
+
233
+ const session = cursor . session ;
234
+ expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
235
+ cursor . rewind ( ) ;
236
+ expect ( session ) . property ( 'hasEnded' ) . to . be . true ;
237
+ done ( ) ;
238
+ } ) ;
239
+ } )
240
+ } ) ;
241
+
242
+ it ( 'should not end an explicit session on rewind' , {
243
+ metadata : { requires : { mongodb : '>=3.6' } } ,
244
+ test : withClientV2 ( function ( client , done ) {
245
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
246
+ const session = client . startSession ( ) ;
247
+
248
+ const cursor = coll . find ( { } , { batchSize : 1 , session } ) ;
249
+ this . defer ( ( ) => cursor . close ( ) ) ;
250
+
251
+ cursor . next ( ( err , doc ) => {
252
+ expect ( err ) . to . not . exist ;
253
+ expect ( doc ) . to . exist ;
254
+
255
+ const session = cursor . session ;
256
+ expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
257
+ cursor . rewind ( ) ;
258
+ expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
259
+
260
+ session . endSession ( done ) ;
261
+ } ) ;
262
+ } )
263
+ } ) ;
264
+ } ) ;
146
265
} ) ;
0 commit comments