Skip to content

Commit

Permalink
Added test for NEAREST slaveOk and stricter setting of slaveOk #905
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Mar 13, 2013
1 parent d7bcfb2 commit 709aaae
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = all

total: build_native

test:
test_functional:
node test/runner.js -t functional

test_ssl:
Expand Down
9 changes: 7 additions & 2 deletions lib/mongodb/connection/repl_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ var ReplSet = exports.ReplSet = function(servers, options) {
|| this._readPreference == false
|| this._readPreference == null ? false : true;

// Ensure correct slave set
if(this.readSecondary) this.slaveOk = true;

// Strategy for picking a secondary
this.secondaryAcceptableLatencyMS = this.options['secondaryAcceptableLatencyMS'] == null ? 15 : this.options['secondaryAcceptableLatencyMS'];
this.strategy = this.options['strategy'];
Expand Down Expand Up @@ -239,8 +242,10 @@ inherits(ReplSet, Base);
ReplSet.prototype.setReadPreference = function(preference) {
// Set read preference
this._readPreference = preference;
// Ensure slaveOk is correct for secodnaries read preference and tags
if((this._readPreference == ReadPreference.SECONDARY_PREFERRED || this._readPreference == ReadPreference.SECONDARY)
// Ensure slaveOk is correct for secondaries read preference and tags
if((this._readPreference == ReadPreference.SECONDARY_PREFERRED
|| this._readPreference == ReadPreference.SECONDARY
|| this._readPreference == ReadPreference.NEAREST)
|| (this._readPreference != null && typeof this._readPreference == 'object')) {
this.slaveOk = true;
}
Expand Down
1 change: 0 additions & 1 deletion lib/mongodb/connection/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var Connection = require('./connection').Connection,
* - **sslCert** {Buffer/String, default:null}, String or buffer containing the certificate we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
* - **sslKey** {Buffer/String, default:null}, String or buffer containing the certificate private key we wish to present (needs to have a mongod server with ssl support, 2.4 or higher)
* - **sslPass** {Buffer/String, default:null}, String or buffer containing the certificate password (needs to have a mongod server with ssl support, 2.4 or higher)
* - **slaveOk** {Boolean, default:false}, legacy option allowing reads from secondary, use **readPrefrence** instead.
* - **poolSize** {Number, default:5}, number of connections in the connection pool, set to 5 as default for legacy reasons.
* - **socketOptions** {Object, default:null}, an object containing socket options to use (noDelay:(boolean), keepAlive:(number), connectTimeoutMS:(number), socketTimeoutMS:(number))
* - **logger** {Object, default:null}, an object representing a logger that you want to use, needs to support functions debug, log, error **({error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}})**.
Expand Down
12 changes: 6 additions & 6 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ if(argv.t == 'functional') {
} else if(argv.t == 'auth') {
// Trap end of tests
standalone_runners.runner_auth.on('end', function() {
replicaset_runners.runner_auth.run('replica_set_auth');
replicaset_runners.runner_auth.run('replica_set_auth', run_options);
});

replicaset_runners.runner_auth.on('end', function() {
sharded_runners.runner_auth.run('sharded_auth');
sharded_runners.runner_auth.run('sharded_auth', run_options);
});

// Start chain of auth tests
standalone_runners.runner_auth.run('single_server_auth');
standalone_runners.runner_auth.run('single_server_auth', run_options);
} else if(argv.t == 'ssl') {
ssl_runners.runner.run('none');
ssl_runners.runner.run('none', run_options);
} else if(argv.t == 'sharded') {
sharded_runners.runner.run('sharded');
sharded_runners.runner.run('sharded', run_options);
} else if(argv.t == 'replicaset') {
replicaset_runners.runner.run('replica_set')
replicaset_runners.runner.run('replica_set', run_options);
}

// console.log(argv.t)
Expand Down
51 changes: 51 additions & 0 deletions test/tests/repl_set/read_preferences_spec_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,57 @@ var format = require('util').format;
// })
// }

exports['Should Correctly Use Secondary Server with Query when using NEAREST'] = function(configuration, test) {
var mongo = configuration.getMongoPackage()
, MongoClient = mongo.MongoClient
, ReadPreference = mongo.ReadPreference
, ReplSetServers = mongo.ReplSetServers
, Server = mongo.Server
, Db = mongo.Db;

var replicasetManager = configuration.getReplicasetManager();

// Replica configuration
var replSet = new ReplSetServers([
new Server(replicasetManager.host, replicasetManager.ports[0]),
new Server(replicasetManager.host, replicasetManager.ports[1]),
new Server(replicasetManager.host, replicasetManager.ports[2])
],
{readPreference: ReadPreference.NEAREST}
);

// Open the database
var db = new Db('integration_test_', replSet, {w:1});
db.open(function(err, db) {
// Force selection of a secondary
db.serverConfig._state.master.runtimeStats['pingMs'] = 5000;
// Check that we get a secondary
var connection = db.serverConfig.checkoutReader();
var keys = Object.keys(db.serverConfig._state.secondaries);
var found = false;

for(var i = 0; i < keys.length; i++) {
if(keys[i].indexOf(connection.socketOptions.port.toString()) != -1) found = true;
}

// Verify that checkout of Reader returns secondary
test.equal(true, found);

// Execute a query
db.collection('nearest_collection_test').insert({a:1}, {w:3, wtimeout:10000}, function(err, doc) {
test.equal(null, err);

db.collection('nearest_collection_test').findOne({a:1}, function(err, doc) {
test.equal(null, err);
test.equal(1, doc.a);

db.close();
test.done();
});
});
});
}

exports['Should Correctly Pick lowest ping time'] = function(configuration, test) {
var mongo = configuration.getMongoPackage()
, MongoClient = mongo.MongoClient
Expand Down

0 comments on commit 709aaae

Please sign in to comment.