summaryrefslogtreecommitdiff
path: root/test/parallel/test-domain-uncaught-exception.js
blob: 0d6465fbf3f42ffd569a3a75236e1385642960f1 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';

/*
 * The goal of this test is to make sure that errors thrown within domains
 * are handled correctly. It checks that the process' 'uncaughtException' event
 * is emitted when appropriate, and not emitted when it shouldn't. It also
 * checks that the proper domain error handlers are called when they should
 * be called, and not called when they shouldn't.
 */

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

const tests = [];

function test1() {
  /*
   * Throwing from an async callback from within a domain that doesn't have
   * an error handler must result in emitting the process' uncaughtException
   * event.
   */
  const d = domain.create();
  d.run(function() {
    setTimeout(function onTimeout() {
      throw new Error('boom!');
    }, 1);
  });
}

tests.push({
  fn: test1,
  expectedMessages: ['uncaughtException']
});

function test2() {
  /*
   * Throwing from from within a domain that doesn't have an error handler must
   * result in emitting the process' uncaughtException event.
   */
  const d2 = domain.create();
  d2.run(function() {
    throw new Error('boom!');
  });
}

tests.push({
  fn: test2,
  expectedMessages: ['uncaughtException']
});

function test3() {
  /*
   * This test creates two nested domains: d3 and d4. d4 doesn't register an
   * error handler, but d3 does. The error is handled by the d3 domain and thus
   * an 'uncaughtException' event should _not_ be emitted.
   */
  const d3 = domain.create();
  const d4 = domain.create();

  d3.on('error', function onErrorInD3Domain() {
    process.send('errorHandledByDomain');
  });

  d3.run(function() {
    d4.run(function() {
      throw new Error('boom!');
    });
  });
}

tests.push({
  fn: test3,
  expectedMessages: ['errorHandledByDomain']
});

function test4() {
  /*
   * This test creates two nested domains: d5 and d6. d6 doesn't register an
   * error handler. When the timer's callback is called, because async
   * operations like timer callbacks are bound to the domain that was active
   * at the time of their creation, and because both d5 and d6 domains have
   * exited by the time the timer's callback is called, its callback runs with
   * only d6 on the domains stack. Since d6 doesn't register an error handler,
   * the process' uncaughtException event should be emitted.
   */
  const d5 = domain.create();
  const d6 = domain.create();

  d5.on('error', function onErrorInD2Domain() {
    process.send('errorHandledByDomain');
  });

  d5.run(function() {
    d6.run(function() {
      setTimeout(function onTimeout() {
        throw new Error('boom!');
      }, 1);
    });
  });
}

tests.push({
  fn: test4,
  expectedMessages: ['uncaughtException']
});

function test5() {
  /*
   * This test creates two nested domains: d7 and d8. d8 _does_ register an
   * error handler, so throwing within that domain should not emit an uncaught
   * exception.
   */
  const d7 = domain.create();
  const d8 = domain.create();

  d8.on('error', function onErrorInD3Domain() {
    process.send('errorHandledByDomain');
  });

  d7.run(function() {
    d8.run(function() {
      throw new Error('boom!');
    });
  });
}
tests.push({
  fn: test5,
  expectedMessages: ['errorHandledByDomain']
});

function test6() {
  /*
   * This test creates two nested domains: d9 and d10. d10 _does_ register an
   * error handler, so throwing within that domain in an async callback should
   * _not_ emit an uncaught exception.
   */
  const d9 = domain.create();
  const d10 = domain.create();

  d10.on('error', function onErrorInD2Domain() {
    process.send('errorHandledByDomain');
  });

  d9.run(function() {
    d10.run(function() {
      setTimeout(function onTimeout() {
        throw new Error('boom!');
      }, 1);
    });
  });
}

tests.push({
  fn: test6,
  expectedMessages: ['errorHandledByDomain']
});

if (process.argv[2] === 'child') {
  const testIndex = process.argv[3];
  process.on('uncaughtException', function onUncaughtException() {
    process.send('uncaughtException');
  });

  tests[testIndex].fn();
} else {
  // Run each test's function in a child process. Listen on
  // messages sent by each child process and compare expected
  // messages defined for each test with the actual received messages.
  tests.forEach(function doTest(test, testIndex) {
    const testProcess = child_process.fork(__filename, ['child', testIndex]);

    testProcess.on('message', function onMsg(msg) {
      if (test.messagesReceived === undefined)
        test.messagesReceived = [];

      test.messagesReceived.push(msg);
    });

    testProcess.on('disconnect', common.mustCall(function onExit() {
      // Make sure that all expected messages were sent from the
      // child process
      test.expectedMessages.forEach(function(expectedMessage) {
        const msgs = test.messagesReceived;
        if (msgs === undefined || !msgs.includes(expectedMessage)) {
          assert.fail(`test ${test.fn.name} should have sent message: ${
            expectedMessage} but didn't`);
        }
      });

      if (test.messagesReceived) {
        test.messagesReceived.forEach(function(receivedMessage) {
          if (!test.expectedMessages.includes(receivedMessage)) {
            assert.fail(`test ${test.fn.name} should not have sent message: ${
              receivedMessage} but did`);
          }
        });
      }
    }));
  });
}