summaryrefslogtreecommitdiff
path: root/test/parallel/test-beforeexit-event.js
blob: 6bc5ef53ed951871f0ad26f3db05e9fb81abd458 (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
'use strict';
var assert = require('assert');
var net = require('net');
var common = require('../common');
var revivals = 0;
var deaths = 0;

process.on('beforeExit', function() { deaths++; });

process.once('beforeExit', tryImmediate);

function tryImmediate() {
  console.log('set immediate');
  setImmediate(function() {
    revivals++;
    process.once('beforeExit', tryTimer);
  });
}

function tryTimer() {
  console.log('set a timeout');
  setTimeout(function() {
    console.log('timeout cb, do another once beforeExit');
    revivals++;
    process.once('beforeExit', tryListen);
  }, 1);
}

function tryListen() {
  console.log('create a server');
  net.createServer()
    .listen(common.PORT)
    .on('listening', function() {
      revivals++;
      this.close();
    });
}

process.on('exit', function() {
  assert.equal(4, deaths);
  assert.equal(3, revivals);
});