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

test: fix flaky test #23811

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
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;
cjihrig marked this conversation as resolved.
Show resolved Hide resolved

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);
Copy link
Member

Choose a reason for hiding this comment

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

Can we use a dummy EventEmitter to emit an event when firstResponse is set instead of polling for it and call makeRemainingRequests when the event is emitted?

}

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