Skip to content

Commit

Permalink
test: add test for hasNext not consuming first document in stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 15, 2020
1 parent 76333fc commit c56ff72
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion test/functional/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const expect = require('chai').expect;
const Long = require('bson').Long;
const sinon = require('sinon');
const Buffer = require('safe-buffer').Buffer;
const Writable = require('stream').Writable;

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

client.connect(function(err, client) {
expect(err).to.not.exist;
const db = client.db(configuration.db);
const collection = db.collection('cursor_session_tests2');

const cursor = collection.find();
expect(cursor.forEach()).to.exist.and.to.be.an.instanceof(cursor.s.promiseLibrary);
const promise = cursor.forEach();
expect(promise).to.exist.and.to.be.an.instanceof(cursor.s.promiseLibrary);
promise.catch(() => {});

cursor.close(() => client.close(() => done()));
});
});
Expand Down Expand Up @@ -4523,4 +4528,45 @@ describe('Cursor', function() {
.catch(e => close(e));
});
});

it('should not consume first document on hasNext when streaming', function(done) {
const configuration = this.configuration;
const client = configuration.newClient({ w: 1 }, { poolSize: 1, auto_reconnect: false });

client.connect(err => {
expect(err).to.not.exist;
this.defer(() => client.close());

const collection = client.db().collection('documents');
collection.drop(() => {
const docs = [{ a: 1 }, { a: 2 }, { a: 3 }];
collection.insertMany(docs, err => {
expect(err).to.not.exist;

const cursor = collection.find({}, { sort: { a: 1 } });
cursor.hasNext((err, hasNext) => {
expect(err).to.not.exist;
expect(hasNext).to.be.true;

const collected = [];
const stream = new Writable({
objectMode: true,
write: (chunk, encoding, next) => {
collected.push(chunk);
next(undefined, chunk);
}
});

cursor.on('close', () => {
expect(collected).to.have.length(3);
expect(collected).to.eql(docs);
done();
});

cursor.pipe(stream);
});
});
});
});
});
});

0 comments on commit c56ff72

Please sign in to comment.