aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-domain-abort-on-uncaught.js
blob: f4884aa8f33421c9e1c969bd5917a775fadb8a5c (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
'use strict';
// Flags: --abort_on_uncaught_exception

var common = require('../common');
var assert = require('assert');
var domain = require('domain');

var tests = [
  nextTick,
  timer,
  timerPlusNextTick,
  netServer,
  firstRun,
];

var errors = 0;

process.on('exit', function() {
  assert.equal(errors, tests.length);
});

tests.forEach(function(test) { test(); });

function nextTick() {
  var d = domain.create();

  d.once('error', function(err) {
    errors += 1;
  });
  d.run(function() {
    process.nextTick(function() {
      throw new Error('exceptional!');
    });
  });
}

function timer() {
  var d = domain.create();

  d.on('error', function(err) {
    errors += 1;
  });
  d.run(function() {
    setTimeout(function() {
      throw new Error('exceptional!');
    }, 33);
  });
}

function timerPlusNextTick() {
  var d = domain.create();

  d.on('error', function(err) {
    errors += 1;
  });
  d.run(function() {
    setTimeout(function() {
      process.nextTick(function() {
        throw new Error('exceptional!');
      });
    }, 33);
  });
}

function firstRun() {
  var d = domain.create();

  d.on('error', function(err) {
    errors += 1;
  });
  d.run(function() {
    throw new Error('exceptional!');
  });
}

function netServer() {
  var net = require('net');
  var d = domain.create();

  d.on('error', function(err) {
    errors += 1;
  });
  d.run(function() {
    var server = net.createServer(function(conn) {
      conn.pipe(conn);
    });
    server.listen(common.PORT, '0.0.0.0', function() {
      var conn = net.connect(common.PORT, '0.0.0.0');
      conn.once('data', function() {
        throw new Error('ok');
      });
      conn.end('ok');
      server.close();
    });
  });
}