1'use strict';
2// Flags: --expose-gc
3// just like test-gc-http-client.js,
4// but aborting every connection that comes in.
5
6const common = require('../common');
7const onGC = require('../common/ongc');
8const http = require('http');
9const os = require('os');
10
11const cpus = os.availableParallelism();
12let createClients = true;
13let done = 0;
14let count = 0;
15let countGC = 0;
16
17function serverHandler(req, res) {
18  res.connection.destroy();
19}
20
21const server = http.createServer(serverHandler);
22server.listen(0, common.mustCall(() => {
23  for (let i = 0; i < cpus; i++)
24    getAll();
25}));
26
27function getAll() {
28  if (!createClients)
29    return;
30
31  const req = http.get({
32    hostname: 'localhost',
33    pathname: '/',
34    port: server.address().port
35  }, cb).on('error', cb);
36
37  count++;
38  onGC(req, { ongc });
39
40  setImmediate(getAll);
41}
42
43function cb(res) {
44  done += 1;
45}
46
47function ongc() {
48  countGC++;
49}
50
51setImmediate(status);
52
53function status() {
54  if (done > 0) {
55    createClients = false;
56    global.gc();
57    console.log(`done/collected/total: ${done}/${countGC}/${count}`);
58    if (countGC === count) {
59      server.close();
60      return;
61    }
62  }
63
64  setImmediate(status);
65}
66