diff --git a/packages/cactus-common/src/main/typescript/servers.ts b/packages/cactus-common/src/main/typescript/servers.ts index 0d721f59c3..21ba40b4f3 100644 --- a/packages/cactus-common/src/main/typescript/servers.ts +++ b/packages/cactus-common/src/main/typescript/servers.ts @@ -86,7 +86,8 @@ export class Servers { ): Promise { if (preferredPort) { try { - return Servers.startOnPort(preferredPort, host); + const server = await Servers.startOnPort(preferredPort, host); + return server; } catch (ex) { // if something else went wrong we still want to just give up if (!ex.message.includes("EADDRINUSE")) { diff --git a/packages/cactus-common/src/test/typescript/unit/servers.test.ts b/packages/cactus-common/src/test/typescript/unit/servers.test.ts index f3917aacdd..693d3d899e 100644 --- a/packages/cactus-common/src/test/typescript/unit/servers.test.ts +++ b/packages/cactus-common/src/test/typescript/unit/servers.test.ts @@ -1,4 +1,5 @@ import { createServer } from "http"; +import { AddressInfo } from "net"; import test, { Test } from "tape-promise/tape"; @@ -37,5 +38,34 @@ test("Servers", async (tParent: Test) => { t.end(); }); + test("Servers#startOnPreferredPort()", async (t: Test) => { + const prefPort = 4123; + const host = "0.0.0.0"; + const portBlocker = createServer(); + test.onFinish(() => portBlocker.close()); + const listenOptionsBlocker = { + server: portBlocker, + hostname: host, + port: prefPort, + }; + await Servers.listen(listenOptionsBlocker); + + await t.doesNotReject(async () => { + const server = await Servers.startOnPreferredPort(prefPort, host); + test.onFinish(() => server.close()); + t.ok(server, "Server returned truthy OK"); + const addressInfo = server.address() as AddressInfo; + t.ok(addressInfo, "AddressInfo returned truthy OK"); + t.ok(addressInfo.port, "AddressInfo.port returned truthy OK"); + t.doesNotEqual( + addressInfo.port, + prefPort, + "Peferred and actually allocated ports are different, therefore fallback is considered successful OK", + ); + }, "Servers.startOnPreferredPort falls back without throwing OK"); + + t.end(); + }); + tParent.end(); });