|
1 | 1 | 'use strict';
|
2 |
| -var common = require('../common'); |
3 |
| -var assert = require('assert'); |
4 |
| -var domain = require('domain'); |
5 |
| -var disposalFailed = false; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const domain = require('domain'); |
6 | 5 |
|
7 |
| -// no matter what happens, we should increment a 10 times. |
8 |
| -var a = 0; |
9 |
| -log(); |
10 |
| -function log() { |
11 |
| - console.log(a++, process.domain); |
12 |
| - if (a < 10) setTimeout(log, 20); |
13 |
| -} |
14 |
| - |
15 |
| -var secondTimerRan = false; |
16 |
| - |
17 |
| -// in 50ms we'll throw an error. |
| 6 | +// Use the same timeout value so that both timers' callbacks are called during |
| 7 | +// the same invocation of the underlying native timer's callback (listOnTimeout |
| 8 | +// in lib/timers.js). |
18 | 9 | setTimeout(err, 50);
|
19 |
| -setTimeout(secondTimer, 50); |
| 10 | +setTimeout(common.mustCall(secondTimer), 50); |
| 11 | + |
20 | 12 | function err() {
|
21 |
| - var d = domain.create(); |
22 |
| - d.on('error', handle); |
| 13 | + const d = domain.create(); |
| 14 | + d.on('error', handleDomainError); |
23 | 15 | d.run(err2);
|
24 | 16 |
|
25 | 17 | function err2() {
|
26 |
| - // this timeout should never be called, since the domain gets |
27 |
| - // disposed when the error happens. |
28 |
| - setTimeout(function() { |
29 |
| - console.error('This should not happen.'); |
30 |
| - disposalFailed = true; |
31 |
| - process.exit(1); |
32 |
| - }); |
33 |
| - |
34 | 18 | // this function doesn't exist, and throws an error as a result.
|
35 | 19 | err3();
|
36 | 20 | }
|
37 | 21 |
|
38 |
| - function handle(e) { |
39 |
| - // this should clean up everything properly. |
40 |
| - d.dispose(); |
41 |
| - console.error(e); |
42 |
| - console.error('in handler', process.domain, process.domain === d); |
| 22 | + function handleDomainError(e) { |
| 23 | + // In the domain's error handler, the current active domain should be the |
| 24 | + // domain within which the error was thrown. |
| 25 | + assert.equal(process.domain, d); |
43 | 26 | }
|
44 | 27 | }
|
45 | 28 |
|
46 | 29 | function secondTimer() {
|
47 |
| - console.log('In second timer'); |
48 |
| - secondTimerRan = true; |
| 30 | + // secondTimer was scheduled before any domain had been created, so its |
| 31 | + // callback should not have any active domain set when it runs. |
| 32 | + // Do not use assert here, as it throws errors and if a domain with an error |
| 33 | + // handler is active, then asserting wouldn't make the test fail. |
| 34 | + if (process.domain !== null) { |
| 35 | + console.log('process.domain should be null, but instead is:', |
| 36 | + process.domain); |
| 37 | + process.exit(1); |
| 38 | + } |
49 | 39 | }
|
50 |
| - |
51 |
| -process.on('exit', function() { |
52 |
| - assert.equal(a, 10); |
53 |
| - assert.equal(disposalFailed, false); |
54 |
| - assert(secondTimerRan); |
55 |
| - console.log('ok'); |
56 |
| -}); |
0 commit comments