diff --git a/lib/mongodb/db.js b/lib/mongodb/db.js index 58fcfbb54f..06996138a5 100644 --- a/lib/mongodb/db.js +++ b/lib/mongodb/db.js @@ -1770,6 +1770,7 @@ Db.prototype._executeQueryCommand = function(db_command, options, callback) { callback = options; options = {}; } + callback = bindToCurrentDomain(callback); // fast fail option used for HA, no retry var failFast = options['failFast'] != null @@ -1826,7 +1827,7 @@ Db.prototype._executeQueryCommand = function(db_command, options, callback) { { type: 'query' , db_command: db_command , options: options - , callback: callback + , callback: callback , db: self , executeQueryCommand: __executeQueryCommand , executeInsertCommand: __executeInsertCommand @@ -1924,7 +1925,7 @@ Db.prototype._executeInsertCommand = function(db_command, options, callback) { callback = options; options = {}; } - + callback = bindToCurrentDomain(callback); // Ensure options are not null options = options == null ? {} : options; diff --git a/test/runners/standalone_runner.js b/test/runners/standalone_runner.js index 7ad0bac667..847ff021c8 100644 --- a/test/runners/standalone_runner.js +++ b/test/runners/standalone_runner.js @@ -60,6 +60,7 @@ module.exports = function(configurations) { , '/test/tests/functional/connection_tests.js' , '/test/tests/functional/collection_tests.js' , '/test/tests/functional/db_tests.js' + , '/test/tests/functional/domain_tests.js' , '/test/tests/functional/read_preferences_tests.js' // , '/test/tests/functional/fluent_api/aggregation_tests.js' , '/test/tests/functional/maxtimems_tests.js' diff --git a/test/tests/functional/domain_tests.js b/test/tests/functional/domain_tests.js new file mode 100644 index 0000000000..1bfb34a2f9 --- /dev/null +++ b/test/tests/functional/domain_tests.js @@ -0,0 +1,131 @@ +/** + * @ignore + */ +exports.shouldStayInCorrectDomainForReadCommand = function(configuration, test) { + var Domain; + + try { + Domain = require('domain'); + } catch (e) { + //Old node versions. Nothing to test + test.done(); + return; + } + + var domainInstance = Domain.create(); + var client = configuration.newDbInstance({w: 0}, {poolSize: 1, auto_reconnect: true}); + + client.open(function(err, client) { + test.ok(!err); + var collection = client.collection('test'); + domainInstance.run(function() { + collection.count({}, function(err) { + test.ok(!err); + test.ok(domainInstance === process.domain); + test.done(); + }); + }); + }); +} + +/** + * @ignore + */ +exports.shouldStayInCorrectDomainForWriteCommand = function(configuration, test) { + var Domain; + + try { + Domain = require('domain'); + } catch (e) { + //Old node versions. Nothing to test + test.done(); + return; + } + + var domainInstance = Domain.create(); + var client = configuration.newDbInstance({w: 1}, {poolSize: 1, auto_reconnect: true}); + + client.open(function(err, client) { + test.ok(!err); + var collection = client.collection('test'); + domainInstance.run(function() { + collection.insert({field: 123}, function(err) { + test.ok(!err); + test.ok(domainInstance === process.domain); + test.done(); + }); + }); + }); +} + + +/** + * @ignore + */ +exports.shouldStayInCorrectDomainForQueuedReadCommand = function(configuration, test) { + var Domain; + + try { + Domain = require('domain'); + } catch (e) { + //Old node versions. Nothing to test + test.done(); + return; + } + + var domainInstance = Domain.create(); + var client = configuration.newDbInstance({w: 0}, {poolSize: 1, auto_reconnect: true}); + + client.open(function(err, client) { + var connection = client.serverConfig.connectionPool.openConnections[0]; + var collection = client.collection('test'); + + //imitate connection error, to make commands queued into + //commandsStore + connection.emit('error', {err: 'fake disconnect'}, connection); + + domainInstance.run(function() { + collection.count({}, function(err) { + test.ok(!err); + test.ok(process.domain === domainInstance); + test.done(); + }); + }); + }); +} + +/** + * @ignore + */ +exports.shouldStayInCorrectDomainForQueuedWriteCommand = function(configuration, test) { + var Domain; + + try { + Domain = require('domain'); + } catch (e) { + //Old node versions. Nothing to test + test.done(); + return; + } + + var domainInstance = Domain.create(); + var client = configuration.newDbInstance({w: 1}, {poolSize: 1, auto_reconnect: true}); + + client.open(function(err, client) { + test.ok(!err); + var connection = client.serverConfig.connectionPool.openConnections[0]; + var collection = client.collection('test'); + + //imitate connection error, to make commands queued into + //commandsStore + connection.emit('error', {err: 'fake disconnect'}, connection); + + domainInstance.run(function() { + collection.insert({field: 123}, function(err) { + test.ok(!err); + test.ok(process.domain === domainInstance); + test.done(); + }); + }); + }); +}