Skip to content

Commit 47ecf20

Browse files
committed
test: remove magic numbers in test-gc-http-client-onerror
Remove magic numbers (500, 10, 100) from the test. Instead, detect when GC has started and stop sending requests at that point. On my laptop, this results in 16 or 20 requests per run instead of 500. Fixes: #23089 PR-URL: #24943 Reviewed-By: Colin Ihrig <[email protected]>
1 parent 4b96a2a commit 47ecf20

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

test/parallel/test-gc-http-client-onerror.js

+30-23
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,44 @@
66
const common = require('../common');
77
const onGC = require('../common/ongc');
88

9+
const cpus = require('os').cpus().length;
10+
911
function serverHandler(req, res) {
1012
req.resume();
1113
res.writeHead(200, { 'Content-Type': 'text/plain' });
1214
res.end('Hello World\n');
1315
}
1416

1517
const http = require('http');
16-
const todo = 500;
18+
let createClients = true;
1719
let done = 0;
1820
let count = 0;
1921
let countGC = 0;
2022

21-
console.log(`We should do ${todo} requests`);
22-
2323
const server = http.createServer(serverHandler);
2424
server.listen(0, common.mustCall(() => {
25-
for (let i = 0; i < 10; i++)
26-
getall();
25+
for (let i = 0; i < cpus; i++)
26+
getAll();
2727
}));
2828

29-
function getall() {
30-
if (count >= todo)
31-
return;
32-
33-
const req = http.get({
34-
hostname: 'localhost',
35-
pathname: '/',
36-
port: server.address().port
37-
}, cb).on('error', onerror);
29+
function getAll() {
30+
if (createClients) {
31+
const req = http.get({
32+
hostname: 'localhost',
33+
pathname: '/',
34+
port: server.address().port
35+
}, cb).on('error', onerror);
3836

39-
count++;
40-
onGC(req, { ongc });
37+
count++;
38+
onGC(req, { ongc });
4139

42-
setImmediate(getall);
40+
setImmediate(getAll);
41+
}
4342
}
4443

4544
function cb(res) {
4645
res.resume();
47-
done += 1;
46+
done++;
4847
}
4948

5049
function onerror(err) {
@@ -55,11 +54,19 @@ function ongc() {
5554
countGC++;
5655
}
5756

58-
setInterval(status, 100).unref();
57+
setImmediate(status);
5958

6059
function status() {
61-
global.gc();
62-
console.log('Done: %d/%d', done, todo);
63-
console.log('Collected: %d/%d', countGC, count);
64-
if (countGC === todo) server.close();
60+
if (done > 0) {
61+
createClients = false;
62+
global.gc();
63+
console.log(`done/collected/total: ${done}/${countGC}/${count}`);
64+
if (countGC === count) {
65+
server.close();
66+
} else {
67+
setImmediate(status);
68+
}
69+
} else {
70+
setImmediate(status);
71+
}
6572
}

0 commit comments

Comments
 (0)