11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-gc
31cb0ef41Sopenharmony_ci// just like test-gc-http-client.js,
41cb0ef41Sopenharmony_ci// but with an on('error') handler that does nothing.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciconst onGC = require('../common/ongc');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst cpus = require('os').availableParallelism();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction serverHandler(req, res) {
121cb0ef41Sopenharmony_ci  req.resume();
131cb0ef41Sopenharmony_ci  res.writeHead(200, { 'Content-Type': 'text/plain' });
141cb0ef41Sopenharmony_ci  res.end('Hello World\n');
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst http = require('http');
181cb0ef41Sopenharmony_ciconst numRequests = 36;
191cb0ef41Sopenharmony_cilet createClients = true;
201cb0ef41Sopenharmony_cilet done = 0;
211cb0ef41Sopenharmony_cilet count = 0;
221cb0ef41Sopenharmony_cilet countGC = 0;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst server = http.createServer(serverHandler);
251cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
261cb0ef41Sopenharmony_ci  for (let i = 0; i < cpus; i++)
271cb0ef41Sopenharmony_ci    getAll(numRequests);
281cb0ef41Sopenharmony_ci}));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction getAll(requestsRemaining) {
311cb0ef41Sopenharmony_ci  if (!createClients)
321cb0ef41Sopenharmony_ci    return;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  if (requestsRemaining <= 0)
351cb0ef41Sopenharmony_ci    return;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  const req = http.get({
381cb0ef41Sopenharmony_ci    hostname: 'localhost',
391cb0ef41Sopenharmony_ci    pathname: '/',
401cb0ef41Sopenharmony_ci    port: server.address().port
411cb0ef41Sopenharmony_ci  }, cb).on('error', onerror);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  count++;
441cb0ef41Sopenharmony_ci  onGC(req, { ongc });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  setImmediate(getAll, requestsRemaining - 1);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cifunction cb(res) {
501cb0ef41Sopenharmony_ci  res.resume();
511cb0ef41Sopenharmony_ci  done++;
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cifunction onerror(err) {
551cb0ef41Sopenharmony_ci  throw err;
561cb0ef41Sopenharmony_ci}
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    } else {
721cb0ef41Sopenharmony_ci      setImmediate(status);
731cb0ef41Sopenharmony_ci    }
741cb0ef41Sopenharmony_ci  } else {
751cb0ef41Sopenharmony_ci    setImmediate(status);
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci}
78