diff --git a/test/benchmarks/mongoBench/suites/multiBench.js b/test/benchmarks/mongoBench/suites/multiBench.js index 574ecd27722..9c5e3685761 100644 --- a/test/benchmarks/mongoBench/suites/multiBench.js +++ b/test/benchmarks/mongoBench/suites/multiBench.js @@ -21,7 +21,7 @@ function makeMultiBench(suite) { .setup(initCollection) .setup(makeLoadTweets(false)) .task(async function () { - await this.collection.find({}).toArray(); + await this.collection.find({}).toArray({ batchSize: 1000 }); }) .teardown(dropDb) .teardown(disconnectClient) diff --git a/test/integration/node-specific/abstract_cursor.test.ts b/test/integration/node-specific/abstract_cursor.test.ts index bee2333db94..80be294e91f 100644 --- a/test/integration/node-specific/abstract_cursor.test.ts +++ b/test/integration/node-specific/abstract_cursor.test.ts @@ -5,12 +5,14 @@ import { Transform } from 'stream'; import { inspect } from 'util'; import { + AbstractCursor, type Collection, type FindCursor, MongoAPIError, type MongoClient, MongoCursorExhaustedError, - MongoServerError + MongoServerError, + GetMoreOperation } from '../../mongodb'; describe('class AbstractCursor', function () { @@ -337,6 +339,7 @@ describe('class AbstractCursor', function () { describe('when the last document has been iterated', () => { it('has a zero id and is closed and is never killed', async function () { cursor = client.db().collection('test').find({}); + expect(cursor).to.have.property('closed', false); await cursor.next(); await cursor.next(); await cursor.next(); @@ -361,4 +364,39 @@ describe('class AbstractCursor', function () { }); }); }); + + describe.only('toArray', () => { + let nextSpy; + let getMoreSpy; + let client: MongoClient; + let cursor: AbstractCursor; + let col: Collection; + const numBatches = 10; + const batchSize = 4; + + beforeEach(async function () { + client = this.configuration.newClient(); + col = client.db().collection('test'); + await col.deleteMany({}); + for (let i = 0; i < numBatches; i++) { + await col.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }]); + } + nextSpy = sinon.spy(AbstractCursor.prototype, 'next'); + getMoreSpy = sinon.spy(GetMoreOperation.prototype, 'execute'); + }); + + afterEach(async function () { + sinon.restore(); + await cursor.close(); + await client.close(); + }); + + it('iterates per batch not per document', async () => { + cursor = client.db().collection('test').find({}, { batchSize }); + await cursor.toArray(); + expect(nextSpy.callCount).to.equal(numBatches + 1); + const numDocuments = numBatches * batchSize; + expect(nextSpy.callCount).to.be.lessThan(numDocuments); + }); + }); });