Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(common): servers#startOnPreferredPort no graceful fallback hyperl…
Browse files Browse the repository at this point in the history
…edger-cacti#683

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 hyperledger-cacti#683

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
petermetz authored and Jordi Giron committed Apr 8, 2021
1 parent 03849d1 commit ebccc40
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
@@ -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")) {
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";

@@ -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 ebccc40

Please sign in to comment.