Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting test to use Countdown #17505

Closed
wants to merge 9 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions test/parallel/test-http-should-keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
const common = require('../common');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be the first include within the file. Could you update?

const Countdown = require('../common/countdown');

const SERVER_RESPONSES = [
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n',
Expand All @@ -41,34 +43,32 @@ const SHOULD_KEEP_ALIVE = [
true, // HTTP/1.1, Connection: keep-alive
false // HTTP/1.1, Connection: close
];
let requests = 0;
let responses = 0;
http.globalAgent.maxSockets = 5;

const countdown = new Countdown(SHOULD_KEEP_ALIVE.length , () => server.close());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove the space between length and the comma?

let countdownIndexInc = SERVER_RESPONSES.length - countdown.remaining;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't be able to store this in a variable since it won't update. I suppose you could make it a function if you want to avoid having to repeat it everywhere... const getCountdownIndex = () => SERVER_RESPONSES.length - countdown.remaining and then call it as getCountdownIndex().


const server = net.createServer(function(socket) {
socket.write(SERVER_RESPONSES[requests]);
++requests;
socket.write(SERVER_RESPONSES[countdownIndexInc]);
}).listen(0, function() {
function makeRequest() {
const req = http.get({ port: server.address().port }, function(res) {
assert.strictEqual(
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[responses],
`${SERVER_RESPONSES[responses]} should ${
SHOULD_KEEP_ALIVE[responses] ? '' : 'not '}Keep-Alive`);
++responses;
if (responses < SHOULD_KEEP_ALIVE.length) {
req.shouldKeepAlive, SHOULD_KEEP_ALIVE[countdownIndexInc],
`${SERVER_RESPONSES[countdownIndexInc]} should ${
SHOULD_KEEP_ALIVE[countdownIndexInc] ? '' : 'not '}Keep-Alive`);
countdown.dec();
if (countdown.remaining) {
makeRequest();
} else {
server.close();
}
res.resume();
});
}

makeRequest();
});

process.on('exit', function() {
assert.strictEqual(requests, SERVER_RESPONSES.length);
assert.strictEqual(responses, SHOULD_KEEP_ALIVE.length);
countdownIndexInc = SERVER_RESPONSES.length - countdown.remaining;
assert.strictEqual(countdownIndexInc, SERVER_RESPONSES.length);
assert.strictEqual(countdownIndexInc, SHOULD_KEEP_ALIVE.length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block can be removed altogether. The process won't exit unless the countdown finishes.

});