11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-gc
31cb0ef41Sopenharmony_ci// Like test-gc-http-client.js, but with a timeout set.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst onGC = require('../common/ongc');
71cb0ef41Sopenharmony_ciconst http = require('http');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cifunction serverHandler(req, res) {
101cb0ef41Sopenharmony_ci  setTimeout(function() {
111cb0ef41Sopenharmony_ci    req.resume();
121cb0ef41Sopenharmony_ci    res.writeHead(200);
131cb0ef41Sopenharmony_ci    res.end('hello\n');
141cb0ef41Sopenharmony_ci  }, 100);
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst numRequests = 128;
181cb0ef41Sopenharmony_cilet done = 0;
191cb0ef41Sopenharmony_cilet countGC = 0;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst server = http.createServer(serverHandler);
221cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
231cb0ef41Sopenharmony_ci  getAll(numRequests);
241cb0ef41Sopenharmony_ci}));
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cifunction getAll(requestsRemaining) {
271cb0ef41Sopenharmony_ci  if (requestsRemaining <= 0)
281cb0ef41Sopenharmony_ci    return;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const req = http.get({
311cb0ef41Sopenharmony_ci    hostname: 'localhost',
321cb0ef41Sopenharmony_ci    pathname: '/',
331cb0ef41Sopenharmony_ci    port: server.address().port
341cb0ef41Sopenharmony_ci  }, cb);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  req.setTimeout(10, common.mustCall());
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  onGC(req, { ongc });
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  setImmediate(getAll, requestsRemaining - 1);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cifunction cb(res) {
441cb0ef41Sopenharmony_ci  res.resume();
451cb0ef41Sopenharmony_ci  done += 1;
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cifunction ongc() {
491cb0ef41Sopenharmony_ci  countGC++;
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cisetImmediate(status);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cifunction status() {
551cb0ef41Sopenharmony_ci  if (done > 0) {
561cb0ef41Sopenharmony_ci    global.gc();
571cb0ef41Sopenharmony_ci    console.log(`done/collected/total: ${done}/${countGC}/${numRequests}`);
581cb0ef41Sopenharmony_ci    if (countGC === numRequests) {
591cb0ef41Sopenharmony_ci      server.close();
601cb0ef41Sopenharmony_ci      return;
611cb0ef41Sopenharmony_ci    }
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  setImmediate(status);
651cb0ef41Sopenharmony_ci}
66