From 85439d9d1c4e9565f9278b0104f5f09625b14751 Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 21 Jan 2021 09:29:26 -0500 Subject: [PATCH 1/2] fix: respect readPreference and writeConcern from connection string --- lib/operations/connect.js | 4 ++++ test/functional/readpreference.test.js | 10 ++++++++++ test/functional/write_concern.test.js | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/lib/operations/connect.js b/lib/operations/connect.js index 0d7825d4d6..2f3bff96bf 100644 --- a/lib/operations/connect.js +++ b/lib/operations/connect.js @@ -308,6 +308,10 @@ function connect(mongoClient, url, options, callback) { // Store the merged options object mongoClient.s.options = _finalOptions; + // Apply read and write concern from parsed url + mongoClient.s.readPreference = ReadPreference.fromOptions(_finalOptions); + mongoClient.s.writeConcern = WriteConcern.fromOptions(_finalOptions); + // Failure modes if (object.servers.length === 0) { return callback(new Error('connection string must contain at least one seed host')); diff --git a/test/functional/readpreference.test.js b/test/functional/readpreference.test.js index 89bce62b66..d03a9eab0f 100644 --- a/test/functional/readpreference.test.js +++ b/test/functional/readpreference.test.js @@ -729,4 +729,14 @@ describe('ReadPreference', function() { }); }); }); + + it('should respect readPreference from uri', function(done) { + const configuration = this.configuration; + const client = configuration.newClient(`${configuration.url()}?readPreference=secondary`); + client.connect(err => { + expect(err).to.not.exist; + expect(client.readPreference.mode).to.equal('secondary'); + client.close(done); + }); + }); }); diff --git a/test/functional/write_concern.test.js b/test/functional/write_concern.test.js index e7c2b954ca..3eb9c49cc3 100644 --- a/test/functional/write_concern.test.js +++ b/test/functional/write_concern.test.js @@ -21,6 +21,16 @@ describe('Write Concern', function() { generateTopologyTests(testSuites, testContext); }); + it('should respect writeConcern from uri', function(done) { + const configuration = this.configuration; + const client = configuration.newClient(`${configuration.url()}?w=0`); + client.connect(err => { + expect(err).to.not.exist; + expect(client.writeConcern).to.eql({ w: 0 }); + client.close(done); + }); + }); + // TODO: once `read-write-concern/connection-string` spec tests are implemented these can likely be removed describe('test journal connection string option', function() { function journalOptionTest(client, events, done) { From 4dbd7678f45bd2bbc9993e8ca95ccdbd658d79e7 Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 21 Jan 2021 10:13:25 -0500 Subject: [PATCH 2/2] improve tests --- test/functional/readpreference.test.js | 36 ++++++++++++++++++++------ test/functional/write_concern.test.js | 30 +++++++++++++++------ 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/test/functional/readpreference.test.js b/test/functional/readpreference.test.js index d03a9eab0f..c51e4c8efc 100644 --- a/test/functional/readpreference.test.js +++ b/test/functional/readpreference.test.js @@ -4,9 +4,12 @@ const Topology = require('../../lib/core/sdam/topology').Topology; const test = require('./shared').assert; const setupDatabase = require('./shared').setupDatabase; const withMonitoredClient = require('./shared').withMonitoredClient; -const expect = require('chai').expect; + const ReadPreference = require('../../lib/core/topologies/read_preference'); const withClient = require('./shared').withClient; +const chai = require('chai'); +chai.use(require('chai-subset')); +const expect = chai.expect; describe('ReadPreference', function() { before(function() { @@ -730,13 +733,30 @@ describe('ReadPreference', function() { }); }); - it('should respect readPreference from uri', function(done) { - const configuration = this.configuration; - const client = configuration.newClient(`${configuration.url()}?readPreference=secondary`); - client.connect(err => { - expect(err).to.not.exist; + it('should respect readPreference from uri', { + metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } }, + test: withMonitoredClient('find', { queryOptions: { readPreference: 'secondary' } }, function( + client, + events, + done + ) { expect(client.readPreference.mode).to.equal('secondary'); - client.close(done); - }); + client + .db('test') + .collection('test') + .findOne({ a: 1 }, err => { + expect(err).to.not.exist; + expect(events) + .to.be.an('array') + .with.lengthOf(1); + expect(events[0]).to.containSubset({ + commandName: 'find', + command: { + $readPreference: { mode: 'secondary' } + } + }); + done(); + }); + }) }); }); diff --git a/test/functional/write_concern.test.js b/test/functional/write_concern.test.js index 3eb9c49cc3..0f73d5396a 100644 --- a/test/functional/write_concern.test.js +++ b/test/functional/write_concern.test.js @@ -21,15 +21,29 @@ describe('Write Concern', function() { generateTopologyTests(testSuites, testContext); }); - it('should respect writeConcern from uri', function(done) { - const configuration = this.configuration; - const client = configuration.newClient(`${configuration.url()}?w=0`); - client.connect(err => { - expect(err).to.not.exist; + it( + 'should respect writeConcern from uri', + withMonitoredClient('insert', { queryOptions: { w: 0 } }, function(client, events, done) { expect(client.writeConcern).to.eql({ w: 0 }); - client.close(done); - }); - }); + client + .db('test') + .collection('test') + .insertOne({ a: 1 }, (err, result) => { + expect(err).to.not.exist; + expect(result).to.exist; + expect(events) + .to.be.an('array') + .with.lengthOf(1); + expect(events[0]).to.containSubset({ + commandName: 'insert', + command: { + writeConcern: { w: 0 } + } + }); + done(); + }); + }) + ); // TODO: once `read-write-concern/connection-string` spec tests are implemented these can likely be removed describe('test journal connection string option', function() {