Skip to content

Commit bd4b97f

Browse files
tomeromrixgibfahn
authored andcommitted
test: update test-http-should-keep-alive to use countdown
PR-URL: #17505 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Jon Moss <[email protected]>
1 parent f53b4df commit bd4b97f

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

test/parallel/test-http-should-keep-alive.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require('../common');
33
const assert = require('assert');
44
const http = require('http');
55
const net = require('net');
6+
const Countdown = require('../common/countdown');
67

78
const SERVER_RESPONSES = [
89
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n',
@@ -20,34 +21,27 @@ const SHOULD_KEEP_ALIVE = [
2021
true, // HTTP/1.1, Connection: keep-alive
2122
false // HTTP/1.1, Connection: close
2223
];
23-
let requests = 0;
24-
let responses = 0;
2524
http.globalAgent.maxSockets = 5;
2625

26+
const countdown = new Countdown(SHOULD_KEEP_ALIVE.length, () => server.close());
27+
28+
const getCountdownIndex = () => SERVER_RESPONSES.length - countdown.remaining;
29+
2730
const server = net.createServer(function(socket) {
28-
socket.write(SERVER_RESPONSES[requests]);
29-
++requests;
31+
socket.write(SERVER_RESPONSES[getCountdownIndex()]);
3032
}).listen(0, function() {
3133
function makeRequest() {
3234
const req = http.get({port: server.address().port}, function(res) {
3335
assert.strictEqual(
34-
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[responses],
35-
`${SERVER_RESPONSES[responses]} should ${
36-
SHOULD_KEEP_ALIVE[responses] ? '' : 'not '}Keep-Alive`);
37-
++responses;
38-
if (responses < SHOULD_KEEP_ALIVE.length) {
36+
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[getCountdownIndex()],
37+
`${SERVER_RESPONSES[getCountdownIndex()]} should ${
38+
SHOULD_KEEP_ALIVE[getCountdownIndex()] ? '' : 'not '}Keep-Alive`);
39+
countdown.dec();
40+
if (countdown.remaining) {
3941
makeRequest();
40-
} else {
41-
server.close();
4242
}
4343
res.resume();
4444
});
4545
}
46-
4746
makeRequest();
4847
});
49-
50-
process.on('exit', function() {
51-
assert.strictEqual(requests, SERVER_RESPONSES.length);
52-
assert.strictEqual(responses, SHOULD_KEEP_ALIVE.length);
53-
});

0 commit comments

Comments
 (0)