test: fix flaky http-server-keep-alive-timeout
Make sure the connection is not closed until the 3 requests and the `timeout` event are received. Some code refactoring to avoid duplicated code. Fixes: https://github.com/nodejs/node/issues/20013 PR-URL: https://github.com/nodejs/node/pull/20093 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
0857790656
commit
998e174ce1
@ -18,15 +18,28 @@ function run() {
|
|||||||
if (fn) fn(run);
|
if (fn) fn(run);
|
||||||
}
|
}
|
||||||
|
|
||||||
test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
|
function done(server, socket, cb) {
|
||||||
const server = http.createServer(common.mustCall((req, res) => {
|
|
||||||
res.end();
|
|
||||||
}, 3));
|
|
||||||
server.setTimeout(500, common.mustCall((socket) => {
|
|
||||||
// End this test and call `run()` for the next test (if any).
|
|
||||||
socket.destroy();
|
socket.destroy();
|
||||||
server.close();
|
server.close(cb);
|
||||||
cb();
|
}
|
||||||
|
|
||||||
|
function serverTest(withPipeline, cb) {
|
||||||
|
let gotAll = false;
|
||||||
|
let timedout = false;
|
||||||
|
const server = http.createServer(common.mustCall((req, res) => {
|
||||||
|
if (withPipeline)
|
||||||
|
res.end();
|
||||||
|
if (req.url === '/3') {
|
||||||
|
gotAll = true;
|
||||||
|
if (timedout)
|
||||||
|
done(server, req.socket, cb);
|
||||||
|
}
|
||||||
|
}, 3));
|
||||||
|
server.setTimeout(500, common.mustCallAtLeast((socket) => {
|
||||||
|
// End this test and call `run()` for the next test (if any).
|
||||||
|
timedout = true;
|
||||||
|
if (gotAll)
|
||||||
|
done(server, socket, cb);
|
||||||
}));
|
}));
|
||||||
server.keepAliveTimeout = 50;
|
server.keepAliveTimeout = 50;
|
||||||
server.listen(0, common.mustCall(() => {
|
server.listen(0, common.mustCall(() => {
|
||||||
@ -40,26 +53,12 @@ test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
|
|||||||
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
|
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
|
||||||
|
serverTest(true, cb);
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
|
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
|
||||||
const server = http.createServer(common.mustCall(3));
|
serverTest(false, cb);
|
||||||
server.setTimeout(500, common.mustCall((socket) => {
|
|
||||||
// End this test and call `run()` for the next test (if any).
|
|
||||||
socket.destroy();
|
|
||||||
server.close();
|
|
||||||
cb();
|
|
||||||
}));
|
|
||||||
server.keepAliveTimeout = 50;
|
|
||||||
server.listen(0, common.mustCall(() => {
|
|
||||||
const options = {
|
|
||||||
port: server.address().port,
|
|
||||||
allowHalfOpen: true
|
|
||||||
};
|
|
||||||
const c = net.connect(options, () => {
|
|
||||||
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
|
|
||||||
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
|
|
||||||
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user