Skip to content

Commit d92c02f

Browse files
committed
Added tests for correct connection against replicaset with mixed setup of replicaset and standalones
1 parent 0bfcfa3 commit d92c02f

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

HISTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2.0.6 2014-11-14
2+
----------------
3+
- Refactored code to be prototype based instead of privileged methods.
4+
- Bumped mongodb-core to 1.1.1 to take advantage of the prototype based refactorings.
5+
- Implemented missing aspects of the CRUD specification.
6+
- Fixed documentation issues.
7+
18
2.0.5 2014-10-29
29
----------------
310
- Minor fixes to documentation and generation of documentation.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
, "bson": "~0.2"
2626
, "jsdoc": "3.3.0-alpha9"
2727
, "semver": "4.1.0"
28+
, "rimraf": "2.2.6"
2829
},
2930
"author": "Christian Kvalheim",
3031
"license": "Apache 2.0",

test/functional/replset_connection_tests.js

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ exports['Should correctly connect with default replicaset and socket options set
114114

115115
var db = new Db('integration_test_', replSet, {w:0});
116116
db.open(function(err, p_db) {
117-
console.dir(err)
118117
test.equal(null, err);
119118
// Get a connection
120119
var connection = db.serverConfig.connections()[0];
@@ -237,13 +236,10 @@ var ensureConnection = function(configuration, numberOfTries, callback) {
237236
{rs_name:configuration.replicasetName, socketOptions: {connectTimeoutMS: 1000}}
238237
);
239238

240-
// console.log("===================== ensureConnection 0 :: " + numberOfTries)
241-
242239
if(numberOfTries <= 0) return callback(new Error("could not connect correctly"), null);
243240
// Open the db
244241
var db = new Db('integration_test_', replSet, {w:0});
245242
db.open(function(err, p_db) {
246-
// console.log("===================== ensureConnection 1")
247243
// Close the connection
248244
db.close();
249245

@@ -959,3 +955,98 @@ exports['Should give an error when using a two server seeds and no setName'] = {
959955
});
960956
}
961957
}
958+
959+
var waitForPrimary = function(count, config, options, callback) {
960+
var ReplSet = require('mongodb-core').ReplSet;
961+
if(count == 0) return callback(new Error("could not connect"));
962+
// Attempt to connect
963+
var server = new ReplSet(config, options);
964+
server.on('error', function(err) {
965+
server.destroy();
966+
967+
setTimeout(function() {
968+
waitForPrimary(count - 1, config, options, callback);
969+
}, 1000);
970+
});
971+
972+
server.on('fullsetup', function(_server) {
973+
server.destroy();
974+
callback();
975+
});
976+
977+
// Start connection
978+
server.connect();
979+
}
980+
981+
exports['Replicaset connection where a server is standalone'] = {
982+
metadata: {
983+
requires: {
984+
topology: "replicaset"
985+
}
986+
},
987+
988+
test: function(configuration, test) {
989+
var Server = configuration.require.Server
990+
, ReplSet = configuration.require.ReplSet
991+
, ServerManager = require('mongodb-core').ServerManager
992+
, MongoClient = configuration.require.MongoClient
993+
, manager = configuration.manager;
994+
995+
// State
996+
var joined = {'primary':[], 'secondary': [], 'arbiter': [], 'passive': []};
997+
var left = {'primary':[], 'secondary': [], 'arbiter': [], 'passive': []};
998+
// Get the primary server
999+
manager.getServerManagerByType('primary', function(err, primaryServerManager) {
1000+
test.equal(null, err);
1001+
1002+
// Get the secondary server
1003+
manager.getServerManagerByType('secondary', function(err, serverManager) {
1004+
test.equal(null, err);
1005+
1006+
// Start a new server manager
1007+
var nonReplSetMember = new ServerManager({
1008+
host: primaryServerManager.host
1009+
, port: primaryServerManager.port
1010+
, dbpath: primaryServerManager.dbpath
1011+
, logpath: primaryServerManager.logpath
1012+
});
1013+
1014+
var config = [{
1015+
host: serverManager.host
1016+
, port: serverManager.port
1017+
}];
1018+
1019+
var options = {
1020+
setName: configuration.replicasetName
1021+
};
1022+
1023+
// Stop the primary
1024+
primaryServerManager.stop(function(err, r) {
1025+
1026+
// Start a non replset member
1027+
nonReplSetMember.start(function() {
1028+
1029+
// Wait for primary
1030+
waitForPrimary(30, config, options, function(err, r) {
1031+
test.equal(null, err);
1032+
1033+
var url = f("mongodb://localhost:%s,localhost:%s,localhost:%s/integration_test_?replicaSet=%s"
1034+
, configuration.port, configuration.port + 1, configuration.port + 2, configuration.replicasetName)
1035+
// Attempt to connect using MongoClient uri
1036+
MongoClient.connect(url, function(err, db) {
1037+
test.equal(null, err);
1038+
test.ok(db.serverConfig instanceof ReplSet);
1039+
1040+
// Stop the normal server
1041+
nonReplSetMember.stop(function() {
1042+
restartAndDone(configuration, test);
1043+
});
1044+
});
1045+
});
1046+
});
1047+
});
1048+
});
1049+
});
1050+
}
1051+
}
1052+

test/functional/replset_operations_tests.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ exports['Should Correctly group using replicaset'] = {
355355
var manager = configuration.manager;
356356

357357
MongoClient.connect(url, function(err, db) {
358+
test.equal(null, err);
359+
358360
var collection = db.collection('testgroup_replicaset', {
359361
readPreference: ReadPreference.SECONDARY
360362
, w:2, wtimeout: 10000

test/runner.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Runner = require('integra').Runner
1111
, FileFilter = require('integra').FileFilter
1212
, TestNameFilter = require('integra').TestNameFilter
1313
, path = require('path')
14+
, rimraf = require('rimraf')
1415
, fs = require('fs')
1516
, f = require('util').format;
1617

@@ -456,6 +457,13 @@ if(argv.t == 'functional') {
456457
// Add travis filter
457458
runner.plugin(new TravisFilter());
458459

460+
// Remove db directories
461+
try {
462+
rimraf.sync('./data');
463+
rimraf.sync('./db');
464+
} catch(err) {
465+
}
466+
459467
// Run the configuration
460468
runner.run(config);
461469
}

0 commit comments

Comments
 (0)