Skip to content

Commit

Permalink
A fix for connection error handling when using the SSL on MongoDB, te…
Browse files Browse the repository at this point in the history
…st fixes, update History and package.json
  • Loading branch information
christkv committed Feb 13, 2012
1 parent 0fc6a75 commit b85a2b0
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 30 deletions.
5 changes: 3 additions & 2 deletions HISTORY
@@ -1,9 +1,10 @@
0.9.9
------------------
0.9.9 2012-02-13
----------------
* Added createFromTime method on ObjectID to allow for queries against _id more easily using the timestamp.
* Db.close(true) now makes connection unusable as it's been force closed by app.
* Fixed mapReduce and group functions to correctly send slaveOk on queries.
* Fixes for find method to correctly work with find(query, fields, callback) (Issue #506).
* A fix for connection error handling when using the SSL on MongoDB.

0.9.8-7 2012-02-06
------------------
Expand Down
8 changes: 4 additions & 4 deletions lib/mongodb/connection/connection.js
Expand Up @@ -49,7 +49,7 @@ Connection.prototype.start = function() {
// If we have a normal connection
if(this.socketOptions.ssl) {
// Create a new stream
this.connection = new net.Stream();
this.connection = new net.Socket();
// Set options on the socket
this.connection.setTimeout(this.socketOptions.timeout);
// Work around for 0.4.X
Expand All @@ -62,9 +62,9 @@ Connection.prototype.start = function() {
this.connection.setKeepAlive(false);
}
}

// Set up pair for tls with server, accept self-signed certificates as well
this.pair = tls.createSecurePair(false);
var pair = this.pair = tls.createSecurePair(false);
// Set up encrypted streams
this.pair.encrypted.pipe(this.connection);
this.connection.pipe(this.pair.encrypted);
Expand All @@ -76,7 +76,7 @@ Connection.prototype.start = function() {
this.pair.cleartext.on("data", createDataHandler(this));
// Add handlers
this.connection.on("error", errorHandler(this));
// Add all handlers to the socket to manage it
// this.connection.on("connect", connectHandler(this));
this.connection.on("end", endHandler(this));
this.connection.on("timeout", timeoutHandler(this));
this.connection.on("drain", drainHandler(this));
Expand Down
3 changes: 1 addition & 2 deletions lib/mongodb/connection/connection_pool.js
Expand Up @@ -92,8 +92,7 @@ var _connect = function(_self) {
connection.on("error", function(err, connection) {
numberOfErrors++;
// If we are already disconnected ignore the event
if(connectionStatus == 'disconnected' && _self.listeners("error").length > 0) {
// if(connectionStatus == 'connected') {
if(connectionStatus != 'disconnected' && _self.listeners("error").length > 0) {
_self.emit("error", err);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/mongodb/connection/server.js
Expand Up @@ -201,7 +201,7 @@ Server.prototype.connect = function(dbInstance, options, callback) {
var returnIsMasterResults = options.returnIsMasterResults == null ? false : options.returnIsMasterResults;

// Create a default connect handler, overriden when using replicasets
var connectCallback = function(err, reply) {
var connectCallback = function(err, reply) {
// ensure no callbacks get called twice
var internalCallback = callback;
callback = null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{ "name" : "mongodb"
, "description" : "A node.js driver for MongoDB"
, "keywords" : ["mongodb", "mongo", "driver", "db"]
, "version" : "0.9.8-7"
, "version" : "0.9.9"
, "author" : "Christian Amor Kvalheim <christkv@gmail.com>"
, "contributors" : [ "Aaron Heckmann",
"Christoph Pojer",
Expand Down
2 changes: 1 addition & 1 deletion test/connect_test.js
Expand Up @@ -129,7 +129,7 @@ exports.testConnectBadUrl = function(test) {
*/
exports.shouldCorrectlyDoSimpleCountExamples = function(test) {
// Connect to the server
Db.connect('mongodb://localhost:27017/integration_tests', function(err, db) {
Db.connect('mongodb://localhost:27017/integration_tests' + (useSSL == true ? '?ssl=true' : ''), function(err, db) {
test.equal(null, err);

db.close();
Expand Down
2 changes: 1 addition & 1 deletion test/db_test.js
Expand Up @@ -584,7 +584,7 @@ exports.shouldCorrectlyRetrieveAllCollections = function(test) {
* @ignore
*/
exports.shouldCorrectlyHandleFailedConnection = function(test) {
var fs_client = new Db(MONGODB, new Server("127.0.0.1", 27117, {auto_reconnect: false, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)});
var fs_client = new Db(MONGODB, new Server("127.0.0.1", 25117, {auto_reconnect: false, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)});
fs_client.open(function(err, fs_client) {
test.ok(err != null)
test.done();
Expand Down
6 changes: 3 additions & 3 deletions test/find_test.js
Expand Up @@ -908,7 +908,7 @@ exports.shouldCorrectlyFindAndModifyDocumentThatFailsInFirstStep = function(test
* @ignore
*/
exports.shouldCorrectlyFindAndModifyDocumentThatFailsInSecondStepWithNoMatchingDocuments = function(test) {
var p_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true}), {strict:true, native_parser: (process.env['TEST_NATIVE'] != null)});
var p_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, ssl:useSSL}), {strict:true, native_parser: (process.env['TEST_NATIVE'] != null)});
p_client.open(function(err, p_client) {
p_client.createCollection('shouldCorrectlyFindAndModifyDocumentThatFailsInSecondStepWithNoMatchingDocuments', function(err, collection) {
// Test return old document on change
Expand Down Expand Up @@ -1182,7 +1182,7 @@ exports.shouldCorrectlyReturnErrorFromMongodbOnFindAndModifyForcedError = functi
* @ignore
*/
exports.shouldCorrectlyExecuteFindAndModifyUnderConcurrentLoad = function(test) {
var p_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize:10}), {native_parser: (process.env['TEST_NATIVE'] != null)});
var p_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize:10, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)});
var running = true;

p_client.open(function(err, p_client) {
Expand Down Expand Up @@ -1220,7 +1220,7 @@ exports.shouldCorrectlyExecuteFindAndModifyUnderConcurrentLoad = function(test)
* @ignore
*/
exports.shouldCorrectlyIterateOverCollection = function(test) {
var p_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize:1}), {native_parser: (process.env['TEST_NATIVE'] != null)});
var p_client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize:1, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)});
var numberOfSteps = 0;

// Open db connection
Expand Down
2 changes: 1 addition & 1 deletion test/reaper_test.js
Expand Up @@ -50,7 +50,7 @@ exports.tearDown = function(callback) {
}

exports.shouldCorrectlySaveUnicodeContainingDocument = function(test) {
var reaperClient = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false}), {reaper:true, native_parser: (process.env['TEST_NATIVE'] != null)});
var reaperClient = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: false, ssl:useSSL}), {reaper:true, native_parser: (process.env['TEST_NATIVE'] != null)});
reaperClient.open(function(err, reaperClient) {
reaperClient._lastReaperTimestamp = (new Date().getTime() - 1000000);
var con = reaperClient.serverConfig.checkoutReader();
Expand Down
23 changes: 11 additions & 12 deletions test/replicaset/map_reduce_test.js
Expand Up @@ -28,14 +28,6 @@ var ensureConnection = function(test, numberOfTries, callback) {
if(numberOfTries <= 0) return callback(new Error("could not connect correctly"), null);

var db = new Db('integration_test_', replSet);
// // Print any errors
// db.on("error", function(err) {
// console.log("============================= ensureConnection caught error")
// console.dir(err)
// if(err != null && err.stack != null) console.log(err.stack)
// db.close();
// })

// Open the db
db.open(function(err, p_db) {
// Close connections
Expand Down Expand Up @@ -63,7 +55,7 @@ exports.setUp = function(callback) {
// Create instance of replicaset manager but only for the first call
if(!serversUp && !noReplicasetStart) {
serversUp = true;
RS = new ReplicaSetManager({retries:120, secondary_count:2, passive_count:1, arbiter_count:1});
RS = new ReplicaSetManager({retries:120, secondary_count:2, passive_count:0, arbiter_count:1});
RS.startSet(true, function(err, result) {
if(err != null) throw err;
// Finish setup
Expand Down Expand Up @@ -111,7 +103,7 @@ exports['Should Correctly group using replicaset'] = function(test) {
db.open(function(err, p_db) {
if(err != null) debug("shouldGroup :: " + inspect(err));

p_db.createCollection("testgroup", {safe:{w:2, wtimeout:10000}}, function(err, collection) {
p_db.createCollection("testgroup_replicaset", {safe:{w:2, wtimeout:10000}}, function(err, collection) {
if(err != null) debug("shoulGroup :: " + inspect(err));

collection.insert([{key:1,x:10}, {key:2,x:30}, {key:1,x:20}, {key:3,x:20}], {safe:{w:2, wtimeout:10000}}, function(err, result) {
Expand Down Expand Up @@ -159,7 +151,10 @@ exports.shouldPerformMapReduceFunctionInline = function(test) {
if(parseInt((result.version.replace(/\./g, ''))) >= 176) {

// Create a test collection
db.createCollection('test_map_reduce_functions_inline', {safe:{w:2, wtimeout:10000}}, function(err, collection) {
db.createCollection('test_map_reduce_functions_inline_map_reduce', {safe:{w:2, wtimeout:10000}}, function(err, collection) {
// console.log("==================================================================================")
// console.dir(err)


// Insert some test documents
collection.insert([{'user_id':1}, {'user_id':2}], {safe:true}, function(err, r) {
Expand All @@ -171,6 +166,10 @@ exports.shouldPerformMapReduceFunctionInline = function(test) {

// Execute map reduce and return results inline
collection.mapReduce(map, reduce, {out : {inline: 1}}, function(err, results) {
// console.log("=============================================================================")
// console.dir(err)
// console.dir(results)

test.equal(2, results.length);

db.close();
Expand Down Expand Up @@ -207,7 +206,7 @@ exports.shouldFailToDoMapReduceToOutCollection = function(test) {
if(parseInt((result.version.replace(/\./g, ''))) >= 176) {

// Create a test collection
db.createCollection('test_map_reduce_functions_notInline', {safe:{w:2, wtimeout:10000}}, function(err, collection) {
db.createCollection('test_map_reduce_functions_notInline_map_reduce', {safe:{w:2, wtimeout:10000}}, function(err, collection) {

// Insert some test documents
collection.insert([{'user_id':1}, {'user_id':2}], {safe:true}, function(err, r) {
Expand Down
4 changes: 2 additions & 2 deletions test/tools/replica_set_manager.js
Expand Up @@ -93,7 +93,7 @@ ReplicaSetManager.prototype.startSet = function(killall, callback) {
debug("** Starting a replica set with " + this.count + " nodes");

// Kill all existing mongod instances
exec(killall ? 'killall mongod' : '', function(err, stdout, stderr) {
exec(killall ? 'killall -9 mongod' : '', function(err, stdout, stderr) {
var n = 0;
var tagsIndex = 0;

Expand Down Expand Up @@ -234,7 +234,7 @@ ReplicaSetManager.prototype.initNode = function(n, fields, callback) {
}

ReplicaSetManager.prototype.killAll = function(callback) {
exec('killall mongod', function(err, stdout, stderr) {
exec('killall -9 mongod', function(err, stdout, stderr) {
return callback();
});
}
Expand Down

0 comments on commit b85a2b0

Please sign in to comment.