aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-net-socket-timeout-unref.js
blob: bbc2dffcc15336e2c982755720ea954588bf2e00 (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
'use strict';

// Test that unref'ed sockets with timeouts do not prevent exit.

const common = require('../common');
const net = require('net');

const server = net.createServer(function(c) {
  c.write('hello');
  c.unref();
});
server.listen(common.PORT);
server.unref();

var connections = 0;
const sockets = [];
const delays = [8, 5, 3, 6, 2, 4];

delays.forEach(function(T) {
  const socket = net.createConnection(common.PORT, 'localhost');
  socket.on('connect', common.mustCall(function() {
    if (++connections === delays.length) {
      sockets.forEach(function(s) {
        s.socket.setTimeout(s.timeout, function() {
          s.socket.destroy();
          throw new Error('socket timed out unexpectedly');
        });

        s.socket.unref();
      });
    }
  }));

  sockets.push({socket: socket, timeout: T * 1000});
});