Skip to content

Commit 6576382

Browse files
tomeromrixMylesBorins
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 70cfe68 commit 6576382

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

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

+11-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require('../common');
2424
const assert = require('assert');
2525
const http = require('http');
2626
const net = require('net');
27+
const Countdown = require('../common/countdown');
2728

2829
const SERVER_RESPONSES = [
2930
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n',
@@ -41,34 +42,27 @@ const SHOULD_KEEP_ALIVE = [
4142
true, // HTTP/1.1, Connection: keep-alive
4243
false // HTTP/1.1, Connection: close
4344
];
44-
let requests = 0;
45-
let responses = 0;
4645
http.globalAgent.maxSockets = 5;
4746

47+
const countdown = new Countdown(SHOULD_KEEP_ALIVE.length, () => server.close());
48+
49+
const getCountdownIndex = () => SERVER_RESPONSES.length - countdown.remaining;
50+
4851
const server = net.createServer(function(socket) {
49-
socket.write(SERVER_RESPONSES[requests]);
50-
++requests;
52+
socket.write(SERVER_RESPONSES[getCountdownIndex()]);
5153
}).listen(0, function() {
5254
function makeRequest() {
5355
const req = http.get({ port: server.address().port }, function(res) {
5456
assert.strictEqual(
55-
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[responses],
56-
`${SERVER_RESPONSES[responses]} should ${
57-
SHOULD_KEEP_ALIVE[responses] ? '' : 'not '}Keep-Alive`);
58-
++responses;
59-
if (responses < SHOULD_KEEP_ALIVE.length) {
57+
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[getCountdownIndex()],
58+
`${SERVER_RESPONSES[getCountdownIndex()]} should ${
59+
SHOULD_KEEP_ALIVE[getCountdownIndex()] ? '' : 'not '}Keep-Alive`);
60+
countdown.dec();
61+
if (countdown.remaining) {
6062
makeRequest();
61-
} else {
62-
server.close();
6363
}
6464
res.resume();
6565
});
6666
}
67-
6867
makeRequest();
6968
});
70-
71-
process.on('exit', function() {
72-
assert.strictEqual(requests, SERVER_RESPONSES.length);
73-
assert.strictEqual(responses, SHOULD_KEEP_ALIVE.length);
74-
});

0 commit comments

Comments
 (0)