Skip to content

Commit bfcbab4

Browse files
Trotttargos
authored andcommitted
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: #27830 Fixes: #24449 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent a4c1fd5 commit bfcbab4

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

test/parallel/test-https-agent-additional-options.js

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,35 @@ const updatedValues = new Map([
3939
['sessionIdContext', 'sessionIdContext'],
4040
]);
4141

42+
let value;
4243
function variations(iter, port, cb) {
43-
const { done, value } = iter.next();
44-
if (done) {
45-
return common.mustCall((res) => {
46-
res.resume();
47-
https.globalAgent.once('free', common.mustCall(() => {
48-
// Verify that different keep-alived connections are created
49-
// for the base call and each variation
50-
const keys = Object.keys(https.globalAgent.freeSockets);
51-
assert.strictEqual(keys.length, 1 + updatedValues.size);
52-
let i = 1;
53-
for (const [, value] of updatedValues) {
54-
assert.ok(
55-
keys[i].startsWith(value.toString() + ':') ||
56-
keys[i].endsWith(':' + value.toString()) ||
57-
keys[i].includes(':' + value.toString() + ':')
58-
);
59-
i++;
60-
}
44+
return common.mustCall((res) => {
45+
res.resume();
46+
https.globalAgent.once('free', common.mustCall(() => {
47+
// Verify that the most recent connection is in the freeSockets pool.
48+
const keys = Object.keys(https.globalAgent.freeSockets);
49+
if (value) {
50+
assert.ok(
51+
keys.some((val) => val.startsWith(value.toString() + ':') ||
52+
val.endsWith(':' + value.toString()) ||
53+
val.includes(':' + value.toString() + ':')),
54+
`missing value: ${value.toString()} in ${keys}`
55+
);
56+
}
57+
const next = iter.next();
58+
59+
if (next.done) {
6160
https.globalAgent.destroy();
6261
server.close();
63-
}));
64-
});
65-
} else {
66-
const [key, val] = value;
67-
return common.mustCall((res) => {
68-
res.resume();
69-
https.globalAgent.once('free', common.mustCall(() => {
70-
https.get(
71-
Object.assign({}, getBaseOptions(port), { [key]: val }),
72-
variations(iter, port, cb)
73-
);
74-
}));
75-
});
76-
}
62+
} else {
63+
// Save `value` for check the next time.
64+
value = next.value.val;
65+
const [key, val] = next.value;
66+
https.get(Object.assign({}, getBaseOptions(port), { [key]: val }),
67+
variations(iter, port, cb));
68+
}
69+
}));
70+
});
7771
}
7872

7973
server.listen(0, common.mustCall(() => {

0 commit comments

Comments
 (0)