Skip to content

Commit c56ff72

Browse files
committed
test: add test for hasNext not consuming first document in stream
1 parent 76333fc commit c56ff72

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

test/functional/cursor.test.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const expect = require('chai').expect;
66
const Long = require('bson').Long;
77
const sinon = require('sinon');
88
const Buffer = require('safe-buffer').Buffer;
9+
const Writable = require('stream').Writable;
910

1011
const core = require('../../lib/core');
1112
const ReadPreference = core.ReadPreference;
@@ -4371,11 +4372,15 @@ describe('Cursor', function() {
43714372
const client = configuration.newClient({ w: 1 }, { poolSize: 1, auto_reconnect: false });
43724373

43734374
client.connect(function(err, client) {
4375+
expect(err).to.not.exist;
43744376
const db = client.db(configuration.db);
43754377
const collection = db.collection('cursor_session_tests2');
43764378

43774379
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+
43794384
cursor.close(() => client.close(() => done()));
43804385
});
43814386
});
@@ -4523,4 +4528,45 @@ describe('Cursor', function() {
45234528
.catch(e => close(e));
45244529
});
45254530
});
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+
});
45264572
});

0 commit comments

Comments
 (0)