Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jul 26, 2022
1 parent ce398f1 commit 74fdbb3
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions test/integration/crud/cursors.test.ts
@@ -0,0 +1,95 @@
import { expect } from 'chai';
import { inspect } from 'util';

import {
Collection,
CommandStartedEvent,
MongoClient,
MongoError,
MongoServerError
} from '../../../src';
import { runLater, sleep } from '../../tools/utils';

describe('MongoDB Cursors', () => {
describe('tailable cursors', () => {
const insertedDocs = [{ _id: 1 }];
let client: MongoClient;
let cappedCollection: Collection<{ _id: number }>;
beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
await client
.db()
.dropCollection('cappedAt3')
.catch(() => null);
await sleep(500);
cappedCollection = await client
.db()
.createCollection('cappedAt3', { capped: true, size: 4096, max: 3 });
cappedCollection.insertMany(insertedDocs);
});

afterEach(async function () {
await client?.close();
});

const tailableValues = [true, false, undefined];
const awaitDataValues = [true, false, undefined];
const maxTimeMSValues = [800, 0, undefined];
const maxAwaitTimeMSValues = [800, 0, undefined];

const tests = tailableValues.flatMap(tailable =>
awaitDataValues.flatMap(awaitData =>
maxAwaitTimeMSValues.flatMap(maxAwaitTimeMS =>
maxTimeMSValues.flatMap(maxTimeMS => {
const awaitDataSet = Boolean(awaitData) === true;
const tailableSet = Boolean(tailable) === true;
const timeIsSet = typeof maxAwaitTimeMS === 'number' || typeof maxTimeMS === 'number';
return [
{
options: { tailable, awaitData, maxAwaitTimeMS, maxTimeMS },
outcome: {
isError:
// Cannot set 'awaitData' without also setting 'tailable'
(awaitDataSet && !tailableSet) ||
// cannot set maxTimeMS on getMore command for a non-awaitData cursor
(timeIsSet && awaitData === false && !tailableSet)
}
}
];
})
)
)
);

for (const { options, outcome } of tests) {
let optionsString = inspect(options, { breakLength: Infinity });
optionsString = optionsString
.slice(2, optionsString.length - 2)
.split('undefined')
.join('omit');

it(`should create find cursor with ${optionsString}`, async () => {
const events: CommandStartedEvent[] = [];
client.on('commandStarted', event => events.push(event));
const cursor = cappedCollection.find({ _id: { $gt: 0 } }, { ...options, batchSize: 1 });

const docOrError: { _id: number } | Error = await cursor.next().catch(error => error);

if (outcome.isError) {
expect(docOrError).to.be.instanceOf(MongoServerError);
} else {
if (docOrError instanceof Error) {
throw docOrError;
}
const doc = docOrError;

expect(doc).to.have.property('_id', 1);

await cursor.tryNext();

await cursor.close();
}
});
}
});
});

0 comments on commit 74fdbb3

Please sign in to comment.