From 4b893c94e20ee3c2d8e4c3f46748f5bc2d5e76a1 Mon Sep 17 00:00:00 2001 From: Nick Sia Date: Fri, 5 Aug 2022 18:29:30 +0800 Subject: [PATCH] test: deflake gc-http-client tests by restricting number of requests 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: https://github.com/nodejs/node/issues/43638 --- .../sequential/test-gc-http-client-onerror.js | 29 +++++++++++-------- .../sequential/test-gc-http-client-timeout.js | 10 +++++-- test/sequential/test-gc-http-client.js | 10 +++++-- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/test/sequential/test-gc-http-client-onerror.js b/test/sequential/test-gc-http-client-onerror.js index 30b272ed94aae9..8222d73eaba4e1 100644 --- a/test/sequential/test-gc-http-client-onerror.js +++ b/test/sequential/test-gc-http-client-onerror.js @@ -15,6 +15,7 @@ function serverHandler(req, res) { } const http = require('http'); +let callsPerCPU = 36; let createClients = true; let done = 0; let count = 0; @@ -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) { diff --git a/test/sequential/test-gc-http-client-timeout.js b/test/sequential/test-gc-http-client-timeout.js index 3ac410a6111bd8..5768ccf555ea12 100644 --- a/test/sequential/test-gc-http-client-timeout.js +++ b/test/sequential/test-gc-http-client-timeout.js @@ -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; @@ -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: '/', @@ -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(); diff --git a/test/sequential/test-gc-http-client.js b/test/sequential/test-gc-http-client.js index 7b9f9865ea9945..cc4b87690f47e4 100644 --- a/test/sequential/test-gc-http-client.js +++ b/test/sequential/test-gc-http-client.js @@ -13,6 +13,7 @@ function serverHandler(req, res) { } const http = require('http'); +let callsPerCPU = 36; let createClients = true; let done = 0; let count = 0; @@ -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: '/', @@ -37,7 +41,7 @@ function getAll() { count++; onGC(req, { ongc }); - setImmediate(getAll); + setImmediate(() => getAll(remainingCalls - 1)); } function cb(res) {