@@ -6,6 +6,7 @@ const expect = require('chai').expect;
6
6
const Long = require ( 'bson' ) . Long ;
7
7
const sinon = require ( 'sinon' ) ;
8
8
const Buffer = require ( 'safe-buffer' ) . Buffer ;
9
+ const Writable = require ( 'stream' ) . Writable ;
9
10
10
11
const core = require ( '../../lib/core' ) ;
11
12
const ReadPreference = core . ReadPreference ;
@@ -4371,11 +4372,15 @@ describe('Cursor', function() {
4371
4372
const client = configuration . newClient ( { w : 1 } , { poolSize : 1 , auto_reconnect : false } ) ;
4372
4373
4373
4374
client . connect ( function ( err , client ) {
4375
+ expect ( err ) . to . not . exist ;
4374
4376
const db = client . db ( configuration . db ) ;
4375
4377
const collection = db . collection ( 'cursor_session_tests2' ) ;
4376
4378
4377
4379
const cursor = collection . find ( ) ;
4378
- expect ( cursor . forEach ( ) ) . to . exist . and . to . be . an . instanceof ( cursor . s . promiseLibrary ) ;
4380
+ const promise = cursor . forEach ( ) ;
4381
+ expect ( promise ) . to . exist . and . to . be . an . instanceof ( cursor . s . promiseLibrary ) ;
4382
+ promise . catch ( ( ) => { } ) ;
4383
+
4379
4384
cursor . close ( ( ) => client . close ( ( ) => done ( ) ) ) ;
4380
4385
} ) ;
4381
4386
} ) ;
@@ -4523,4 +4528,45 @@ describe('Cursor', function() {
4523
4528
. catch ( e => close ( e ) ) ;
4524
4529
} ) ;
4525
4530
} ) ;
4531
+
4532
+ it ( 'should not consume first document on hasNext when streaming' , function ( done ) {
4533
+ const configuration = this . configuration ;
4534
+ const client = configuration . newClient ( { w : 1 } , { poolSize : 1 , auto_reconnect : false } ) ;
4535
+
4536
+ client . connect ( err => {
4537
+ expect ( err ) . to . not . exist ;
4538
+ this . defer ( ( ) => client . close ( ) ) ;
4539
+
4540
+ const collection = client . db ( ) . collection ( 'documents' ) ;
4541
+ collection . drop ( ( ) => {
4542
+ const docs = [ { a : 1 } , { a : 2 } , { a : 3 } ] ;
4543
+ collection . insertMany ( docs , err => {
4544
+ expect ( err ) . to . not . exist ;
4545
+
4546
+ const cursor = collection . find ( { } , { sort : { a : 1 } } ) ;
4547
+ cursor . hasNext ( ( err , hasNext ) => {
4548
+ expect ( err ) . to . not . exist ;
4549
+ expect ( hasNext ) . to . be . true ;
4550
+
4551
+ const collected = [ ] ;
4552
+ const stream = new Writable ( {
4553
+ objectMode : true ,
4554
+ write : ( chunk , encoding , next ) => {
4555
+ collected . push ( chunk ) ;
4556
+ next ( undefined , chunk ) ;
4557
+ }
4558
+ } ) ;
4559
+
4560
+ cursor . on ( 'close' , ( ) => {
4561
+ expect ( collected ) . to . have . length ( 3 ) ;
4562
+ expect ( collected ) . to . eql ( docs ) ;
4563
+ done ( ) ;
4564
+ } ) ;
4565
+
4566
+ cursor . pipe ( stream ) ;
4567
+ } ) ;
4568
+ } ) ;
4569
+ } ) ;
4570
+ } ) ;
4571
+ } ) ;
4526
4572
} ) ;
0 commit comments