node/test/parallel/test-http-server-keepalive-req-gc.js
Etienne Pierre-doray 2dea6a4520 test: fix test-http-server-keepalive-req-gc
This changes adds a second explicit gc call in the test. Without this
call, the test relies on gc eventually happening based, since the
first call doesn't free the object.
Relying on gc to eventually happen prevents changing GC heuristics
unrelated to this test.
The gc call is async; otherwise doing multiple sync GCs doesn't free
the object.

PR-URL: https://github.com/nodejs/node/pull/53292
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2024-06-10 09:38:54 +00:00

41 lines
1.1 KiB
JavaScript

// Flags: --expose-gc
'use strict';
const common = require('../common');
const onGC = require('../common/ongc');
const { createServer } = require('http');
const { connect } = require('net');
// Make sure that for HTTP keepalive requests, the req object can be
// garbage collected once the request is finished.
// Refs: https://github.com/nodejs/node/issues/9668
let client;
const server = createServer(common.mustCall((req, res) => {
onGC(req, { ongc: common.mustCall(() => { server.close(); }) });
req.resume();
req.on('end', common.mustCall(() => {
setImmediate(async () => {
client.end();
await global.gc({ type: 'major', execution: 'async' });
await global.gc({ type: 'major', execution: 'async' });
});
}));
res.end('hello world');
}));
server.listen(0, common.mustCall(() => {
client = connect(server.address().port);
const req = [
'POST / HTTP/1.1',
`Host: localhost:${server.address().port}`,
'Connection: keep-alive',
'Content-Length: 11',
'',
'hello world',
'',
].join('\r\n');
client.write(req);
client.unref();
}));