Skip to content

Commit

Permalink
fix(find): respect client-level provided read preference
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Aug 20, 2019
1 parent c18b4ba commit fec4f15
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/operations/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
const OperationBase = require('./operation').OperationBase;
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const resolveReadPreference = require('../utils').resolveReadPreference;

class FindOperation extends OperationBase {
constructor(collection, ns, command, options) {
super(options);

this.ns = ns;
this.cmd = command;
this.readPreference = resolveReadPreference(collection, this.options);
}

execute(server, callback) {
Expand Down
42 changes: 42 additions & 0 deletions test/functional/find_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const test = require('./shared').assert;
const setupDatabase = require('./shared').setupDatabase;
const expect = require('chai').expect;
const Buffer = require('safe-buffer').Buffer;
const sinon = require('sinon');

describe('Find', function() {
before(function() {
Expand Down Expand Up @@ -3121,4 +3122,45 @@ describe('Find', function() {
});
}
});

it('should respect client-level read preference', {
metadata: { requires: { topology: ['replicaset'] } },

test: function(done) {
const config = this.configuration;
const client = config.newClient({}, { monitorCommands: true, readPreference: 'secondary' });

if (!config.usingUnifiedTopology()) {
this.skip();
return;
}

client.connect((err, client) => {
expect(err).to.not.exist;

let selectedServer;
const selectServerStub = sinon.stub(client.topology, 'selectServer').callsFake(function() {
const args = Array.prototype.slice.call(arguments);
const originalCallback = args.pop();
args.push((err, server) => {
selectedServer = server;
originalCallback(err, server);
});

return client.topology.selectServer.wrappedMethod.apply(this, args);
});

const collection = client.db().collection('test_read_preference');
collection.find().toArray(err => {
expect(err).to.not.exist;
expect(selectedServer.description.type).to.eql('RSSecondary');

client.close(err => {
selectServerStub.restore();
done(err);
});
});
});
}
});
});

0 comments on commit fec4f15

Please sign in to comment.