summaryrefslogtreecommitdiff
path: root/test/parallel/test-domain-top-level-error-handler-clears-stack.js
blob: 05d5fca46718260f5e9698131d2720926c6240ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';

const common = require('../common');
const domain = require('domain');

/*
 * Make sure that the domains stack is cleared after a top-level domain
 * error handler exited gracefully.
 */
const d = domain.create();

d.on('error', common.mustCall(() => {
  process.nextTick(() => {
    // Scheduling a callback with process.nextTick will enter a _new_ domain,
    // and the callback will be called after the domain that handled the error
    // was exited. So there should be only one domain on the domains stack if
    // the domains stack was cleared properly when the domain error handler
    // returned.
    if (domain._stack.length !== 1) {
      // Do not use assert to perform this test: this callback runs in a
      // different callstack as the original process._fatalException that
      // handled the original error, thus throwing here would trigger another
      // call to process._fatalException, and so on recursively and
      // indefinitely.
      console.error('domains stack length should be 1, but instead is:',
                    domain._stack.length);
      process.exit(1);
    }
  });
}));

d.run(() => {
  throw new Error('Error from domain');
});