summaryrefslogtreecommitdiff
path: root/test/parallel/test-domain-emit-error-handler-stack.js
blob: d42e47097992582300873d2d549b6d9da3a052a1 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const EventEmitter = require('events');

/*
 * Make sure that the domains stack and the active domain is setup properly when
 * a domain's error handler is called due to an error event being emitted.
 * More specifically, we want to test that:
 * - the active domain in the domain's error handler is *not* that domain, *but*
 *   the active domain is a any direct parent domain at the time the error was
 *   emitted.
 * - the domains stack in the domain's error handler does *not* include that
 *   domain, *but* it includes all parents of that domain when the error was
 *   emitted.
 */
const d1 = domain.create();
const d2 = domain.create();
const d3 = domain.create();

function checkExpectedDomains(err) {
  // First, check that domains stack and active domain is as expected when the
  // event handler is called synchronously via ee.emit('error').
  if (domain._stack.length !== err.expectedStackLength) {
    console.error('expected domains stack length of %d, but instead is %d',
                  err.expectedStackLength, domain._stack.length);
    process.exit(1);
  }

  if (process.domain !== err.expectedActiveDomain) {
    console.error('expected active domain to be %j, but instead is %j',
                  err.expectedActiveDomain, process.domain);
    process.exit(1);
  }

  // Then make sure that the domains stack and active domain is setup as
  // expected when executing a callback scheduled via nextTick from the error
  // handler.
  process.nextTick(() => {
    const expectedStackLengthInNextTickCb =
            err.expectedStackLength > 0 ? 1 : 0;
    if (domain._stack.length !== expectedStackLengthInNextTickCb) {
      console.error('expected stack length in nextTick cb to be %d, ' +
                'but instead is %d', expectedStackLengthInNextTickCb,
                    domain._stack.length);
      process.exit(1);
    }

    const expectedActiveDomainInNextTickCb =
            expectedStackLengthInNextTickCb === 0 ? undefined :
              err.expectedActiveDomain;
    if (process.domain !== expectedActiveDomainInNextTickCb) {
      console.error('expected active domain in nextTick cb to be %j, ' +
                'but instead is %j', expectedActiveDomainInNextTickCb,
                    process.domain);
      process.exit(1);
    }
  });
}

d1.on('error', common.mustCall((err) => {
  checkExpectedDomains(err);
}, 2));

d2.on('error', common.mustCall((err) => {
  checkExpectedDomains(err);
}, 2));

d3.on('error', common.mustCall((err) => {
  checkExpectedDomains(err);
}, 1));

d1.run(() => {
  const ee = new EventEmitter();
  assert.strictEqual(process.domain, d1);
  assert.strictEqual(domain._stack.length, 1);

  const err = new Error('oops');
  err.expectedStackLength = 0;
  err.expectedActiveDomain = null;
  ee.emit('error', err);

  assert.strictEqual(process.domain, d1);
  assert.strictEqual(domain._stack.length, 1);
});

d1.run(() => {
  d1.run(() => {
    const ee = new EventEmitter();

    assert.strictEqual(process.domain, d1);
    assert.strictEqual(domain._stack.length, 2);

    const err = new Error('oops');
    err.expectedStackLength = 0;
    err.expectedActiveDomain = null;
    ee.emit('error', err);

    assert.strictEqual(process.domain, d1);
    assert.strictEqual(domain._stack.length, 2);
  });
});

d1.run(() => {
  d2.run(() => {
    const ee = new EventEmitter();

    assert.strictEqual(process.domain, d2);
    assert.strictEqual(domain._stack.length, 2);

    const err = new Error('oops');
    err.expectedStackLength = 1;
    err.expectedActiveDomain = d1;
    ee.emit('error', err);

    assert.strictEqual(process.domain, d2);
    assert.strictEqual(domain._stack.length, 2);
  });
});

d1.run(() => {
  d2.run(() => {
    d2.run(() => {
      const ee = new EventEmitter();

      assert.strictEqual(process.domain, d2);
      assert.strictEqual(domain._stack.length, 3);

      const err = new Error('oops');
      err.expectedStackLength = 1;
      err.expectedActiveDomain = d1;
      ee.emit('error', err);

      assert.strictEqual(process.domain, d2);
      assert.strictEqual(domain._stack.length, 3);
    });
  });
});

d3.run(() => {
  d1.run(() => {
    d3.run(() => {
      d3.run(() => {
        const ee = new EventEmitter();

        assert.strictEqual(process.domain, d3);
        assert.strictEqual(domain._stack.length, 4);

        const err = new Error('oops');
        err.expectedStackLength = 2;
        err.expectedActiveDomain = d1;
        ee.emit('error', err);

        assert.strictEqual(process.domain, d3);
        assert.strictEqual(domain._stack.length, 4);
      });
    });
  });
});