src: fix domains + --abort-on-uncaught-exception
[platform/upstream/nodejs.git] / test / parallel / test-domain-abort-on-uncaught.js
1 var common = require('../common');
2 var assert = require('assert');
3 var spawn = require('child_process').spawn;
4
5 var tests = [
6   nextTick,
7   timer,
8   timerPlusNextTick,
9   firstRun,
10   netServer
11 ]
12
13 tests.forEach(function(test) {
14   console.log(test.name);
15   var child = spawn(process.execPath, [
16     '--abort-on-uncaught-exception',
17     '-e',
18     '(' + test + ')()',
19     common.PORT
20   ]);
21   child.stderr.pipe(process.stderr);
22   child.stdout.pipe(process.stdout);
23   child.on('exit', function(code) {
24     assert.strictEqual(code, 0);
25   });
26 });
27
28 function nextTick() {
29   var domain = require('domain');
30   var d = domain.create();
31
32   d.on('error', function(err) {
33     console.log('ok');
34     process.exit(0);
35   });
36   d.run(function() {
37     process.nextTick(function() {
38       throw new Error('exceptional!');
39     });
40   });
41 }
42
43 function timer() {
44   var domain = require('domain');
45   var d = domain.create();
46
47   d.on('error', function(err) {
48     console.log('ok');
49     process.exit(0);
50   });
51   d.run(function() {
52     setTimeout(function() {
53       throw new Error('exceptional!');
54     }, 33);
55   });
56 }
57
58 function timerPlusNextTick() {
59   var domain = require('domain');
60   var d = domain.create();
61
62   d.on('error', function(err) {
63     console.log('ok');
64     process.exit(0);
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 domain = require('domain');
77   var d = domain.create();
78
79   d.on('error', function(err) {
80     console.log('ok');
81     process.exit(0);
82   });
83   d.run(function() {
84     throw new Error('exceptional!');
85   });
86 }
87
88 function netServer() {
89   var domain = require('domain');
90   var net = require('net');
91   var d = domain.create();
92
93   d.on('error', function(err) {
94     console.log('ok');
95     process.exit(0);
96   });
97   d.run(function() {
98     var server = net.createServer(function(conn) {
99       conn.pipe(conn);
100     });
101     server.listen(Number(process.argv[1]), '0.0.0.0', function() {
102       var conn = net.connect(Number(process.argv[1]), '0.0.0.0')
103       conn.once('data', function() {
104         throw new Error('ok');
105       })
106       conn.end('ok');
107     });
108   });
109 }