From 8d240e1d0094b00ee55dbf6c71ab7947afe15f8b Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Tue, 19 Apr 2022 15:36:57 +0200 Subject: [PATCH] test: simplify test-gc-{http-client,net}-* Instead of sending/creating a fixed number of requests/connections, detect when GC has started and stop sending requests/creating connections at that point. Refs: https://github.com/nodejs/node/commit/47ecf2060343 Refs: https://github.com/nodejs/node/commit/7ce8403ef1a6 --- .../test-gc-http-client-connaborted.js | 36 ++++++++------ test/parallel/test-gc-http-client-timeout.js | 47 +++++++++++-------- test/parallel/test-gc-net-timeout.js | 40 +++++++++------- 3 files changed, 73 insertions(+), 50 deletions(-) diff --git a/test/parallel/test-gc-http-client-connaborted.js b/test/parallel/test-gc-http-client-connaborted.js index fa6bf20c176560..b729b2e8b66fca 100644 --- a/test/parallel/test-gc-http-client-connaborted.js +++ b/test/parallel/test-gc-http-client-connaborted.js @@ -5,27 +5,27 @@ const common = require('../common'); const onGC = require('../common/ongc'); - const http = require('http'); -const todo = 500; +const os = require('os'); + +const cpus = os.cpus().length; +let createClients = true; let done = 0; let count = 0; let countGC = 0; -console.log(`We should do ${todo} requests`); - function serverHandler(req, res) { res.connection.destroy(); } const server = http.createServer(serverHandler); server.listen(0, common.mustCall(() => { - for (let i = 0; i < 10; i++) - getall(); + for (let i = 0; i < cpus; i++) + getAll(); })); -function getall() { - if (count >= todo) +function getAll() { + if (!createClients) return; const req = http.get({ @@ -37,7 +37,7 @@ function getall() { count++; onGC(req, { ongc }); - setImmediate(getall); + setImmediate(getAll); } function cb(res) { @@ -48,11 +48,19 @@ function ongc() { countGC++; } -setInterval(status, 100).unref(); +setImmediate(status); function status() { - global.gc(); - console.log('Done: %d/%d', done, todo); - console.log('Collected: %d/%d', countGC, count); - if (countGC === todo) server.close(); + if (done > 0) { + createClients = false; + global.gc(); + console.log(`done/collected/total: ${done}/${countGC}/${count}`); + if (countGC === count) { + server.close(); + } else { + setImmediate(status); + } + } else { + setImmediate(status); + } } diff --git a/test/parallel/test-gc-http-client-timeout.js b/test/parallel/test-gc-http-client-timeout.js index 359f890dc3944e..896431a5e154f3 100644 --- a/test/parallel/test-gc-http-client-timeout.js +++ b/test/parallel/test-gc-http-client-timeout.js @@ -5,28 +5,29 @@ const common = require('../common'); const onGC = require('../common/ongc'); +const http = require('http'); +const os = require('os'); function serverHandler(req, res) { + req.resume(); + res.flushHeaders(); + setTimeout(function() { - req.resume(); - res.writeHead(200); res.end('hello\n'); }, 100); } -const http = require('http'); -const todo = 300; +const cpus = os.cpus().length; +let createClients = true; let done = 0; let count = 0; let countGC = 0; -console.log(`We should do ${todo} requests`); - const server = http.createServer(serverHandler); -server.listen(0, common.mustCall(getall)); +server.listen(0, common.mustCall(getAll)); -function getall() { - if (count >= todo) +function getAll() { + if (!createClients) return; const req = http.get({ @@ -35,18 +36,16 @@ function getall() { port: server.address().port }, cb); - req.setTimeout(10, function() { - console.log('timeout (expected)'); - }); + req.setTimeout(10, common.mustCall()); count++; onGC(req, { ongc }); - setImmediate(getall); + setImmediate(getAll); } -for (let i = 0; i < 10; i++) - getall(); +for (let i = 0; i < cpus; i++) + getAll(); function cb(res) { res.resume(); @@ -57,11 +56,19 @@ function ongc() { countGC++; } -setInterval(status, 100).unref(); +setImmediate(status); function status() { - global.gc(); - console.log('Done: %d/%d', done, todo); - console.log('Collected: %d/%d', countGC, count); - if (countGC === todo) server.close(); + if (done > 0) { + createClients = false; + global.gc(); + console.log(`done/collected/total: ${done}/${countGC}/${count}`); + if (countGC === count) { + server.close(); + } else { + setImmediate(status); + } + } else { + setImmediate(status); + } } diff --git a/test/parallel/test-gc-net-timeout.js b/test/parallel/test-gc-net-timeout.js index 9ba6d2bc1744f9..9ade667ba95c9a 100644 --- a/test/parallel/test-gc-net-timeout.js +++ b/test/parallel/test-gc-net-timeout.js @@ -5,6 +5,9 @@ require('../common'); const onGC = require('../common/ongc'); +const assert = require('assert'); +const net = require('net'); +const os = require('os'); function serverHandler(sock) { sock.setTimeout(120000); @@ -23,20 +26,17 @@ function serverHandler(sock) { }, 100); } -const net = require('net'); -const assert = require('assert'); -const todo = 500; +const cpus = os.cpus().length; +let createClients = true; let done = 0; let count = 0; let countGC = 0; -console.log(`We should do ${todo} requests`); - const server = net.createServer(serverHandler); -server.listen(0, getall); +server.listen(0, getAll); -function getall() { - if (count >= todo) +function getAll() { + if (!createClients) return; const req = net.connect(server.address().port); @@ -49,21 +49,29 @@ function getall() { count++; onGC(req, { ongc }); - setImmediate(getall); + setImmediate(getAll); } -for (let i = 0; i < 10; i++) - getall(); +for (let i = 0; i < cpus; i++) + getAll(); function ongc() { countGC++; } -setInterval(status, 100).unref(); +setImmediate(status); function status() { - global.gc(); - console.log('Done: %d/%d', done, todo); - console.log('Collected: %d/%d', countGC, count); - if (countGC === todo) server.close(); + if (done > 0) { + createClients = false; + global.gc(); + console.log(`done/collected/total: ${done}/${countGC}/${count}`); + if (countGC === count) { + server.close(); + } else { + setImmediate(status); + } + } else { + setImmediate(status); + } }