mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
fix(tests): fix fetchConnectionError test if port is in use (#9465)
Fixes #9379
This commit is contained in:
parent
05911e5d7f
commit
8be0c8b43a
2 changed files with 28 additions and 1 deletions
|
@ -5,6 +5,7 @@ import {
|
||||||
assertThrows,
|
assertThrows,
|
||||||
assertThrowsAsync,
|
assertThrowsAsync,
|
||||||
fail,
|
fail,
|
||||||
|
unimplemented,
|
||||||
unitTest,
|
unitTest,
|
||||||
} from "./test_util.ts";
|
} 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(
|
unitTest(
|
||||||
{ perms: { net: true } },
|
{ perms: { net: true } },
|
||||||
async function fetchConnectionError(): Promise<void> {
|
async function fetchConnectionError(): Promise<void> {
|
||||||
|
const port = findClosedPortInRange(4000, 9999);
|
||||||
await assertThrowsAsync(
|
await assertThrowsAsync(
|
||||||
async (): Promise<void> => {
|
async (): Promise<void> => {
|
||||||
await fetch("http://localhost:4000");
|
await fetch(`http://localhost:${port}`);
|
||||||
},
|
},
|
||||||
TypeError,
|
TypeError,
|
||||||
"error trying to connect",
|
"error trying to connect",
|
||||||
|
|
|
@ -17,6 +17,7 @@ export {
|
||||||
assertThrows,
|
assertThrows,
|
||||||
assertThrowsAsync,
|
assertThrowsAsync,
|
||||||
fail,
|
fail,
|
||||||
|
unimplemented,
|
||||||
unreachable,
|
unreachable,
|
||||||
} from "../../../test_util/std/testing/asserts.ts";
|
} from "../../../test_util/std/testing/asserts.ts";
|
||||||
export { deferred } from "../../../test_util/std/async/deferred.ts";
|
export { deferred } from "../../../test_util/std/async/deferred.ts";
|
||||||
|
|
Loading…
Reference in a new issue