Skip to content

Commit da66166

Browse files
Julien GilliMyles Borins
Julien Gilli
authored and
Myles Borins
committed
test: fix test-domain-exit-dispose-again
test-domain-exit-dispose-again had been written for node v0.10.x, and was using the fact that callbacks scheduled with `process.nextTick` wouldn't run if the domain attached to it was disposed. This is not longer the case, and as a result the test would not catch any regression: it would always pass. This change rewrites that test to check that the current domain is cleared properly when processing the rest of the timers list if a timer's callback throws an error. This makes the test fail without the original fix, and pass with the original fix, as expected. PR: #3991 PR-URL: #3991 Reviewed-By: Trevor Norris <[email protected]>
1 parent 3a48f00 commit da66166

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

test/simple/test-domain-exit-dispose-again.js

+21-38
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,41 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
var common = require('../common.js');
22+
var common = require('../common');
2323
var assert = require('assert');
2424
var domain = require('domain');
25-
var disposalFailed = false;
2625

27-
// no matter what happens, we should increment a 10 times.
28-
var a = 0;
29-
log();
30-
function log(){
31-
console.log(a++, process.domain);
32-
if (a < 10) setTimeout(log, 20);
33-
}
34-
35-
var secondTimerRan = false;
36-
37-
// in 50ms we'll throw an error.
26+
// Use the same timeout value so that both timers' callbacks are called during
27+
// the same invocation of the underlying native timer's callback (listOnTimeout
28+
// in lib/timers.js).
3829
setTimeout(err, 50);
39-
setTimeout(secondTimer, 50);
40-
function err(){
30+
setTimeout(common.mustCall(secondTimer), 50);
31+
32+
function err() {
4133
var d = domain.create();
42-
d.on('error', handle);
34+
d.on('error', handleDomainError);
4335
d.run(err2);
4436

4537
function err2() {
46-
// this timeout should never be called, since the domain gets
47-
// disposed when the error happens.
48-
setTimeout(function() {
49-
console.error('This should not happen.');
50-
disposalFailed = true;
51-
process.exit(1);
52-
});
53-
5438
// this function doesn't exist, and throws an error as a result.
5539
err3();
5640
}
5741

58-
function handle(e) {
59-
// this should clean up everything properly.
60-
d.dispose();
61-
console.error(e);
62-
console.error('in handler', process.domain, process.domain === d);
42+
function handleDomainError(e) {
43+
// In the domain's error handler, the current active domain should be the
44+
// domain within which the error was thrown.
45+
assert.equal(process.domain, d);
6346
}
6447
}
6548

6649
function secondTimer() {
67-
console.log('In second timer');
68-
secondTimerRan = true;
50+
// secondTimer was scheduled before any domain had been created, so its
51+
// callback should not have any active domain set when it runs.
52+
// Do not use assert here, as it throws errors and if a domain with an error
53+
// handler is active, then asserting wouldn't make the test fail.
54+
if (process.domain !== null) {
55+
console.log('process.domain should be null, but instead is:',
56+
process.domain);
57+
process.exit(1);
58+
}
6959
}
70-
71-
process.on('exit', function() {
72-
assert.equal(a, 10);
73-
assert.equal(disposalFailed, false);
74-
assert(secondTimerRan);
75-
console.log('ok');
76-
});

0 commit comments

Comments
 (0)