Skip to content

Commit 12c1ac4

Browse files
lpincaBethGriggs
authored andcommitted
test: simplify test-gc-http-client
Instead of sending a fixed number of requests, detect when GC has started and stop sending requests at that point. PR-URL: #41620 Refs: 47ecf2060343 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 9b1a379 commit 12c1ac4

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

test/sequential/test-gc-http-client.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55
const common = require('../common');
66
const onGC = require('../common/ongc');
77

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

1315
const http = require('http');
14-
const todo = 300;
16+
let createClients = true;
1517
let done = 0;
1618
let count = 0;
1719
let countGC = 0;
1820

19-
console.log(`We should do ${todo} requests`);
20-
2121
const server = http.createServer(serverHandler);
2222
server.listen(0, common.mustCall(() => {
23-
for (let i = 0; i < 15; i++)
24-
getall();
23+
for (let i = 0; i < cpus; i++)
24+
getAll();
2525
}));
2626

27-
function getall() {
28-
if (count === todo)
27+
function getAll() {
28+
if (!createClients)
2929
return;
3030

3131
const req = http.get({
@@ -37,7 +37,7 @@ function getall() {
3737
count++;
3838
onGC(req, { ongc });
3939

40-
setImmediate(getall);
40+
setImmediate(getAll);
4141
}
4242

4343
function cb(res) {
@@ -49,11 +49,19 @@ function ongc() {
4949
countGC++;
5050
}
5151

52-
setInterval(status, 100).unref();
52+
setImmediate(status);
5353

5454
function status() {
55-
global.gc();
56-
console.log('Done: %d/%d', done, todo);
57-
console.log('Collected: %d/%d', countGC, count);
58-
if (countGC === todo) server.close();
55+
if (done > 0) {
56+
createClients = false;
57+
global.gc();
58+
console.log(`done/collected/total: ${done}/${countGC}/${count}`);
59+
if (countGC === count) {
60+
server.close();
61+
} else {
62+
setImmediate(status);
63+
}
64+
} else {
65+
setImmediate(status);
66+
}
5967
}

0 commit comments

Comments
 (0)