Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jul 26, 2024
1 parent 2f1b16a commit 5c87be0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/benchmarks/mongoBench/suites/multiBench.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 39 additions & 1 deletion test/integration/node-specific/abstract_cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
});
});
});

0 comments on commit 5c87be0

Please sign in to comment.