test: update test-http-should-keep-alive to use countdown
PR-URL: https://github.com/nodejs/node/pull/17505 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Jon Moss <me@jonathanmoss.me>
This commit is contained in:
parent
ea77b33a52
commit
e9d1e1265a
@ -24,6 +24,7 @@ require('../common');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
|
const Countdown = require('../common/countdown');
|
||||||
|
|
||||||
const SERVER_RESPONSES = [
|
const SERVER_RESPONSES = [
|
||||||
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n',
|
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n',
|
||||||
@ -41,34 +42,27 @@ const SHOULD_KEEP_ALIVE = [
|
|||||||
true, // HTTP/1.1, Connection: keep-alive
|
true, // HTTP/1.1, Connection: keep-alive
|
||||||
false // HTTP/1.1, Connection: close
|
false // HTTP/1.1, Connection: close
|
||||||
];
|
];
|
||||||
let requests = 0;
|
|
||||||
let responses = 0;
|
|
||||||
http.globalAgent.maxSockets = 5;
|
http.globalAgent.maxSockets = 5;
|
||||||
|
|
||||||
|
const countdown = new Countdown(SHOULD_KEEP_ALIVE.length, () => server.close());
|
||||||
|
|
||||||
|
const getCountdownIndex = () => SERVER_RESPONSES.length - countdown.remaining;
|
||||||
|
|
||||||
const server = net.createServer(function(socket) {
|
const server = net.createServer(function(socket) {
|
||||||
socket.write(SERVER_RESPONSES[requests]);
|
socket.write(SERVER_RESPONSES[getCountdownIndex()]);
|
||||||
++requests;
|
|
||||||
}).listen(0, function() {
|
}).listen(0, function() {
|
||||||
function makeRequest() {
|
function makeRequest() {
|
||||||
const req = http.get({ port: server.address().port }, function(res) {
|
const req = http.get({ port: server.address().port }, function(res) {
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[responses],
|
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[getCountdownIndex()],
|
||||||
`${SERVER_RESPONSES[responses]} should ${
|
`${SERVER_RESPONSES[getCountdownIndex()]} should ${
|
||||||
SHOULD_KEEP_ALIVE[responses] ? '' : 'not '}Keep-Alive`);
|
SHOULD_KEEP_ALIVE[getCountdownIndex()] ? '' : 'not '}Keep-Alive`);
|
||||||
++responses;
|
countdown.dec();
|
||||||
if (responses < SHOULD_KEEP_ALIVE.length) {
|
if (countdown.remaining) {
|
||||||
makeRequest();
|
makeRequest();
|
||||||
} else {
|
|
||||||
server.close();
|
|
||||||
}
|
}
|
||||||
res.resume();
|
res.resume();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
makeRequest();
|
makeRequest();
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('exit', function() {
|
|
||||||
assert.strictEqual(requests, SERVER_RESPONSES.length);
|
|
||||||
assert.strictEqual(responses, SHOULD_KEEP_ALIVE.length);
|
|
||||||
});
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user