Skip to content

Commit

Permalink
fix(cursor): cursor.count not respecting parent readPreference
Browse files Browse the repository at this point in the history
Fix wrong placement and typo in options argument in
cursor.s.topology.command, and test that specified readPreference
is passed in to that call.

Fixes NODE-1511
  • Loading branch information
rweinberger authored Jun 25, 2018
1 parent 12ff392 commit 5a9fdf0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 0 additions & 1 deletion lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ const validOptions = require('./operations/mongo_client_ops').validOptions;
*/
function MongoClient(url, options) {
if (!(this instanceof MongoClient)) return new MongoClient(url, options);

// Set up event emitter
EventEmitter.call(this);

Expand Down
4 changes: 2 additions & 2 deletions lib/operations/cursor_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ function count(cursor, applySkipLimit, opts, callback) {
cursor.s.topology.command(
`${cursor.s.ns.substr(0, delimiter)}.$cmd`,
command,
cursor.s.options,
(err, result) => {
callback(err, result ? result.result.n : null);
},
cursor.options
}
);
}

Expand Down
31 changes: 31 additions & 0 deletions test/functional/cursor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const setupDatabase = require('./shared').setupDatabase;
const fs = require('fs');
const expect = require('chai').expect;
const Long = require('bson').Long;
const sinon = require('sinon');

describe('Cursor', function() {
before(function() {
Expand Down Expand Up @@ -4501,4 +4502,34 @@ describe('Cursor', function() {

testTransformStream(config, done);
});

it('should apply parent read preference to count command', function(done) {
const configuration = this.configuration;
const ReadPreference = this.configuration.require.ReadPreference;
const client = configuration.newClient(
{ w: 1, readPreference: ReadPreference.secondary },
{ poolSize: 1, auto_reconnect: false }
);

client.connect(function(err, client) {
const db = client.db(configuration.db);
let collection, cursor, spy;
const close = e => cursor.close(() => client.close(() => done(e)));

Promise.resolve()
.then(() => db.createCollection('test_count_readPreference'))
.then(() => (collection = db.collection('test_count_readPreference')))
.then(() => collection.find())
.then(_cursor => (cursor = _cursor))
.then(() => (spy = sinon.spy(cursor.s.topology, 'command')))
.then(() => cursor.count())
.then(() =>
expect(spy.firstCall.args[2])
.to.have.nested.property('readPreference.mode')
.that.equals('secondary')
)
.then(() => close())
.catch(e => close(e));
});
});
});

0 comments on commit 5a9fdf0

Please sign in to comment.