Skip to content

Commit

Permalink
fix(common): servers#startOnPreferredPort no graceful fallback #683
Browse files Browse the repository at this point in the history
The code of the method was not waiting for a promise to resolve
which was masking the underlying exception that the code
was supposed to catch and examine in order to determine if
it should perform a fallback of binding to port zero or not.
Fixed it by making sure the initial try of the preferred port
allocation is awaited for so that the exception is thrown
where it is expected by the algorithm.

Fixes #683

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Mar 26, 2021
1 parent c966769 commit 18f5af7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/cactus-common/src/main/typescript/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export class Servers {
): Promise<Server> {
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")) {
Expand Down
30 changes: 30 additions & 0 deletions packages/cactus-common/src/test/typescript/unit/servers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createServer } from "http";
import { AddressInfo } from "net";

import test, { Test } from "tape-promise/tape";

Expand Down Expand Up @@ -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();
});

0 comments on commit 18f5af7

Please sign in to comment.