Skip to content

Commit f596a70

Browse files
committed
Don't hardcode hostname or that leads to flaky test depending on where it runs and skip ipv6 if not supported
1 parent 3da21d4 commit f596a70

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

test/report/test-report-exclude-network.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,35 @@ describe('report exclude network option', () => {
4545
res.writeHead(200, { 'Content-Type': 'text/plain' });
4646
res.end();
4747
});
48+
let ipv6Available = true;
4849
const port = await new Promise((resolve) => server.listen(0, async () => {
4950
await Promise.all([
5051
fetch('http://127.0.0.1:' + server.address().port),
51-
fetch('http://[::1]:' + server.address().port),
52+
fetch('http://[::1]:' + server.address().port).catch(() => ipv6Available = false),
5253
]);
5354
resolve(server.address().port);
5455
server.close();
5556
}));
5657
process.report.excludeNetwork = false;
5758
let report = process.report.getReport();
5859
let tcp = report.libuv.filter((uv) => uv.type === 'tcp' && uv.remoteEndpoint?.port === port);
59-
assert.strictEqual(tcp.length, 2);
60-
const findHandle = (host, local = true, ip4 = true) => {
61-
return tcp.some(
60+
assert.strictEqual(tcp.length, ipv6Available ? 2 : 1);
61+
const findHandle = (local, ip4 = true) => {
62+
return tcp.find(
6263
({ [local ? 'localEndpoint' : 'remoteEndpoint']: ep }) =>
63-
(ep[ip4 ? 'ip4' : 'ip6'] === (ip4 ? '127.0.0.1' : '::1') &&
64-
(Array.isArray(host) ? host.includes(ep.host) : ep.host === host)),
65-
);
64+
(ep[ip4 ? 'ip4' : 'ip6'] === (ip4 ? '127.0.0.1' : '::1')),
65+
)?.[local ? 'localEndpoint' : 'remoteEndpoint'];
6666
};
6767
try {
68-
assert.ok(findHandle('localhost'), 'local localhost handle not found');
69-
assert.ok(findHandle('localhost', false), 'remote localhost handle not found');
68+
// The reverse DNS of 127.0.0.1 can be a lot of things other than localhost
69+
// it could resolve to the server name for instance
70+
assert.notStrictEqual(findHandle(true)?.host, '127.0.0.1');
71+
assert.notStrictEqual(findHandle( false)?.host, '127.0.0.1');
7072

71-
assert.ok(findHandle(['localhost', 'ip6-localhost'], true, false), 'local ip6-localhost handle not found');
72-
assert.ok(findHandle(['localhost', 'ip6-localhost'], false, false), 'remote ip6-localhost handle not found');
73+
if (ipv6Available) {
74+
assert.notStrictEqual(findHandle( true, false)?.host, '::1');
75+
assert.notStrictEqual(findHandle( false, false)?.host, '::1');
76+
}
7377
} catch (e) {
7478
throw new Error(e.message + ' in ' + JSON.stringify(tcp, null, 2));
7579
}
@@ -79,12 +83,14 @@ describe('report exclude network option', () => {
7983
tcp = report.libuv.filter((uv) => uv.type === 'tcp' && uv.remoteEndpoint?.port === port);
8084

8185
try {
82-
assert.strictEqual(tcp.length, 2);
83-
assert.ok(findHandle('127.0.0.1'), 'local 127.0.0.1 handle not found');
84-
assert.ok(findHandle('127.0.0.1', false), 'remote 127.0.0.1 handle not found');
86+
assert.strictEqual(tcp.length, ipv6Available ? 2 : 1);
87+
assert.strictEqual(findHandle(true)?.host, '127.0.0.1');
88+
assert.strictEqual(findHandle(false)?.host, '127.0.0.1');
8589

86-
assert.ok(findHandle('::1', true, false), 'local ::1 handle not found');
87-
assert.ok(findHandle('::1', false, false), 'remote ::1 handle not found');
90+
if (ipv6Available) {
91+
assert.strictEqual(findHandle(true, false)?.host, '::1');
92+
assert.strictEqual(findHandle(false, false)?.host, '::1');
93+
}
8894
} catch (e) {
8995
throw new Error(e.message + ' in ' + JSON.stringify(tcp, null, 2));
9096
}

0 commit comments

Comments
 (0)