Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect readPreference and writeConcern from connection string #2711

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/operations/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
32 changes: 31 additions & 1 deletion test/functional/readpreference.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -729,4 +732,31 @@ describe('ReadPreference', function() {
});
});
});

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
.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();
});
})
});
});
24 changes: 24 additions & 0 deletions test/functional/write_concern.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ describe('Write Concern', function() {
generateTopologyTests(testSuites, testContext);
});

it(
'should respect writeConcern from uri',
withMonitoredClient('insert', { queryOptions: { w: 0 } }, function(client, events, done) {
expect(client.writeConcern).to.eql({ w: 0 });
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() {
function journalOptionTest(client, events, done) {
Expand Down