From c6fb9587ffa88e67cb04a1c923cd3806cde45ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=20=D0=9B=D0=BE=D0=B3?= =?UTF-8?q?=D0=B2=D0=B8=D0=BD?= Date: Sun, 16 Feb 2025 14:49:48 +0300 Subject: [PATCH 1/3] fix: Pass WriteConcernOptions instead on WriteConcernSettings --- src/utils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index cf6ad7752d7..4a436c2a35d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -531,9 +531,11 @@ export function resolveOptions( if (writeConcern) { if (timeoutMS != null) { writeConcern = WriteConcern.fromOptions({ - ...writeConcern, - wtimeout: undefined, - wtimeoutMS: undefined + writeConcern: { + ...writeConcern, + wtimeout: undefined, + wtimeoutMS: undefined + } }); } result.writeConcern = writeConcern; From d090947222cf8f54024a94754a9f7b051dfcd272 Mon Sep 17 00:00:00 2001 From: bailey Date: Tue, 18 Feb 2025 11:42:00 -0700 Subject: [PATCH 2/3] add regression test --- .../read-write-concern/write_concern.test.ts | 46 +++++++++++++++++++ test/unit/utils.test.ts | 7 ++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/test/integration/read-write-concern/write_concern.test.ts b/test/integration/read-write-concern/write_concern.test.ts index 9db7269b89f..b9bd0574df8 100644 --- a/test/integration/read-write-concern/write_concern.test.ts +++ b/test/integration/read-write-concern/write_concern.test.ts @@ -304,4 +304,50 @@ describe('Write Concern', function () { } }); }); + + describe('NODE-6763: write concern is still added with timeoutMS is set', function () { + let client: MongoClient; + let collection: Collection; + const commands: CommandStartedEvent[] = []; + beforeEach(async function () { + client = this.configuration.newClient({}, { monitorCommands: true }); + client.on('commandStarted', filterForCommands('insert', commands)); + collection = client.db('foo').collection('bar'); + }) + + + afterEach(async function () { + await client.close(); + commands.length = 0; + }) + + context('when the write concern includes only timeouts', function () { + it('the writeConcern is not added to the command.', async function () { + await collection.insertOne({ name: 'john doe' }, { timeoutMS: 1000, writeConcern: { wtimeout: 1000 } }); + const [ + { + command: { + writeConcern + } + } + ] = commands; + expect(writeConcern).not.to.exist; + }) + }) + + context('when the write concern includes only non-timeout values (`w`)', function () { + it('the writeConcern is added to the command.', async function () { + await collection.insertOne({ name: 'john doe' }, { timeoutMS: 1000, writeConcern: { wtimeout: 1000, w: 'majority' } }); + const [ + { + command: { + writeConcern + } + } + ] = commands; + expect(writeConcern).to.deep.equal({ w: 'majority' }) + }) + + }) + }) }); diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts index 26c6211075f..7eac2799542 100644 --- a/test/unit/utils.test.ts +++ b/test/unit/utils.test.ts @@ -403,7 +403,7 @@ describe('driver utils', function () { it('should be instanceof GeneratorFunction', () => { const list = new List(); // eslint-disable-next-line @typescript-eslint/no-empty-function - expect(list[Symbol.iterator]).to.be.instanceOf(function* () {}.constructor); + expect(list[Symbol.iterator]).to.be.instanceOf(function* () { }.constructor); }); it('should only run generator for the number of items in the list', () => { @@ -857,9 +857,8 @@ describe('driver utils', function () { continue; } - const title = `comparing ${oid1} to ${oid2} returns ${ - result === 0 ? 'equal' : result === -1 ? 'less than' : 'greater than' - }`; + const title = `comparing ${oid1} to ${oid2} returns ${result === 0 ? 'equal' : result === -1 ? 'less than' : 'greater than' + }`; // @ts-expect-error: not narrowed based on numeric result, but these values are correct it(title, () => expect(compareObjectId(oid1, oid2)).to.equal(result)); } From eaa610be58473f6f173a8a2f1be9f098b0a3d961 Mon Sep 17 00:00:00 2001 From: bailey Date: Tue, 18 Feb 2025 11:44:39 -0700 Subject: [PATCH 3/3] lint --- .../read-write-concern/write_concern.test.ts | 37 ++++++++++--------- test/unit/utils.test.ts | 7 ++-- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/test/integration/read-write-concern/write_concern.test.ts b/test/integration/read-write-concern/write_concern.test.ts index b9bd0574df8..58afc2d04e6 100644 --- a/test/integration/read-write-concern/write_concern.test.ts +++ b/test/integration/read-write-concern/write_concern.test.ts @@ -309,45 +309,46 @@ describe('Write Concern', function () { let client: MongoClient; let collection: Collection; const commands: CommandStartedEvent[] = []; + beforeEach(async function () { client = this.configuration.newClient({}, { monitorCommands: true }); client.on('commandStarted', filterForCommands('insert', commands)); collection = client.db('foo').collection('bar'); - }) - + }); afterEach(async function () { await client.close(); commands.length = 0; - }) + }); context('when the write concern includes only timeouts', function () { it('the writeConcern is not added to the command.', async function () { - await collection.insertOne({ name: 'john doe' }, { timeoutMS: 1000, writeConcern: { wtimeout: 1000 } }); + await collection.insertOne( + { name: 'john doe' }, + { timeoutMS: 1000, writeConcern: { wtimeout: 1000 } } + ); const [ { - command: { - writeConcern - } + command: { writeConcern } } ] = commands; expect(writeConcern).not.to.exist; - }) - }) + }); + }); context('when the write concern includes only non-timeout values (`w`)', function () { it('the writeConcern is added to the command.', async function () { - await collection.insertOne({ name: 'john doe' }, { timeoutMS: 1000, writeConcern: { wtimeout: 1000, w: 'majority' } }); + await collection.insertOne( + { name: 'john doe' }, + { timeoutMS: 1000, writeConcern: { wtimeout: 1000, w: 'majority' } } + ); const [ { - command: { - writeConcern - } + command: { writeConcern } } ] = commands; - expect(writeConcern).to.deep.equal({ w: 'majority' }) - }) - - }) - }) + expect(writeConcern).to.deep.equal({ w: 'majority' }); + }); + }); + }); }); diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts index 7eac2799542..26c6211075f 100644 --- a/test/unit/utils.test.ts +++ b/test/unit/utils.test.ts @@ -403,7 +403,7 @@ describe('driver utils', function () { it('should be instanceof GeneratorFunction', () => { const list = new List(); // eslint-disable-next-line @typescript-eslint/no-empty-function - expect(list[Symbol.iterator]).to.be.instanceOf(function* () { }.constructor); + expect(list[Symbol.iterator]).to.be.instanceOf(function* () {}.constructor); }); it('should only run generator for the number of items in the list', () => { @@ -857,8 +857,9 @@ describe('driver utils', function () { continue; } - const title = `comparing ${oid1} to ${oid2} returns ${result === 0 ? 'equal' : result === -1 ? 'less than' : 'greater than' - }`; + const title = `comparing ${oid1} to ${oid2} returns ${ + result === 0 ? 'equal' : result === -1 ? 'less than' : 'greater than' + }`; // @ts-expect-error: not narrowed based on numeric result, but these values are correct it(title, () => expect(compareObjectId(oid1, oid2)).to.equal(result)); }