node/test/parallel/test-gc-net-timeout.js
tannal 0b6b2a4dc7
test: merge ongc and gcutil into gc.js
PR-URL: https://github.com/nodejs/node/pull/54355
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-08-29 08:49:52 +01:00

77 lines
1.4 KiB
JavaScript

'use strict';
// Flags: --expose-gc
// just like test-gc-http-client-timeout.js,
// but using a net server/client instead
require('../common');
const { onGC } = require('../common/gc');
const assert = require('assert');
const net = require('net');
const os = require('os');
function serverHandler(sock) {
sock.setTimeout(120000);
sock.resume();
sock.on('close', function() {
clearTimeout(timer);
});
sock.on('end', function() {
clearTimeout(timer);
});
sock.on('error', function(err) {
assert.strictEqual(err.code, 'ECONNRESET');
});
const timer = setTimeout(function() {
sock.end('hello\n');
}, 100);
}
const cpus = os.availableParallelism();
let createClients = true;
let done = 0;
let count = 0;
let countGC = 0;
const server = net.createServer(serverHandler);
server.listen(0, getAll);
function getAll() {
if (!createClients)
return;
const req = net.connect(server.address().port);
req.resume();
req.setTimeout(10, function() {
req.destroy();
done++;
});
count++;
onGC(req, { ongc });
setImmediate(getAll);
}
for (let i = 0; i < cpus; i++)
getAll();
function ongc() {
countGC++;
}
setImmediate(status);
function status() {
if (done > 0) {
createClients = false;
global.gc();
console.log(`done/collected/total: ${done}/${countGC}/${count}`);
if (countGC === count) {
server.close();
return;
}
}
setImmediate(status);
}