Skip to content

Commit 5c87be0

Browse files
add test
1 parent 2f1b16a commit 5c87be0

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

test/benchmarks/mongoBench/suites/multiBench.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function makeMultiBench(suite) {
2121
.setup(initCollection)
2222
.setup(makeLoadTweets(false))
2323
.task(async function () {
24-
await this.collection.find({}).toArray();
24+
await this.collection.find({}).toArray({ batchSize: 1000 });
2525
})
2626
.teardown(dropDb)
2727
.teardown(disconnectClient)

test/integration/node-specific/abstract_cursor.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { Transform } from 'stream';
55
import { inspect } from 'util';
66

77
import {
8+
AbstractCursor,
89
type Collection,
910
type FindCursor,
1011
MongoAPIError,
1112
type MongoClient,
1213
MongoCursorExhaustedError,
13-
MongoServerError
14+
MongoServerError,
15+
GetMoreOperation
1416
} from '../../mongodb';
1517

1618
describe('class AbstractCursor', function () {
@@ -337,6 +339,7 @@ describe('class AbstractCursor', function () {
337339
describe('when the last document has been iterated', () => {
338340
it('has a zero id and is closed and is never killed', async function () {
339341
cursor = client.db().collection('test').find({});
342+
expect(cursor).to.have.property('closed', false);
340343
await cursor.next();
341344
await cursor.next();
342345
await cursor.next();
@@ -361,4 +364,39 @@ describe('class AbstractCursor', function () {
361364
});
362365
});
363366
});
367+
368+
describe.only('toArray', () => {
369+
let nextSpy;
370+
let getMoreSpy;
371+
let client: MongoClient;
372+
let cursor: AbstractCursor;
373+
let col: Collection;
374+
const numBatches = 10;
375+
const batchSize = 4;
376+
377+
beforeEach(async function () {
378+
client = this.configuration.newClient();
379+
col = client.db().collection('test');
380+
await col.deleteMany({});
381+
for (let i = 0; i < numBatches; i++) {
382+
await col.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }]);
383+
}
384+
nextSpy = sinon.spy(AbstractCursor.prototype, 'next');
385+
getMoreSpy = sinon.spy(GetMoreOperation.prototype, 'execute');
386+
});
387+
388+
afterEach(async function () {
389+
sinon.restore();
390+
await cursor.close();
391+
await client.close();
392+
});
393+
394+
it('iterates per batch not per document', async () => {
395+
cursor = client.db().collection('test').find({}, { batchSize });
396+
await cursor.toArray();
397+
expect(nextSpy.callCount).to.equal(numBatches + 1);
398+
const numDocuments = numBatches * batchSize;
399+
expect(nextSpy.callCount).to.be.lessThan(numDocuments);
400+
});
401+
});
364402
});

0 commit comments

Comments
 (0)