@@ -294,4 +294,58 @@ describe('Find Cursor', function () {
294
294
} )
295
295
} ) ;
296
296
} ) ;
297
+
298
+ context ( '#allowDiskUse' , function ( ) {
299
+ it ( 'should set allowDiskUse to true by default' , {
300
+ metadata : { requires : { mongodb : '>=4.4' } } ,
301
+ test : withClientV2 ( function ( client , done ) {
302
+ const commands = [ ] ;
303
+ client . on ( 'commandStarted' , filterForCommands ( [ 'find' ] , commands ) ) ;
304
+
305
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
306
+ const cursor = coll . find ( { } , { sort : 'foo' } ) ;
307
+ cursor . allowDiskUse ( ) ;
308
+ this . defer ( ( ) => cursor . close ( ) ) ;
309
+
310
+ cursor . toArray ( err => {
311
+ expect ( err ) . to . not . exist ;
312
+ expect ( commands ) . to . have . length ( 1 ) ;
313
+ expect ( commands [ 0 ] . command . allowDiskUse ) . to . equal ( true ) ;
314
+ done ( ) ;
315
+ } ) ;
316
+ } )
317
+ } ) ;
318
+
319
+ it ( 'should set allowDiskUse to false if specified' , {
320
+ metadata : { requires : { mongodb : '>=4.4' } } ,
321
+ test : withClientV2 ( function ( client , done ) {
322
+ const commands = [ ] ;
323
+ client . on ( 'commandStarted' , filterForCommands ( [ 'find' ] , commands ) ) ;
324
+
325
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
326
+ const cursor = coll . find ( { } , { sort : 'foo' } ) ;
327
+ cursor . allowDiskUse ( false ) ;
328
+ this . defer ( ( ) => cursor . close ( ) ) ;
329
+
330
+ cursor . toArray ( err => {
331
+ expect ( err ) . to . not . exist ;
332
+ expect ( commands ) . to . have . length ( 1 ) ;
333
+ expect ( commands [ 0 ] . command . allowDiskUse ) . to . equal ( false ) ;
334
+ done ( ) ;
335
+ } ) ;
336
+ } )
337
+ } ) ;
338
+
339
+ it ( 'throws if the query does not have sort specified' , {
340
+ metadata : { requires : { mongodb : '>=4.4' } } ,
341
+ test : withClientV2 ( function ( client , done ) {
342
+ const coll = client . db ( ) . collection ( 'abstract_cursor' ) ;
343
+ const cursor = coll . find ( { } ) ;
344
+ expect ( ( ) => cursor . allowDiskUse ( false ) ) . to . throw (
345
+ 'Option "allowDiskUse" requires a sort specification'
346
+ ) ;
347
+ done ( ) ;
348
+ } )
349
+ } ) ;
350
+ } ) ;
297
351
} ) ;
0 commit comments