Skip to content

Commit

Permalink
test: deflake gc-http-client tests by restricting number of requests
Browse files Browse the repository at this point in the history
sequential/test-gc-http-client tests were sometimes failing due to
a non-deterministic number of requests being created, causing the
test to fail on some systems with a "ECONNRESET" error cause by
too many concurrent connections

Fixes: nodejs#43638
  • Loading branch information
nicksia-vgw committed Aug 5, 2022
1 parent 9ec2787 commit 4b893c9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
29 changes: 17 additions & 12 deletions test/sequential/test-gc-http-client-onerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function serverHandler(req, res) {
}

const http = require('http');
let callsPerCPU = 36;
let createClients = true;
let done = 0;
let count = 0;
Expand All @@ -23,22 +24,26 @@ let countGC = 0;
const server = http.createServer(serverHandler);
server.listen(0, common.mustCall(() => {
for (let i = 0; i < cpus; i++)
getAll();
getAll(callsPerCPU);
}));

function getAll() {
if (createClients) {
const req = http.get({
hostname: 'localhost',
pathname: '/',
port: server.address().port
}, cb).on('error', onerror);
function getAll(remainingCalls) {
if (!createClients)
return;

count++;
onGC(req, { ongc });
if (remainingCalls <= 0)
return;

setImmediate(getAll);
}
const req = http.get({
hostname: 'localhost',
pathname: '/',
port: server.address().port
}, cb).on('error', onerror);

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

setImmediate(() => getAll(remainingCalls - 1));
}

function cb(res) {
Expand Down
10 changes: 7 additions & 3 deletions test/sequential/test-gc-http-client-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function serverHandler(req, res) {
}

const cpus = os.cpus().length;
let callsPerCPU = 36;
let createClients = true;
let done = 0;
let count = 0;
Expand All @@ -25,10 +26,13 @@ let countGC = 0;
const server = http.createServer(serverHandler);
server.listen(0, common.mustCall(getAll));

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

if (remainingCalls <= 0)
return;

const req = http.get({
hostname: 'localhost',
pathname: '/',
Expand All @@ -40,11 +44,11 @@ function getAll() {
count++;
onGC(req, { ongc });

setImmediate(getAll);
setImmediate(() => getAll(remainingCalls - 1));
}

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

function cb(res) {
res.resume();
Expand Down
10 changes: 7 additions & 3 deletions test/sequential/test-gc-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function serverHandler(req, res) {
}

const http = require('http');
let callsPerCPU = 36;
let createClients = true;
let done = 0;
let count = 0;
Expand All @@ -21,13 +22,16 @@ let countGC = 0;
const server = http.createServer(serverHandler);
server.listen(0, common.mustCall(() => {
for (let i = 0; i < cpus; i++)
getAll();
getAll(callsPerCPU);
}));

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

if (remainingCalls <= 0)
return;

const req = http.get({
hostname: 'localhost',
pathname: '/',
Expand All @@ -37,7 +41,7 @@ function getAll() {
count++;
onGC(req, { ongc });

setImmediate(getAll);
setImmediate(() => getAll(remainingCalls - 1));
}

function cb(res) {
Expand Down

0 comments on commit 4b893c9

Please sign in to comment.