|
19 | 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21 | 21 |
|
22 |
| -var common = require('../common.js'); |
| 22 | +var common = require('../common'); |
23 | 23 | var assert = require('assert');
|
24 | 24 | var domain = require('domain');
|
25 |
| -var disposalFailed = false; |
26 | 25 |
|
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). |
38 | 29 | setTimeout(err, 50);
|
39 |
| -setTimeout(secondTimer, 50); |
40 |
| -function err(){ |
| 30 | +setTimeout(common.mustCall(secondTimer), 50); |
| 31 | + |
| 32 | +function err() { |
41 | 33 | var d = domain.create();
|
42 |
| - d.on('error', handle); |
| 34 | + d.on('error', handleDomainError); |
43 | 35 | d.run(err2);
|
44 | 36 |
|
45 | 37 | 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 |
| - |
54 | 38 | // this function doesn't exist, and throws an error as a result.
|
55 | 39 | err3();
|
56 | 40 | }
|
57 | 41 |
|
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); |
63 | 46 | }
|
64 | 47 | }
|
65 | 48 |
|
66 | 49 | 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 | + } |
69 | 59 | }
|
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