11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-gc
31cb0ef41Sopenharmony_ci// just a simple http server and client.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst onGC = require('../common/ongc');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst cpus = require('os').availableParallelism();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifunction serverHandler(req, res) {
111cb0ef41Sopenharmony_ci  res.writeHead(200, { 'Content-Type': 'text/plain' });
121cb0ef41Sopenharmony_ci  res.end('Hello World\n');
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst http = require('http');
161cb0ef41Sopenharmony_ciconst numRequests = 36;
171cb0ef41Sopenharmony_cilet createClients = true;
181cb0ef41Sopenharmony_cilet done = 0;
191cb0ef41Sopenharmony_cilet count = 0;
201cb0ef41Sopenharmony_cilet countGC = 0;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst server = http.createServer(serverHandler);
231cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
241cb0ef41Sopenharmony_ci  for (let i = 0; i < cpus; i++)
251cb0ef41Sopenharmony_ci    getAll(numRequests);
261cb0ef41Sopenharmony_ci}));
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cifunction getAll(requestsRemaining) {
291cb0ef41Sopenharmony_ci  if (!createClients)
301cb0ef41Sopenharmony_ci    return;
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  if (requestsRemaining <= 0)
331cb0ef41Sopenharmony_ci    return;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const req = http.get({
361cb0ef41Sopenharmony_ci    hostname: 'localhost',
371cb0ef41Sopenharmony_ci    pathname: '/',
381cb0ef41Sopenharmony_ci    port: server.address().port
391cb0ef41Sopenharmony_ci  }, cb);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  count++;
421cb0ef41Sopenharmony_ci  onGC(req, { ongc });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  setImmediate(getAll, requestsRemaining - 1);
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cifunction cb(res) {
481cb0ef41Sopenharmony_ci  res.resume();
491cb0ef41Sopenharmony_ci  done += 1;
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cifunction ongc() {
531cb0ef41Sopenharmony_ci  countGC++;
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cisetImmediate(status);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cifunction status() {
591cb0ef41Sopenharmony_ci  if (done > 0) {
601cb0ef41Sopenharmony_ci    createClients = false;
611cb0ef41Sopenharmony_ci    global.gc();
621cb0ef41Sopenharmony_ci    console.log(`done/collected/total: ${done}/${countGC}/${count}`);
631cb0ef41Sopenharmony_ci    if (countGC === count) {
641cb0ef41Sopenharmony_ci      server.close();
651cb0ef41Sopenharmony_ci    } else {
661cb0ef41Sopenharmony_ci      setImmediate(status);
671cb0ef41Sopenharmony_ci    }
681cb0ef41Sopenharmony_ci  } else {
691cb0ef41Sopenharmony_ci    setImmediate(status);
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci}
72