• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Flags: --expose-gc
3// just like test-gc-http-client-timeout.js,
4// but using a net server/client instead
5
6require('../common');
7const onGC = require('../common/ongc');
8const assert = require('assert');
9const net = require('net');
10const os = require('os');
11
12function serverHandler(sock) {
13  sock.setTimeout(120000);
14  sock.resume();
15  sock.on('close', function() {
16    clearTimeout(timer);
17  });
18  sock.on('end', function() {
19    clearTimeout(timer);
20  });
21  sock.on('error', function(err) {
22    assert.strictEqual(err.code, 'ECONNRESET');
23  });
24  const timer = setTimeout(function() {
25    sock.end('hello\n');
26  }, 100);
27}
28
29const cpus = os.availableParallelism();
30let createClients = true;
31let done = 0;
32let count = 0;
33let countGC = 0;
34
35const server = net.createServer(serverHandler);
36server.listen(0, getAll);
37
38function getAll() {
39  if (!createClients)
40    return;
41
42  const req = net.connect(server.address().port);
43  req.resume();
44  req.setTimeout(10, function() {
45    req.destroy();
46    done++;
47  });
48
49  count++;
50  onGC(req, { ongc });
51
52  setImmediate(getAll);
53}
54
55for (let i = 0; i < cpus; i++)
56  getAll();
57
58function ongc() {
59  countGC++;
60}
61
62setImmediate(status);
63
64function status() {
65  if (done > 0) {
66    createClients = false;
67    global.gc();
68    console.log(`done/collected/total: ${done}/${countGC}/${count}`);
69    if (countGC === count) {
70      server.close();
71      return;
72    }
73  }
74
75  setImmediate(status);
76}
77