Skip to content

Commit

Permalink
test: fix flaky test
Browse files Browse the repository at this point in the history
This commit fixes test-tls-set-secure-context.js. The test was
making one long lasting HTTP connection, followed by a number of
shorter lived connections. However, it was possible that the
connections were not received in the desired order. This commit
ensures that the long lasting connection is established before
making any other connections.

PR-URL: #23811
Fixes: #23807
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
cjihrig authored and targos committed Oct 24, 2018
1 parent 141aec9 commit 83ddd3e
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions test/parallel/test-tls-set-secure-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

// This test verifies the behavior of the tls setSecureContext() method.
// It also verifies that existing connections are not disrupted when the
// secure context is changed.

const assert = require('assert');
const https = require('https');
const fixtures = require('../common/fixtures');
Expand All @@ -19,54 +23,63 @@ const credentialOptions = [
ca: fixtures.readKey('ca2-cert.pem')
}
];
let requestsCount = 0;
let firstResponse;

const server = https.createServer(credentialOptions[0], (req, res) => {
requestsCount++;
const id = +req.headers.id;

if (requestsCount === 1) {
if (id === 1) {
firstResponse = res;
firstResponse.write('multi-');
return;
} else if (requestsCount === 3) {
} else if (id === 4) {
firstResponse.write('success-');
}

res.end('success');
});

server.listen(0, common.mustCall(async () => {
server.listen(0, common.mustCall(() => {
const { port } = server.address();
const firstRequest = makeRequest(port);
const firstRequest = makeRequest(port, 1);

async function makeRemainingRequests() {
// Wait until the first request is guaranteed to have been handled.
if (!firstResponse) {
return setImmediate(makeRemainingRequests);
}

assert.strictEqual(await makeRequest(port), 'success');
assert.strictEqual(await makeRequest(port, 2), 'success');

server.setSecureContext(credentialOptions[1]);
firstResponse.write('request-');
await assert.rejects(async () => {
await makeRequest(port);
}, /^Error: self signed certificate$/);
server.setSecureContext(credentialOptions[1]);
firstResponse.write('request-');
await assert.rejects(async () => {
await makeRequest(port, 3);
}, /^Error: self signed certificate$/);

server.setSecureContext(credentialOptions[0]);
assert.strictEqual(await makeRequest(port), 'success');
server.setSecureContext(credentialOptions[0]);
assert.strictEqual(await makeRequest(port, 4), 'success');

server.setSecureContext(credentialOptions[1]);
firstResponse.end('fun!');
await assert.rejects(async () => {
await makeRequest(port);
}, /^Error: self signed certificate$/);
server.setSecureContext(credentialOptions[1]);
firstResponse.end('fun!');
await assert.rejects(async () => {
await makeRequest(port, 5);
}, /^Error: self signed certificate$/);

assert.strictEqual(await firstRequest, 'multi-request-success-fun!');
server.close();
}

assert.strictEqual(await firstRequest, 'multi-request-success-fun!');
server.close();
makeRemainingRequests();
}));

function makeRequest(port) {
function makeRequest(port, id) {
return new Promise((resolve, reject) => {
const options = {
rejectUnauthorized: true,
ca: credentialOptions[0].ca,
servername: 'agent1'
servername: 'agent1',
headers: { id }
};

https.get(`https://localhost:${port}`, options, (res) => {
Expand Down

0 comments on commit 83ddd3e

Please sign in to comment.