• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// This test makes sure that when throwing from within a timer's callback,
4// its active domain at the time of the throw is not the process' active domain
5// for the next timers that need to be processed on the same turn of the event
6// loop.
7
8const common = require('../common');
9const assert = require('assert');
10const domain = require('domain');
11
12// Use the same timeout value so that both timers' callbacks are called during
13// the same invocation of the underlying native timer's callback (listOnTimeout
14// in lib/timers.js).
15setTimeout(err, 50);
16setTimeout(common.mustCall(secondTimer), 50);
17
18function err() {
19  const d = domain.create();
20  d.on('error', handleDomainError);
21  d.run(err2);
22
23  function err2() {
24    // This function doesn't exist, and throws an error as a result.
25    err3(); // eslint-disable-line no-undef
26  }
27
28  function handleDomainError(e) {
29    // In the domain's error handler, the current active domain should be the
30    // domain within which the error was thrown.
31    assert.strictEqual(process.domain, d);
32  }
33}
34
35function secondTimer() {
36  // secondTimer was scheduled before any domain had been created, so its
37  // callback should not have any active domain set when it runs.
38  if (process.domain !== null) {
39    console.log('process.domain should be null in this timer callback, but is:',
40                process.domain);
41    // Do not use assert here, as it throws errors and if a domain with an error
42    // handler is active, then asserting wouldn't make the test fail.
43    process.exit(1);
44  }
45}
46