From 74fdbb3de1e5efa7e132160e8d1a26bfeb65aa7f Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Tue, 26 Jul 2022 18:46:42 -0400 Subject: [PATCH] wip --- test/integration/crud/cursors.test.ts | 95 +++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 test/integration/crud/cursors.test.ts diff --git a/test/integration/crud/cursors.test.ts b/test/integration/crud/cursors.test.ts new file mode 100644 index 0000000000..2c0d1ac95f --- /dev/null +++ b/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(); + } + }); + } + }); +});