Skip to content

Commit 392ed93

Browse files
santigimenojasnell
authored andcommitted
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: #20013 PR-URL: #20093 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 22819aa commit 392ed93

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

test/parallel/test-http-server-keep-alive-timeout.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,28 @@ function run() {
1818
if (fn) fn(run);
1919
}
2020

21-
test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
21+
function done(server, socket, cb) {
22+
socket.destroy();
23+
server.close(cb);
24+
}
25+
26+
function serverTest(withPipeline, cb) {
27+
let gotAll = false;
28+
let timedout = false;
2229
const server = http.createServer(common.mustCall((req, res) => {
23-
res.end();
30+
if (withPipeline)
31+
res.end();
32+
if (req.url === '/3') {
33+
gotAll = true;
34+
if (timedout)
35+
done(server, req.socket, cb);
36+
}
2437
}, 3));
25-
server.setTimeout(500, common.mustCall((socket) => {
38+
server.setTimeout(500, common.mustCallAtLeast((socket) => {
2639
// End this test and call `run()` for the next test (if any).
27-
socket.destroy();
28-
server.close();
29-
cb();
40+
timedout = true;
41+
if (gotAll)
42+
done(server, socket, cb);
3043
}));
3144
server.keepAliveTimeout = 50;
3245
server.listen(0, common.mustCall(() => {
@@ -40,26 +53,12 @@ test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
4053
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
4154
});
4255
}));
56+
}
57+
58+
test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
59+
serverTest(true, cb);
4360
});
4461

4562
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
46-
const server = http.createServer(common.mustCall(3));
47-
server.setTimeout(500, common.mustCall((socket) => {
48-
// End this test and call `run()` for the next test (if any).
49-
socket.destroy();
50-
server.close();
51-
cb();
52-
}));
53-
server.keepAliveTimeout = 50;
54-
server.listen(0, common.mustCall(() => {
55-
const options = {
56-
port: server.address().port,
57-
allowHalfOpen: true
58-
};
59-
const c = net.connect(options, () => {
60-
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
61-
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
62-
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
63-
});
64-
}));
63+
serverTest(false, cb);
6564
});

0 commit comments

Comments
 (0)