diff options
author | David DeSimone <lord.good.mail@gmail.com> | 2021-02-22 04:26:17 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-22 13:26:17 +0100 |
commit | 8be0c8b43a56ee690463efb3e68686f0fefa868b (patch) | |
tree | d43d064de5c6a92b055b2528be35407318066382 /cli/tests/unit/fetch_test.ts | |
parent | 05911e5d7f7ef4789a6a6d244f271992c7690a87 (diff) |
fix(tests): fix fetchConnectionError test if port is in use (#9465)
Fixes #9379
Diffstat (limited to 'cli/tests/unit/fetch_test.ts')
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 9776baa2f..71307cd69 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -5,6 +5,7 @@ import { assertThrows, assertThrowsAsync, fail, + unimplemented, unitTest, } from "./test_util.ts"; @@ -20,12 +21,37 @@ unitTest({ perms: { net: true } }, async function fetchProtocolError(): Promise< ); }); +function findClosedPortInRange( + minPort: number, + maxPort: number, +): number | never { + let port = minPort; + + // If we hit the return statement of this loop + // that means that we did not throw an + // AddrInUse error when we executed Deno.listen. + while (port < maxPort) { + try { + const listener = Deno.listen({ port }); + listener.close(); + return port; + } catch (e) { + port++; + } + } + + unimplemented( + `No available ports between ${minPort} and ${maxPort} to test fetch`, + ); +} + unitTest( { perms: { net: true } }, async function fetchConnectionError(): Promise<void> { + const port = findClosedPortInRange(4000, 9999); await assertThrowsAsync( async (): Promise<void> => { - await fetch("http://localhost:4000"); + await fetch(`http://localhost:${port}`); }, TypeError, "error trying to connect", |