mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
test(net): improve test coverage (#4709)
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
parent
ea70808fee
commit
1cd35a6746
@ -1,7 +1,8 @@
|
|||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
import { getAvailablePort } from "./get_available_port.ts";
|
import { getAvailablePort } from "./get_available_port.ts";
|
||||||
import { assertEquals } from "@std/assert";
|
import { assertEquals, assertNotEquals, assertThrows } from "@std/assert";
|
||||||
|
import { stub } from "@std/testing/mock";
|
||||||
|
|
||||||
/** Helper function to see if a port is indeed available for listening (race-y) */
|
/** Helper function to see if a port is indeed available for listening (race-y) */
|
||||||
async function testWithPort(port: number) {
|
async function testWithPort(port: number) {
|
||||||
@ -20,10 +21,34 @@ async function testWithPort(port: number) {
|
|||||||
|
|
||||||
Deno.test("getAvailablePort() gets an available port", async () => {
|
Deno.test("getAvailablePort() gets an available port", async () => {
|
||||||
const port = getAvailablePort();
|
const port = getAvailablePort();
|
||||||
|
assertEquals(typeof port, "number");
|
||||||
await testWithPort(port);
|
await testWithPort(port);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("getAvailablePort() gets an available port with a preferred port", async () => {
|
Deno.test("getAvailablePort() gets an available port with a preferred port", async () => {
|
||||||
const port = getAvailablePort({ preferredPort: 9563 });
|
const preferredPort = 9563;
|
||||||
|
const port = getAvailablePort({ preferredPort });
|
||||||
|
assertEquals(port, preferredPort);
|
||||||
await testWithPort(port);
|
await testWithPort(port);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("getAvailablePort() falls back to another port if preferred port is in use", async () => {
|
||||||
|
const preferredPort = 9563;
|
||||||
|
const server = Deno.serve(
|
||||||
|
{ port: preferredPort, onListen: () => {} },
|
||||||
|
() => new Response("hello"),
|
||||||
|
);
|
||||||
|
const port = getAvailablePort({ preferredPort });
|
||||||
|
assertEquals(typeof port, "number");
|
||||||
|
assertNotEquals(port, preferredPort);
|
||||||
|
server.shutdown();
|
||||||
|
await server.finished;
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("getAvailablePort() throws if error is not AddrInUse", () => {
|
||||||
|
using _ = stub(Deno, "listen", () => {
|
||||||
|
throw new Error();
|
||||||
|
});
|
||||||
|
const preferredPort = 9563;
|
||||||
|
assertThrows(() => getAvailablePort({ preferredPort }), Error);
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user