Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): servers#startOnPreferredPort no graceful fallback #683 #700

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});