Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions test/integration/crud/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as sinon from 'sinon';

import {
Code,
CursorResponse,
Long,
type MongoClient,
MongoServerError,
Expand Down Expand Up @@ -1075,29 +1074,29 @@ describe('Find', function () {
});

it(
'regression test (NODE-6878): CursorResponse.emptyGetMore contains all CursorResponse fields',
'regression test (NODE-6878): cursor can be rewound after limit-based exhaustion',
{ requires: { topology: 'sharded' } },
async function () {
const collection = client.db('rewind-regression').collection('bar');

await collection.deleteMany({});
await collection.insertMany(Array.from({ length: 4 }, (_, i) => ({ x: i })));

const getMoreSpy = sinon.spy(CursorResponse, 'emptyGetMore', ['get']);

const cursor = collection.find({}, { batchSize: 1, limit: 3 });
// emptyGetMore is used internally after limit + 1 documents have been iterated
// Iterate limit + 1 times to exercise the post-limit cursor path before rewind.
await cursor.next();
await cursor.next();
await cursor.next();
await cursor.next();

// assert that `emptyGetMore` is called. if it is not, this test
// always passes, even without the fix in NODE-6878.
expect(getMoreSpy.get).to.have.been.called;
Comment thread
dariakp marked this conversation as resolved.
// On servers < 9.0, mongos returns a non-zero cursorId even after the limit
// is satisfied, so the driver uses CursorResponse.emptyGetMore to avoid a
// wasted getMore. On 9.0+, mongos returns cursorId: 0 when the limit is
// reached. In both cases, rewinding and re-iterating the cursor must work.

// rewind + toArray must not throw. Before the NODE-6878 fix,
// this would fail with a TypeError from emptyGetMore lacking CursorResponse fields.
cursor.rewind();

await cursor.toArray();
}
);
Expand Down
Loading