11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-gc
31cb0ef41Sopenharmony_ci// just like test-gc-http-client-timeout.js,
41cb0ef41Sopenharmony_ci// but using a net server/client instead
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cirequire('../common');
71cb0ef41Sopenharmony_ciconst onGC = require('../common/ongc');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ciconst os = require('os');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cifunction serverHandler(sock) {
131cb0ef41Sopenharmony_ci  sock.setTimeout(120000);
141cb0ef41Sopenharmony_ci  sock.resume();
151cb0ef41Sopenharmony_ci  sock.on('close', function() {
161cb0ef41Sopenharmony_ci    clearTimeout(timer);
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci  sock.on('end', function() {
191cb0ef41Sopenharmony_ci    clearTimeout(timer);
201cb0ef41Sopenharmony_ci  });
211cb0ef41Sopenharmony_ci  sock.on('error', function(err) {
221cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ECONNRESET');
231cb0ef41Sopenharmony_ci  });
241cb0ef41Sopenharmony_ci  const timer = setTimeout(function() {
251cb0ef41Sopenharmony_ci    sock.end('hello\n');
261cb0ef41Sopenharmony_ci  }, 100);
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst cpus = os.availableParallelism();
301cb0ef41Sopenharmony_cilet createClients = true;
311cb0ef41Sopenharmony_cilet done = 0;
321cb0ef41Sopenharmony_cilet count = 0;
331cb0ef41Sopenharmony_cilet countGC = 0;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciconst server = net.createServer(serverHandler);
361cb0ef41Sopenharmony_ciserver.listen(0, getAll);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifunction getAll() {
391cb0ef41Sopenharmony_ci  if (!createClients)
401cb0ef41Sopenharmony_ci    return;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  const req = net.connect(server.address().port);
431cb0ef41Sopenharmony_ci  req.resume();
441cb0ef41Sopenharmony_ci  req.setTimeout(10, function() {
451cb0ef41Sopenharmony_ci    req.destroy();
461cb0ef41Sopenharmony_ci    done++;
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  count++;
501cb0ef41Sopenharmony_ci  onGC(req, { ongc });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  setImmediate(getAll);
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cifor (let i = 0; i < cpus; i++)
561cb0ef41Sopenharmony_ci  getAll();
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cifunction ongc() {
591cb0ef41Sopenharmony_ci  countGC++;
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cisetImmediate(status);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cifunction status() {
651cb0ef41Sopenharmony_ci  if (done > 0) {
661cb0ef41Sopenharmony_ci    createClients = false;
671cb0ef41Sopenharmony_ci    global.gc();
681cb0ef41Sopenharmony_ci    console.log(`done/collected/total: ${done}/${countGC}/${count}`);
691cb0ef41Sopenharmony_ci    if (countGC === count) {
701cb0ef41Sopenharmony_ci      server.close();
711cb0ef41Sopenharmony_ci      return;
721cb0ef41Sopenharmony_ci    }
731cb0ef41Sopenharmony_ci  }
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  setImmediate(status);
761cb0ef41Sopenharmony_ci}
77