• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// This test makes sure that when using --abort-on-uncaught-exception and
4// when throwing an error from within a domain that has an error handler
5// setup, the process _does not_ abort.
6
7const common = require('../common');
8
9const assert = require('assert');
10const domain = require('domain');
11const child_process = require('child_process');
12
13const tests = [
14  function nextTick() {
15    const d = domain.create();
16
17    d.once('error', common.mustCall());
18
19    d.run(function() {
20      process.nextTick(function() {
21        throw new Error('exceptional!');
22      });
23    });
24  },
25
26  function timer() {
27    const d = domain.create();
28
29    d.on('error', common.mustCall());
30
31    d.run(function() {
32      setTimeout(function() {
33        throw new Error('exceptional!');
34      }, 33);
35    });
36  },
37
38  function immediate() {
39    const d = domain.create();
40
41    d.on('error', common.mustCall());
42
43    d.run(function() {
44      setImmediate(function() {
45        throw new Error('boom!');
46      });
47    });
48  },
49
50  function timerPlusNextTick() {
51    const d = domain.create();
52
53    d.on('error', common.mustCall());
54
55    d.run(function() {
56      setTimeout(function() {
57        process.nextTick(function() {
58          throw new Error('exceptional!');
59        });
60      }, 33);
61    });
62  },
63
64  function firstRun() {
65    const d = domain.create();
66
67    d.on('error', common.mustCall());
68
69    d.run(function() {
70      throw new Error('exceptional!');
71    });
72  },
73
74  function fsAsync() {
75    const d = domain.create();
76
77    d.on('error', common.mustCall());
78
79    d.run(function() {
80      const fs = require('fs');
81      fs.exists('/non/existing/file', function onExists(exists) {
82        throw new Error('boom!');
83      });
84    });
85  },
86
87  function netServer() {
88    const net = require('net');
89    const d = domain.create();
90
91    d.on('error', common.mustCall());
92
93    d.run(function() {
94      const server = net.createServer(function(conn) {
95        conn.pipe(conn);
96      });
97      server.listen(0, common.localhostIPv4, function() {
98        const conn = net.connect(this.address().port, common.localhostIPv4);
99        conn.once('data', function() {
100          throw new Error('ok');
101        });
102        conn.end('ok');
103        server.close();
104      });
105    });
106  },
107
108  function firstRunOnlyTopLevelErrorHandler() {
109    const d = domain.create();
110    const d2 = domain.create();
111
112    d.on('error', common.mustCall());
113
114    d.run(function() {
115      d2.run(function() {
116        throw new Error('boom!');
117      });
118    });
119  },
120
121  function firstRunNestedWithErrorHandler() {
122    const d = domain.create();
123    const d2 = domain.create();
124
125    d2.on('error', common.mustCall());
126
127    d.run(function() {
128      d2.run(function() {
129        throw new Error('boom!');
130      });
131    });
132  },
133
134  function timeoutNestedWithErrorHandler() {
135    const d = domain.create();
136    const d2 = domain.create();
137
138    d2.on('error', common.mustCall());
139
140    d.run(function() {
141      d2.run(function() {
142        setTimeout(function() {
143          console.log('foo');
144          throw new Error('boom!');
145        }, 33);
146      });
147    });
148  },
149
150  function setImmediateNestedWithErrorHandler() {
151    const d = domain.create();
152    const d2 = domain.create();
153
154    d2.on('error', common.mustCall());
155
156    d.run(function() {
157      d2.run(function() {
158        setImmediate(function() {
159          throw new Error('boom!');
160        });
161      });
162    });
163  },
164
165  function nextTickNestedWithErrorHandler() {
166    const d = domain.create();
167    const d2 = domain.create();
168
169    d2.on('error', common.mustCall());
170
171    d.run(function() {
172      d2.run(function() {
173        process.nextTick(function() {
174          throw new Error('boom!');
175        });
176      });
177    });
178  },
179
180  function fsAsyncNestedWithErrorHandler() {
181    const d = domain.create();
182    const d2 = domain.create();
183
184    d2.on('error', common.mustCall());
185
186    d.run(function() {
187      d2.run(function() {
188        const fs = require('fs');
189        fs.exists('/non/existing/file', function onExists(exists) {
190          throw new Error('boom!');
191        });
192      });
193    });
194  },
195];
196
197if (process.argv[2] === 'child') {
198  const testIndex = +process.argv[3];
199
200  tests[testIndex]();
201
202} else {
203
204  tests.forEach(function(test, testIndex) {
205    let testCmd = '';
206    if (!common.isWindows) {
207      // Do not create core files, as it can take a lot of disk space on
208      // continuous testing and developers' machines
209      testCmd += 'ulimit -c 0 && ';
210    }
211
212    testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception ` +
213               `"${process.argv[1]}" child ${testIndex}`;
214
215    try {
216      child_process.execSync(testCmd);
217    } catch (e) {
218      assert.fail(`Test index ${testIndex} failed: ${e}`);
219    }
220  });
221}
222