11import { expect } from 'chai' ;
22
33import {
4- CursorTimeoutContext ,
4+ type Collection ,
5+ type FindCursor ,
56 MongoAPIError ,
6- MongoCursorExhaustedError ,
7- promiseWithResolvers ,
8- TimeoutContext
9- } from '../../mongodb' ;
7+ type MongoClient ,
8+ MongoCursorExhaustedError
9+ } from '../../../src' ;
10+ import { CursorTimeoutContext } from '../../../src/cursor/abstract_cursor' ;
11+ import { TimeoutContext } from '../../../src/timeout' ;
12+ import { promiseWithResolvers } from '../../../src/utils' ;
1013import { filterForCommands } from '../shared' ;
1114
1215describe ( 'Find Cursor' , function ( ) {
13- let client ;
16+ let client : MongoClient ;
1417
1518 beforeEach ( async function ( ) {
1619 const setupClient = this . configuration . newClient ( ) ;
@@ -32,20 +35,16 @@ describe('Find Cursor', function () {
3235 } ) ;
3336
3437 context ( '#next' , function ( ) {
35- it ( 'should support a batch size' , function ( done ) {
38+ it ( 'should support a batch size' , async function ( ) {
3639 const commands = [ ] ;
3740 client . on ( 'commandStarted' , filterForCommands ( [ 'getMore' ] , commands ) ) ;
3841
3942 const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
4043 const cursor = coll . find ( { } , { batchSize : 2 } ) ;
41- this . defer ( ( ) => cursor . close ( ) ) ;
4244
43- cursor . toArray ( ( err , docs ) => {
44- expect ( err ) . to . not . exist ;
45- expect ( docs ) . to . have . length ( 6 ) ;
46- expect ( commands ) . to . have . length ( 3 ) ;
47- done ( ) ;
48- } ) ;
45+ const docs = await cursor . toArray ( ) ;
46+ expect ( docs ) . to . have . length ( 6 ) ;
47+ expect ( commands ) . to . have . length ( 3 ) ;
4948 } ) ;
5049 } ) ;
5150
@@ -173,51 +172,36 @@ describe('Find Cursor', function () {
173172 } ) ;
174173 } ) ;
175174
176- context ( '#forEach ' , function ( ) {
177- it ( 'should iterate each document in a cursor' , function ( done ) {
175+ context ( 'iterator ' , function ( ) {
176+ it ( 'should iterate each document in a cursor' , async function ( ) {
178177 const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
179178 const cursor = coll . find ( { } , { batchSize : 2 } ) ;
180179
181180 const bag = [ ] ;
182- cursor . forEach (
183- doc => bag . push ( doc ) ,
184- err => {
185- expect ( err ) . to . not . exist ;
186- expect ( bag ) . to . have . lengthOf ( 6 ) ;
187- done ( ) ;
188- }
189- ) ;
181+ for await ( const doc of cursor ) {
182+ bag . push ( doc ) ;
183+ }
184+ expect ( bag ) . to . have . lengthOf ( 6 ) ;
190185 } ) ;
191186 } ) ;
192187
193188 context ( '#tryNext' , function ( ) {
194- it ( 'should return control to the user if an empty batch is returned' , function ( done ) {
189+ it ( 'should return control to the user if an empty batch is returned' , async function ( ) {
195190 const db = client . db ( ) ;
196- db . createCollection ( 'try_next' , { capped : true , size : 10000000 } , ( ) => {
197- const coll = db . collection ( 'try_next' ) ;
198- coll . insertMany ( [ { } , { } ] , err => {
199- expect ( err ) . to . not . exist ;
200-
201- const cursor = coll . find ( { } , { tailable : true , awaitData : true } ) ;
202- this . defer ( ( ) => cursor . close ( ) ) ;
203-
204- cursor . tryNext ( ( err , doc ) => {
205- expect ( err ) . to . not . exist ;
206- expect ( doc ) . to . exist ;
207-
208- cursor . tryNext ( ( err , doc ) => {
209- expect ( err ) . to . not . exist ;
210- expect ( doc ) . to . exist ;
211-
212- cursor . tryNext ( ( err , doc ) => {
213- expect ( err ) . to . not . exist ;
214- expect ( doc ) . to . be . null ;
215- done ( ) ;
216- } ) ;
217- } ) ;
218- } ) ;
219- } ) ;
220- } ) ;
191+ await db . createCollection ( 'try_next' , { capped : true , size : 10000000 } ) ;
192+ const coll = db . collection ( 'try_next' ) ;
193+ await coll . insertMany ( [ { } , { } ] ) ;
194+
195+ const cursor = coll . find ( { } , { tailable : true , awaitData : true } ) ;
196+
197+ const doc1 = await cursor . tryNext ( ) ;
198+ expect ( doc1 ) . to . exist ;
199+
200+ const doc2 = await cursor . tryNext ( ) ;
201+ expect ( doc2 ) . to . exist ;
202+
203+ const doc3 = await cursor . tryNext ( ) ;
204+ expect ( doc3 ) . to . be . null ;
221205 } ) ;
222206 } ) ;
223207
@@ -293,51 +277,36 @@ describe('Find Cursor', function () {
293277 }
294278 } ) ;
295279
296- it ( 'should end an implicit session on rewind' , {
297- metadata : { requires : { mongodb : '>=3.6' } } ,
298- test : function ( done ) {
299- const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
300- const cursor = coll . find ( { } , { batchSize : 1 } ) ;
301- this . defer ( ( ) => cursor . close ( ) ) ;
302-
303- cursor . next ( ( err , doc ) => {
304- expect ( err ) . to . not . exist ;
305- expect ( doc ) . to . exist ;
306-
307- const session = cursor . session ;
308- expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
309- cursor . rewind ( ) ;
310- expect ( session ) . property ( 'hasEnded' ) . to . be . true ;
311- done ( ) ;
312- } ) ;
313- }
314- } ) ;
280+ it ( 'should end an implicit session on rewind' , async function ( ) {
281+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
282+ const cursor = coll . find ( { } , { batchSize : 1 } ) ;
315283
316- it ( 'should not end an explicit session on rewind' , {
317- metadata : { requires : { mongodb : '>=3.6' } } ,
318- test : function ( done ) {
319- const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
320- const session = client . startSession ( ) ;
284+ const doc = await cursor . next ( ) ;
285+ expect ( doc ) . to . exist ;
321286
322- const cursor = coll . find ( { } , { batchSize : 1 , session } ) ;
323- this . defer ( ( ) => cursor . close ( ) ) ;
287+ const session = cursor . session ;
288+ expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
289+ cursor . rewind ( ) ;
290+ expect ( session ) . property ( 'hasEnded' ) . to . be . true ;
291+ } ) ;
324292
325- cursor . next ( ( err , doc ) => {
326- expect ( err ) . to . not . exist ;
327- expect ( doc ) . to . exist ;
293+ it ( 'should not end an explicit session on rewind' , async function ( ) {
294+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
295+ const cursor = coll . find ( { } , { batchSize : 1 , session : client . startSession ( ) } ) ;
328296
329- const session = cursor . session ;
330- expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
331- cursor . rewind ( ) ;
332- expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
297+ const doc = await cursor . next ( ) ;
298+ expect ( doc ) . to . exist ;
333299
334- session . endSession ( done ) ;
335- } ) ;
336- }
300+ const session = cursor . session ;
301+ expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
302+ cursor . rewind ( ) ;
303+ expect ( session ) . property ( 'hasEnded' ) . to . be . false ;
304+
305+ await session . endSession ( ) ;
337306 } ) ;
338307
339308 it ( 'emits close after rewind' , async ( ) => {
340- let cursor ;
309+ let cursor : FindCursor ;
341310 try {
342311 const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
343312 cursor = coll . find ( { } , { batchSize : 1 } ) ;
@@ -359,41 +328,33 @@ describe('Find Cursor', function () {
359328 context ( '#allowDiskUse' , function ( ) {
360329 it ( 'should set allowDiskUse to true by default' , {
361330 metadata : { requires : { mongodb : '>=4.4' } } ,
362- test : function ( done ) {
331+ test : async function ( ) {
363332 const commands = [ ] ;
364333 client . on ( 'commandStarted' , filterForCommands ( [ 'find' ] , commands ) ) ;
365334
366335 const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
367336 const cursor = coll . find ( { } , { sort : 'foo' } ) ;
368337 cursor . allowDiskUse ( ) ;
369- this . defer ( ( ) => cursor . close ( ) ) ;
370338
371- cursor . toArray ( err => {
372- expect ( err ) . to . not . exist ;
373- expect ( commands ) . to . have . length ( 1 ) ;
374- expect ( commands [ 0 ] . command . allowDiskUse ) . to . equal ( true ) ;
375- done ( ) ;
376- } ) ;
339+ await cursor . toArray ( ) ;
340+ expect ( commands ) . to . have . length ( 1 ) ;
341+ expect ( commands [ 0 ] . command . allowDiskUse ) . to . equal ( true ) ;
377342 }
378343 } ) ;
379344
380345 it ( 'should set allowDiskUse to false if specified' , {
381346 metadata : { requires : { mongodb : '>=4.4' } } ,
382- test : function ( done ) {
347+ test : async function ( ) {
383348 const commands = [ ] ;
384349 client . on ( 'commandStarted' , filterForCommands ( [ 'find' ] , commands ) ) ;
385350
386351 const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
387352 const cursor = coll . find ( { } , { sort : 'foo' } ) ;
388353 cursor . allowDiskUse ( false ) ;
389- this . defer ( ( ) => cursor . close ( ) ) ;
390354
391- cursor . toArray ( err => {
392- expect ( err ) . to . not . exist ;
393- expect ( commands ) . to . have . length ( 1 ) ;
394- expect ( commands [ 0 ] . command . allowDiskUse ) . to . equal ( false ) ;
395- done ( ) ;
396- } ) ;
355+ await cursor . toArray ( ) ;
356+ expect ( commands ) . to . have . length ( 1 ) ;
357+ expect ( commands [ 0 ] . command . allowDiskUse ) . to . equal ( false ) ;
397358 }
398359 } ) ;
399360
@@ -411,9 +372,9 @@ describe('Find Cursor', function () {
411372 } ) ;
412373
413374 describe ( 'mixing iteration APIs' , function ( ) {
414- let client ;
415- let collection ;
416- let cursor ;
375+ let client : MongoClient ;
376+ let collection : Collection ;
377+ let cursor : FindCursor ;
417378
418379 beforeEach ( async function ( ) {
419380 client = this . configuration . newClient ( ) ;
@@ -454,7 +415,7 @@ describe('Find Cursor', function () {
454415
455416 await cursor . next ( ) ;
456417
457- let doc ;
418+ let doc : any ;
458419 while ( ( doc = ( await cursor . next ( ) ) && doc != null ) ) {
459420 /** empty */
460421 }
@@ -472,7 +433,7 @@ describe('Find Cursor', function () {
472433
473434 await cursor . next ( ) ;
474435
475- let doc ;
436+ let doc : any ;
476437 while ( ( doc = ( await cursor . next ( ) ) && doc != null ) ) {
477438 /** empty */
478439 }
@@ -671,7 +632,7 @@ describe('Find Cursor', function () {
671632
672633 await cursor . next ( ) ;
673634
674- let doc ;
635+ let doc : any ;
675636 while ( ( doc = ( await cursor . next ( ) ) && doc != null ) ) {
676637 /** empty */
677638 }
0 commit comments