Skip to content

Commit

Permalink
Force correct setting of read_secondary based on the read preference …
Browse files Browse the repository at this point in the history
…(Issue #741)
  • Loading branch information
christkv committed Oct 29, 2012
1 parent 87b1287 commit f6ca881
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
5 changes: 3 additions & 2 deletions HISTORY
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
1.1.12
-----------------
- honor connectTimeoutMS option for replicasets (Issue #750, https://github.com/aheckmann)
- Honor connectTimeoutMS option for replicasets (Issue #750, https://github.com/aheckmann)
- Fix ping strategy regression (Issue #738, https://github.com/aheckmann)
- Small cleanup of code (Issue #753, https://github.com/sokra/node-mongodb-native)
- fixed index declaration using objects/arrays from other contexts (Issue #755, https://github.com/sokra/node-mongodb-native)
- Fixed index declaration using objects/arrays from other contexts (Issue #755, https://github.com/sokra/node-mongodb-native)
- Intermittent (and rare) null callback exception when using ReplicaSets (Issue #752)
- Force correct setting of read_secondary based on the read preference (Issue #741)

1.1.11 2012-10-10
-----------------
Expand Down
3 changes: 3 additions & 0 deletions lib/mongodb/connection/repl_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ var ReplSet = exports.ReplSet = function(servers, options) {
this._readPreference = null;
}

// Ensure read_secondary is set correctly
this.readSecondary = this._readPreference == ReadPreference.PRIMARY ? false : true;

// Strategy for picking a secondary
this.secondaryAcceptableLatencyMS = this.options['secondaryAcceptableLatencyMS'] == null ? 15 : this.options['secondaryAcceptableLatencyMS'];
this.strategy = this.options['strategy'];
Expand Down
52 changes: 51 additions & 1 deletion test/auxilliary/replicaset_auth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var testCase = require('nodeunit').testCase,
Cursor = mongodb.Cursor,
Collection = mongodb.Collection,
Server = mongodb.Server,
ReadPreference = mongodb.ReadPreference,
ReplSetServers = mongodb.ReplSetServers,
ReplicaSetManager = require('../../test/tools/replica_set_manager').ReplicaSetManager,
Step = require("step");
Expand All @@ -27,7 +28,7 @@ exports.setUp = function(callback) {
RS = new ReplicaSetManager({retries:120,
auth:true,
arbiter_count:0,
secondary_count:1,
secondary_count:2,
passive_count:0});
RS.startSet(true, function(err, result) {
if(err != null) throw err;
Expand Down Expand Up @@ -291,6 +292,55 @@ exports.shouldCorrectlyAuthenticateAndEnsureIndex = function(test) {
});
}

exports.shouldCorrectlyAuthenticateAndUseReadPreference = function(test) {
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
],
{rs_name:RS.name}
);

var db = new Db(MONGODB, replSet, {safe:false, native_parser: false});
db.open(function(err, db_p) {
test.equal(null, err);

db_p.addUser('test', 'test', function(err, result) {
test.equal(null, err);

db_p.authenticate('test', 'test', function(err, replies) {
test.equal(null, err);

db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
console.log("---------------------------------------------------------------")
console.dir(err)
console.dir(items)

db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
console.log("---------------------------------------------------------------")
console.dir(err)
console.dir(items)

db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
console.log("---------------------------------------------------------------")
console.dir(err)
console.dir(items)

db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
console.log("---------------------------------------------------------------")
console.dir(err)
console.dir(items)
test.equal(null, err);
db_p.close();
test.done();
});
});
});
});
});
});
});
}

/**
* Retrieve the server information for the current
* instance of the db client
Expand Down

0 comments on commit f6ca881

Please sign in to comment.