test: fix test-https-agent-additional-options

test-https-agent-additional-options can occasionally fail if a socket
closes before the checks near the end of the test. Modify the test to
check the freeSockets pool after each request.

PR-URL: https://github.com/nodejs/node/pull/27830
Fixes: https://github.com/nodejs/node/issues/24449
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Rich Trott 2019-05-21 16:36:36 -07:00
parent 0fa5c9f256
commit 0d2830041a

View File

@ -39,41 +39,35 @@ const updatedValues = new Map([
['sessionIdContext', 'sessionIdContext'], ['sessionIdContext', 'sessionIdContext'],
]); ]);
let value;
function variations(iter, port, cb) { function variations(iter, port, cb) {
const { done, value } = iter.next(); return common.mustCall((res) => {
if (done) { res.resume();
return common.mustCall((res) => { https.globalAgent.once('free', common.mustCall(() => {
res.resume(); // Verify that the most recent connection is in the freeSockets pool.
https.globalAgent.once('free', common.mustCall(() => { const keys = Object.keys(https.globalAgent.freeSockets);
// Verify that different keep-alived connections are created if (value) {
// for the base call and each variation assert.ok(
const keys = Object.keys(https.globalAgent.freeSockets); keys.some((val) => val.startsWith(value.toString() + ':') ||
assert.strictEqual(keys.length, 1 + updatedValues.size); val.endsWith(':' + value.toString()) ||
let i = 1; val.includes(':' + value.toString() + ':')),
for (const [, value] of updatedValues) { `missing value: ${value.toString()} in ${keys}`
assert.ok( );
keys[i].startsWith(value.toString() + ':') || }
keys[i].endsWith(':' + value.toString()) || const next = iter.next();
keys[i].includes(':' + value.toString() + ':')
); if (next.done) {
i++;
}
https.globalAgent.destroy(); https.globalAgent.destroy();
server.close(); server.close();
})); } else {
}); // Save `value` for check the next time.
} else { value = next.value.val;
const [key, val] = value; const [key, val] = next.value;
return common.mustCall((res) => { https.get(Object.assign({}, getBaseOptions(port), { [key]: val }),
res.resume(); variations(iter, port, cb));
https.globalAgent.once('free', common.mustCall(() => { }
https.get( }));
Object.assign({}, getBaseOptions(port), { [key]: val }), });
variations(iter, port, cb)
);
}));
});
}
} }
server.listen(0, common.mustCall(() => { server.listen(0, common.mustCall(() => {