Skip to content

Commit 66f8987

Browse files
author
Anton Kotenko
committed
Make callbacks to be executed in right domain
When command is executed through non executed commands queue, callbacks are executed in wrong domain. Fix this.
1 parent 4e39078 commit 66f8987

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

lib/mongodb/db.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,7 @@ Db.prototype._executeQueryCommand = function(db_command, options, callback) {
17701770
callback = options;
17711771
options = {};
17721772
}
1773+
callback = bindToCurrentDomain(callback);
17731774

17741775
// fast fail option used for HA, no retry
17751776
var failFast = options['failFast'] != null
@@ -1826,7 +1827,7 @@ Db.prototype._executeQueryCommand = function(db_command, options, callback) {
18261827
{ type: 'query'
18271828
, db_command: db_command
18281829
, options: options
1829-
, callback: callback
1830+
, callback: callback
18301831
, db: self
18311832
, executeQueryCommand: __executeQueryCommand
18321833
, executeInsertCommand: __executeInsertCommand
@@ -1924,7 +1925,7 @@ Db.prototype._executeInsertCommand = function(db_command, options, callback) {
19241925
callback = options;
19251926
options = {};
19261927
}
1927-
1928+
callback = bindToCurrentDomain(callback);
19281929
// Ensure options are not null
19291930
options = options == null ? {} : options;
19301931

test/runners/standalone_runner.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = function(configurations) {
6060
, '/test/tests/functional/connection_tests.js'
6161
, '/test/tests/functional/collection_tests.js'
6262
, '/test/tests/functional/db_tests.js'
63+
, '/test/tests/functional/domain_tests.js'
6364
, '/test/tests/functional/read_preferences_tests.js'
6465
// , '/test/tests/functional/fluent_api/aggregation_tests.js'
6566
, '/test/tests/functional/maxtimems_tests.js'

test/tests/functional/domain_tests.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @ignore
3+
*/
4+
exports.shouldStayInCorrectDomainForReadCommand = function(configuration, test) {
5+
var Domain;
6+
7+
try {
8+
Domain = require('domain');
9+
} catch (e) {
10+
//Old node versions. Nothing to test
11+
test.done();
12+
return;
13+
}
14+
15+
var domainInstance = Domain.create();
16+
var client = configuration.newDbInstance({w: 0}, {poolSize: 1, auto_reconnect: true});
17+
18+
client.open(function(err, client) {
19+
test.ok(!err);
20+
var collection = client.collection('test');
21+
domainInstance.run(function() {
22+
collection.count({}, function(err) {
23+
test.ok(!err);
24+
test.ok(domainInstance === process.domain);
25+
test.done();
26+
});
27+
});
28+
});
29+
}
30+
31+
/**
32+
* @ignore
33+
*/
34+
exports.shouldStayInCorrectDomainForWriteCommand = function(configuration, test) {
35+
var Domain;
36+
37+
try {
38+
Domain = require('domain');
39+
} catch (e) {
40+
//Old node versions. Nothing to test
41+
test.done();
42+
return;
43+
}
44+
45+
var domainInstance = Domain.create();
46+
var client = configuration.newDbInstance({w: 1}, {poolSize: 1, auto_reconnect: true});
47+
48+
client.open(function(err, client) {
49+
test.ok(!err);
50+
var collection = client.collection('test');
51+
domainInstance.run(function() {
52+
collection.insert({field: 123}, function(err) {
53+
test.ok(!err);
54+
test.ok(domainInstance === process.domain);
55+
test.done();
56+
});
57+
});
58+
});
59+
}
60+
61+
62+
/**
63+
* @ignore
64+
*/
65+
exports.shouldStayInCorrectDomainForQueuedReadCommand = function(configuration, test) {
66+
var Domain;
67+
68+
try {
69+
Domain = require('domain');
70+
} catch (e) {
71+
//Old node versions. Nothing to test
72+
test.done();
73+
return;
74+
}
75+
76+
var domainInstance = Domain.create();
77+
var client = configuration.newDbInstance({w: 0}, {poolSize: 1, auto_reconnect: true});
78+
79+
client.open(function(err, client) {
80+
var connection = client.serverConfig.connectionPool.openConnections[0];
81+
var collection = client.collection('test');
82+
83+
//imitate connection error, to make commands queued into
84+
//commandsStore
85+
connection.emit('error', {err: 'fake disconnect'}, connection);
86+
87+
domainInstance.run(function() {
88+
collection.count({}, function(err) {
89+
test.ok(!err);
90+
test.ok(process.domain === domainInstance);
91+
test.done();
92+
});
93+
});
94+
});
95+
}
96+
97+
/**
98+
* @ignore
99+
*/
100+
exports.shouldStayInCorrectDomainForQueuedWriteCommand = function(configuration, test) {
101+
var Domain;
102+
103+
try {
104+
Domain = require('domain');
105+
} catch (e) {
106+
//Old node versions. Nothing to test
107+
test.done();
108+
return;
109+
}
110+
111+
var domainInstance = Domain.create();
112+
var client = configuration.newDbInstance({w: 1}, {poolSize: 1, auto_reconnect: true});
113+
114+
client.open(function(err, client) {
115+
test.ok(!err);
116+
var connection = client.serverConfig.connectionPool.openConnections[0];
117+
var collection = client.collection('test');
118+
119+
//imitate connection error, to make commands queued into
120+
//commandsStore
121+
connection.emit('error', {err: 'fake disconnect'}, connection);
122+
123+
domainInstance.run(function() {
124+
collection.insert({field: 123}, function(err) {
125+
test.ok(!err);
126+
test.ok(process.domain === domainInstance);
127+
test.done();
128+
});
129+
});
130+
});
131+
}

0 commit comments

Comments
 (0)