Skip to content

Commit

Permalink
Make callbacks to be executed in right domain
Browse files Browse the repository at this point in the history
When command is executed through non executed commands queue,
callbacks are executed in wrong domain. Fix this.
  • Loading branch information
Anton Kotenko committed Jun 12, 2014
1 parent 4e39078 commit 66f8987
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/mongodb/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions test/runners/standalone_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
131 changes: 131 additions & 0 deletions test/tests/functional/domain_tests.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
}

0 comments on commit 66f8987

Please sign in to comment.