test: fix flaky test-net-connect-econnrefused

Test is flaky in CI with `common.PORT` but not flaky if a port is
determined from createServer() first.

PR-URL: https://github.com/nodejs/node/pull/34330
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Rich Trott
2020-07-12 17:40:50 -07:00
parent 128ed10fc4
commit e44855d317

View File

@@ -31,12 +31,17 @@ const ATTEMPTS_PER_ROUND = 50;
let rounds = 1;
let reqs = 0;
pummel();
let port;
const server = net.createServer().listen(0, common.mustCall(() => {
port = server.address().port;
server.close(common.mustCall(pummel));
}));
function pummel() {
let pending;
for (pending = 0; pending < ATTEMPTS_PER_ROUND; pending++) {
net.createConnection(common.PORT).on('error', function(err) {
net.createConnection(port).on('error', function(err) {
console.log('pending', pending, 'rounds', rounds);
assert.strictEqual(err.code, 'ECONNREFUSED');
if (--pending > 0) return;
if (rounds === ROUNDS) return check();