summaryrefslogtreecommitdiff
path: root/test/parallel/test-process-getactivehandles.js
blob: 2db3da3c563e6ec23b1b3fcfdca29524c02fa95c (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
'use strict';

require('../common');
const assert = require('assert');
const net = require('net');
const NUM = 8;
const connections = [];
const clients = [];
let clients_counter = 0;

const server = net.createServer(function listener(c) {
  connections.push(c);
}).listen(0, makeConnection);


function makeConnection() {
  if (clients_counter >= NUM) return;
  net.connect(server.address().port, function connected() {
    clientConnected(this);
    makeConnection();
  });
}


function clientConnected(client) {
  clients.push(client);
  if (++clients_counter >= NUM)
    checkAll();
}


function checkAll() {
  const handles = process._getActiveHandles();

  clients.forEach(function(item) {
    assert.ok(handles.includes(item));
    item.destroy();
  });

  connections.forEach(function(item) {
    assert.ok(handles.includes(item));
    item.end();
  });

  assert.ok(handles.includes(server));
  server.close();
}