From d1a81595e4e53ca3143dc07f3342e365f1a8d553 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 27 Jul 2022 17:18:41 -0300 Subject: [PATCH 1/2] fix(NODE-4467): Add back support for `oplogReplay` option as deprecated The option has been deprecated for MongoDB >=4.4 but should still be available to MongoDB <=4.2 --- src/operations/find.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/operations/find.ts b/src/operations/find.ts index c52aa2cd92..ba2e8cf741 100644 --- a/src/operations/find.ts +++ b/src/operations/find.ts @@ -63,6 +63,11 @@ export interface FindOptions extends Comman showRecordId?: boolean; /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ let?: Document; + /** + * Option to enable an optimized code path for queries looking for a particular range of `ts` values in the oplog. Requires `tailable` to be true. + * @deprecated Starting from MongoDB 4.4 this flag is not needed and will be ignored. + */ + oplogReplay?: boolean; } const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5; @@ -242,6 +247,10 @@ function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOp findCommand.tailable = options.tailable; } + if (typeof options.oplogReplay === 'boolean') { + findCommand.oplogReplay = options.oplogReplay; + } + if (typeof options.timeout === 'boolean') { findCommand.noCursorTimeout = !options.timeout; } else if (typeof options.noCursorTimeout === 'boolean') { From 34798b8d6b7890e54bba8f9cb959608fdac6707b Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Thu, 28 Jul 2022 12:10:57 -0300 Subject: [PATCH 2/2] test(NODE-4467): Add tests for FindOperation --- test/unit/operations/find.test.ts | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 test/unit/operations/find.test.ts diff --git a/test/unit/operations/find.test.ts b/test/unit/operations/find.test.ts new file mode 100644 index 0000000000..f4031bcfbd --- /dev/null +++ b/test/unit/operations/find.test.ts @@ -0,0 +1,69 @@ +import { expect } from 'chai'; +import * as sinon from 'sinon'; +import { promisify } from 'util'; + +import { FindOperation } from '../../../src/operations/find'; +import { Server } from '../../../src/sdam/server'; +import { ServerDescription } from '../../../src/sdam/server_description'; +import { Topology } from '../../../src/sdam/topology'; +import { ns } from '../../../src/utils'; + +describe('FindOperation', function () { + const namespace = ns('db.coll'); + const options = { + batchSize: 100 + }; + const filter = { + ts: { $gt: new Date() } + }; + + afterEach(function () { + sinon.restore(); + }); + + describe('#constructor', function () { + const operation = new FindOperation(undefined, namespace, filter, options); + + it('sets the namespace', function () { + expect(operation.ns).to.equal(namespace); + }); + + it('sets options', function () { + expect(operation.options).to.equal(options); + }); + + it('sets filter', function () { + expect(operation.filter).to.equal(filter); + }); + }); + + describe('#execute', function () { + context('command construction', () => { + const namespace = ns('db.collection'); + const server = new Server(new Topology([], {} as any), new ServerDescription(''), {} as any); + + it('should build basic find command with filter', async () => { + const findOperation = new FindOperation(undefined, namespace, filter); + const stub = sinon.stub(server, 'command').yieldsRight(); + await promisify(findOperation.execute.bind(findOperation))(server, undefined); + expect(stub).to.have.been.calledOnceWith(namespace, { + find: namespace.collection, + filter + }); + }); + + it('should build find command with oplogReplay', async () => { + const options = { + oplogReplay: true + }; + const findOperation = new FindOperation(undefined, namespace, {}, options); + const stub = sinon.stub(server, 'command').yieldsRight(); + await promisify(findOperation.execute.bind(findOperation))(server, undefined); + expect(stub).to.have.been.calledOnceWith( + namespace, + sinon.match.has('oplogReplay', options.oplogReplay) + ); + }); + }); + }); +});