• 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    assert.strictEqual(e.domain, d);
30    // Domains' error handlers are called outside of their domain's context, so
31    // we're not expecting any active domain here.
32    assert.strictEqual(process.domain, undefined);
33  }
34}
35
36function secondTimer() {
37  // secondTimer was scheduled before any domain had been created, so its
38  // callback should not have any active domain set when it runs.
39  if (process.domain !== null) {
40    console.log('process.domain should be null in this timer callback, but is:',
41                process.domain);
42    // Do not use assert here, as it throws errors and if a domain with an error
43    // handler is active, then asserting wouldn't make the test fail.
44    process.exit(1);
45  }
46}
47