node/test/parallel/test-http-keep-alive-timeout-race-condition.js
jazelly 05ad947899 http: reduce likelihood of race conditions on keep-alive timeout
Fixes: https://github.com/nodejs/node/issues/52649
Refs: https://github.com/nodejs/node/issues/54293

Co-authored-by: Arrigo Zanette <zanettea@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/54863
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
2024-09-13 08:45:00 -07:00

39 lines
1022 B
JavaScript

'use strict';
const common = require('../common');
const http = require('http');
const makeRequest = (port, agent) =>
new Promise((resolve, reject) => {
const req = http.get(
{ path: '/', port, agent },
(res) => {
res.resume();
res.on('end', () => resolve());
},
);
req.on('error', (e) => reject(e));
req.end();
});
const server = http.createServer(
{ keepAliveTimeout: common.platformTimeout(2000), keepAlive: true },
common.mustCall((req, res) => {
const body = 'hello world\n';
res.writeHead(200, { 'Content-Length': body.length });
res.write(body);
res.end();
}, 2)
);
const agent = new http.Agent({ maxSockets: 5, keepAlive: true });
server.listen(0, common.mustCall(async function() {
await makeRequest(this.address().port, agent);
// Block the event loop for 2 seconds
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 2000);
await makeRequest(this.address().port, agent);
server.close();
agent.destroy();
}));