Skip to content

Commit

Permalink
test: fix flaky test-https-server-close-idle
Browse files Browse the repository at this point in the history
Don't initiate the second connection until the first has been
established.

Refs: nodejs/reliability#289
  • Loading branch information
santigimeno committed May 26, 2022
1 parent 331088f commit f3f6774
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions test/parallel/test-https-server-close-idle.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,45 @@ server.listen(0, function() {
// Create a first request but never finish it
const client1 = connect({ port, rejectUnauthorized: false });

client1.on('close', common.mustCall(() => {
client1Closed = true;
}));
client1.on('connect', common.mustCall(() => {
assert.strictEqual(connections, 1);
// Create a second request, let it finish but leave the connection opened using HTTP keep-alive
const client2 = connect({ port, rejectUnauthorized: false });
let response = '';

client1.on('error', () => {});
client2.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');

client1.write('GET / HTTP/1.1');

// Create a second request, let it finish but leave the connection opened using HTTP keep-alive
const client2 = connect({ port, rejectUnauthorized: false });
let response = '';
if (response.endsWith('0\r\n\r\n')) {
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));
assert.strictEqual(connections, 2);

client2.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
server.closeIdleConnections();
server.close(common.mustCall());

if (response.endsWith('0\r\n\r\n')) {
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));
assert.strictEqual(connections, 2);
// Check that only the idle connection got closed
setTimeout(common.mustCall(() => {
assert(!client1Closed);
assert(client2Closed);

server.closeIdleConnections();
server.close(common.mustCall());
server.closeAllConnections();
server.close(common.mustCall());
}), common.platformTimeout(500)).unref();
}
}));

// Check that only the idle connection got closed
setTimeout(common.mustCall(() => {
assert(!client1Closed);
assert(client2Closed);
client2.on('close', common.mustCall(() => {
client2Closed = true;
}));

server.closeAllConnections();
server.close(common.mustCall());
}), common.platformTimeout(500)).unref();
}
client2.write('GET / HTTP/1.1\r\n\r\n');
}));

client2.on('close', common.mustCall(() => {
client2Closed = true;
client1.on('close', common.mustCall(() => {
client1Closed = true;
}));

client2.write('GET / HTTP/1.1\r\n\r\n');
client1.on('error', () => {});

client1.write('GET / HTTP/1.1');
});

0 comments on commit f3f6774

Please sign in to comment.