Skip to content

Commit

Permalink
test: refactor test-gc-http-client-timeout
Browse files Browse the repository at this point in the history
Due to server response delay, all possible requests are created anyway.
Instead of doing `36 * os.availableParallelism()` requests, use a fixed
number.

Refs: #48078 (comment)
  • Loading branch information
lpinca committed Jun 2, 2023
1 parent 2fca7ea commit c8d2b12
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions test/sequential/test-gc-http-client-timeout.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';
// Flags: --expose-gc
// just like test-gc-http-client.js,
// but with a timeout set
// Like test-gc-http-client.js, but with a timeout set.

const common = require('../common');
const onGC = require('../common/ongc');
const http = require('http');
const os = require('os');

function serverHandler(req, res) {
setTimeout(function() {
Expand All @@ -16,23 +14,17 @@ function serverHandler(req, res) {
}, 100);
}

const cpus = os.availableParallelism();
const numRequests = 36;
let createClients = true;
const numRequests = 128;
let done = 0;
let count = 0;
let countGC = 0;

const server = http.createServer(serverHandler);
server.listen(0, common.mustCall(() => getAll(numRequests)));

function getAll(requestsRemaining) {
if (!createClients)
return;

if (requestsRemaining <= 0)
return;
server.listen(0, common.mustCall(() => {
for (let i = 0; i < numRequests; i++)
doRequest();
}));

function doRequest(requestsRemaining) {
const req = http.get({
hostname: 'localhost',
pathname: '/',
Expand All @@ -41,15 +33,9 @@ function getAll(requestsRemaining) {

req.setTimeout(10, common.mustCall());

count++;
onGC(req, { ongc });

setImmediate(getAll, requestsRemaining - 1);
}

for (let i = 0; i < cpus; i++)
getAll(numRequests);

function cb(res) {
res.resume();
done += 1;
Expand All @@ -63,10 +49,9 @@ setImmediate(status);

function status() {
if (done > 0) {
createClients = false;
global.gc();
console.log(`done/collected/total: ${done}/${countGC}/${count}`);
if (countGC === count) {
console.log(`done/collected/total: ${done}/${countGC}/${numRequests}`);
if (countGC === numRequests) {
server.close();
return;
}
Expand Down

0 comments on commit c8d2b12

Please sign in to comment.