|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* |
| 4 | + * This test makes sure that when using --abort-on-uncaught-exception and |
| 5 | + * when throwing an error from within a domain that has an error handler |
| 6 | + * setup, the process _does not_ abort. |
| 7 | + */ |
| 8 | +var common = require('../common'); |
| 9 | +var assert = require('assert'); |
| 10 | +var domain = require('domain'); |
| 11 | +var child_process = require('child_process'); |
| 12 | + |
| 13 | +var errorHandlerCalled = false; |
| 14 | + |
| 15 | +var tests = [ |
| 16 | + function nextTick() { |
| 17 | + var d = domain.create(); |
| 18 | + |
| 19 | + d.once('error', function(err) { |
| 20 | + errorHandlerCalled = true; |
| 21 | + }); |
| 22 | + |
| 23 | + d.run(function() { |
| 24 | + process.nextTick(function() { |
| 25 | + throw new Error('exceptional!'); |
| 26 | + }); |
| 27 | + }); |
| 28 | + }, |
| 29 | + |
| 30 | + function timer() { |
| 31 | + var d = domain.create(); |
| 32 | + |
| 33 | + d.on('error', function(err) { |
| 34 | + errorHandlerCalled = true; |
| 35 | + }); |
| 36 | + |
| 37 | + d.run(function() { |
| 38 | + setTimeout(function() { |
| 39 | + throw new Error('exceptional!'); |
| 40 | + }, 33); |
| 41 | + }); |
| 42 | + }, |
| 43 | + |
| 44 | + function immediate() { |
| 45 | + console.log('starting test'); |
| 46 | + var d = domain.create(); |
| 47 | + |
| 48 | + d.on('error', function errorHandler() { |
| 49 | + errorHandlerCalled = true; |
| 50 | + }); |
| 51 | + |
| 52 | + d.run(function() { |
| 53 | + setImmediate(function() { |
| 54 | + throw new Error('boom!'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }, |
| 58 | + |
| 59 | + function timerPlusNextTick() { |
| 60 | + var d = domain.create(); |
| 61 | + |
| 62 | + d.on('error', function(err) { |
| 63 | + errorHandlerCalled = true; |
| 64 | + }); |
| 65 | + |
| 66 | + d.run(function() { |
| 67 | + setTimeout(function() { |
| 68 | + process.nextTick(function() { |
| 69 | + throw new Error('exceptional!'); |
| 70 | + }); |
| 71 | + }, 33); |
| 72 | + }); |
| 73 | + }, |
| 74 | + |
| 75 | + function firstRun() { |
| 76 | + var d = domain.create(); |
| 77 | + |
| 78 | + d.on('error', function(err) { |
| 79 | + errorHandlerCalled = true; |
| 80 | + }); |
| 81 | + |
| 82 | + d.run(function() { |
| 83 | + throw new Error('exceptional!'); |
| 84 | + }); |
| 85 | + }, |
| 86 | + |
| 87 | + function fsAsync() { |
| 88 | + var d = domain.create(); |
| 89 | + |
| 90 | + d.on('error', function errorHandler() { |
| 91 | + errorHandlerCalled = true; |
| 92 | + }); |
| 93 | + |
| 94 | + d.run(function() { |
| 95 | + var fs = require('fs'); |
| 96 | + fs.exists('/non/existing/file', function onExists(exists) { |
| 97 | + throw new Error('boom!'); |
| 98 | + }); |
| 99 | + }); |
| 100 | + }, |
| 101 | + |
| 102 | + function netServer() { |
| 103 | + var net = require('net'); |
| 104 | + var d = domain.create(); |
| 105 | + |
| 106 | + d.on('error', function(err) { |
| 107 | + errorHandlerCalled = true; |
| 108 | + }); |
| 109 | + |
| 110 | + d.run(function() { |
| 111 | + var server = net.createServer(function(conn) { |
| 112 | + conn.pipe(conn); |
| 113 | + }); |
| 114 | + server.listen(common.PORT, '0.0.0.0', function() { |
| 115 | + var conn = net.connect(common.PORT, '0.0.0.0'); |
| 116 | + conn.once('data', function() { |
| 117 | + throw new Error('ok'); |
| 118 | + }); |
| 119 | + conn.end('ok'); |
| 120 | + server.close(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + }, |
| 124 | + |
| 125 | + function firstRunOnlyTopLevelErrorHandler() { |
| 126 | + var d = domain.create(); |
| 127 | + var d2 = domain.create(); |
| 128 | + |
| 129 | + d.on('error', function errorHandler() { |
| 130 | + errorHandlerCalled = true; |
| 131 | + }); |
| 132 | + |
| 133 | + d.run(function() { |
| 134 | + d2.run(function() { |
| 135 | + throw new Error('boom!'); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }, |
| 139 | + |
| 140 | + function firstRunNestedWithErrorHandler() { |
| 141 | + var d = domain.create(); |
| 142 | + var d2 = domain.create(); |
| 143 | + |
| 144 | + d2.on('error', function errorHandler() { |
| 145 | + errorHandlerCalled = true; |
| 146 | + }); |
| 147 | + |
| 148 | + d.run(function() { |
| 149 | + d2.run(function() { |
| 150 | + throw new Error('boom!'); |
| 151 | + }); |
| 152 | + }); |
| 153 | + }, |
| 154 | + |
| 155 | + function timeoutNestedWithErrorHandler() { |
| 156 | + var d = domain.create(); |
| 157 | + var d2 = domain.create(); |
| 158 | + |
| 159 | + d2.on('error', function errorHandler() { |
| 160 | + errorHandlerCalled = true; |
| 161 | + }); |
| 162 | + |
| 163 | + d.run(function() { |
| 164 | + d2.run(function() { |
| 165 | + setTimeout(function() { |
| 166 | + console.log('foo'); |
| 167 | + throw new Error('boom!'); |
| 168 | + }, 33); |
| 169 | + }); |
| 170 | + }); |
| 171 | + }, |
| 172 | + |
| 173 | + function setImmediateNestedWithErrorHandler() { |
| 174 | + var d = domain.create(); |
| 175 | + var d2 = domain.create(); |
| 176 | + |
| 177 | + d2.on('error', function errorHandler() { |
| 178 | + errorHandlerCalled = true; |
| 179 | + }); |
| 180 | + |
| 181 | + d.run(function() { |
| 182 | + d2.run(function() { |
| 183 | + setImmediate(function() { |
| 184 | + throw new Error('boom!'); |
| 185 | + }); |
| 186 | + }); |
| 187 | + }); |
| 188 | + }, |
| 189 | + |
| 190 | + function nextTickNestedWithErrorHandler() { |
| 191 | + var d = domain.create(); |
| 192 | + var d2 = domain.create(); |
| 193 | + |
| 194 | + d2.on('error', function errorHandler() { |
| 195 | + errorHandlerCalled = true; |
| 196 | + }); |
| 197 | + |
| 198 | + d.run(function() { |
| 199 | + d2.run(function() { |
| 200 | + process.nextTick(function() { |
| 201 | + throw new Error('boom!'); |
| 202 | + }); |
| 203 | + }); |
| 204 | + }); |
| 205 | + }, |
| 206 | + |
| 207 | + function fsAsyncNestedWithErrorHandler() { |
| 208 | + var d = domain.create(); |
| 209 | + var d2 = domain.create(); |
| 210 | + |
| 211 | + d2.on('error', function errorHandler() { |
| 212 | + errorHandlerCalled = true; |
| 213 | + }); |
| 214 | + |
| 215 | + d.run(function() { |
| 216 | + d2.run(function() { |
| 217 | + var fs = require('fs'); |
| 218 | + fs.exists('/non/existing/file', function onExists(exists) { |
| 219 | + throw new Error('boom!'); |
| 220 | + }); |
| 221 | + }); |
| 222 | + }); |
| 223 | + } |
| 224 | +]; |
| 225 | + |
| 226 | +if (process.argv[2] === 'child') { |
| 227 | + var testIndex = +process.argv[3]; |
| 228 | + |
| 229 | + tests[testIndex](); |
| 230 | + |
| 231 | + process.on('exit', function onExit() { |
| 232 | + assert.equal(errorHandlerCalled, true); |
| 233 | + }); |
| 234 | +} else { |
| 235 | + |
| 236 | + tests.forEach(function(test, testIndex) { |
| 237 | + var testCmd = ''; |
| 238 | + if (process.platform !== 'win32') { |
| 239 | + // Do not create core files, as it can take a lot of disk space on |
| 240 | + // continuous testing and developers' machines |
| 241 | + testCmd += 'ulimit -c 0 && '; |
| 242 | + } |
| 243 | + |
| 244 | + testCmd += process.argv[0]; |
| 245 | + testCmd += ' ' + '--abort-on-uncaught-exception'; |
| 246 | + testCmd += ' ' + process.argv[1]; |
| 247 | + testCmd += ' ' + 'child'; |
| 248 | + testCmd += ' ' + testIndex; |
| 249 | + |
| 250 | + var child = child_process.exec(testCmd); |
| 251 | + |
| 252 | + child.on('exit', function onExit(code, signal) { |
| 253 | + assert.equal(code, 0, 'Test at index ' + testIndex + |
| 254 | + ' should have exited with exit code 0 but instead exited with code ' + |
| 255 | + code + ' and signal ' + signal); |
| 256 | + }); |
| 257 | + |
| 258 | + }); |
| 259 | +} |
0 commit comments