• 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');
8
9function serverHandler(sock) {
10  sock.setTimeout(120000);
11  sock.resume();
12  sock.on('close', function() {
13    clearTimeout(timer);
14  });
15  sock.on('end', function() {
16    clearTimeout(timer);
17  });
18  sock.on('error', function(err) {
19    assert.strictEqual(err.code, 'ECONNRESET');
20  });
21  const timer = setTimeout(function() {
22    sock.end('hello\n');
23  }, 100);
24}
25
26const net = require('net');
27const assert = require('assert');
28const todo = 500;
29let done = 0;
30let count = 0;
31let countGC = 0;
32
33console.log(`We should do ${todo} requests`);
34
35const server = net.createServer(serverHandler);
36server.listen(0, getall);
37
38function getall() {
39  if (count >= todo)
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 < 10; i++)
56  getall();
57
58function ongc() {
59  countGC++;
60}
61
62setInterval(status, 100).unref();
63
64function status() {
65  global.gc();
66  console.log('Done: %d/%d', done, todo);
67  console.log('Collected: %d/%d', countGC, count);
68  if (countGC === todo) server.close();
69}
70