deno.land / std@0.166.0 / node / net_test.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import * as net from "./net.ts";import { assertEquals } from "../testing/asserts.ts";import { deferred } from "../async/deferred.ts";import * as http from "./http.ts";
Deno.test("[node/net] close event emits after error event", async () => { const socket = net.createConnection(27009, "doesnotexist"); const events: ("error" | "close")[] = []; const errorEmitted = deferred(); const closeEmitted = deferred(); socket.once("error", () => { events.push("error"); errorEmitted.resolve(); }); socket.once("close", () => { events.push("close"); closeEmitted.resolve(); }); await Promise.all([errorEmitted, closeEmitted]);
// `error` happens before `close` assertEquals(events, ["error", "close"]);});
Deno.test("[node/net] the port is available immediately after close callback", async () => { const p = deferred();
// This simulates what get-port@5.1.1 does. const getAvailablePort = (port: number) => new Promise((resolve, reject) => { const server = net.createServer(); server.on("error", reject); server.listen({ port }, () => { // deno-lint-ignore no-explicit-any const { port } = server.address() as any; server.close(() => { resolve(port); }); }); });
const port = await getAvailablePort(5555);
const httpServer = http.createServer(); httpServer.on("error", (e) => p.reject(e)); httpServer.listen(port, () => { httpServer.close(() => p.resolve()); }); await p;});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago