Files
Arvids Godjuks 0fb9e793d7 Stabilize player-list integration tests and RCON fixtures (#1117)
* Wait for player list replication in integration tests

* Select available RCON ports before starting Factorio

* Document zero as automatic RCON port allocation

* Address review wording and test organization

* Restrict port collision hardening to integration fixtures
2026-10-18 22:01:43 +03:00

43 lines
1.1 KiB
JavaScript

import net from "node:net";
// Give test fixtures distinct ports from a dedicated range rather than relying
// on the application's random RCON selection. Probe each candidate before use.
let nextPort = 20000;
export async function getTestRconPort() {
while (nextPort < 30000) {
const port = nextPort;
nextPort += 1;
const probe = net.createServer(socket => socket.destroy());
const listen = host => new Promise((resolve, reject) => {
probe.once("error", reject);
probe.listen({ host, port }, () => {
probe.removeListener("error", reject);
resolve();
});
});
try {
try {
await listen("::");
} catch (err) {
if (err.code !== "EAFNOSUPPORT" && err.code !== "EADDRNOTAVAIL") {
throw err;
}
await listen("0.0.0.0");
}
return port;
} catch (err) {
if (err.code !== "EADDRINUSE") {
throw err;
}
} finally {
if (probe.listening) {
await new Promise((resolve, reject) => {
probe.close(err => (err ? reject(err) : resolve()));
});
}
}
}
throw new Error("No available RCON port in the test fixture range 20000-29999");
}