diff options
Diffstat (limited to 'cli/tests/unit/websocket_test.ts')
-rw-r--r-- | cli/tests/unit/websocket_test.ts | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/cli/tests/unit/websocket_test.ts b/cli/tests/unit/websocket_test.ts index 948e2add2..997d8f0df 100644 --- a/cli/tests/unit/websocket_test.ts +++ b/cli/tests/unit/websocket_test.ts @@ -1,5 +1,11 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { assertEquals, assertThrows, deferred, fail } from "./test_util.ts"; +import { + assert, + assertEquals, + assertThrows, + deferred, + fail, +} from "./test_util.ts"; Deno.test({ permissions: "none" }, function websocketPermissionless() { assertThrows( @@ -82,3 +88,46 @@ Deno.test( ws.close(); }, ); + +// https://github.com/denoland/deno/issues/18775 +Deno.test({ + sanitizeOps: false, + sanitizeResources: false, +}, async function websocketDoubleClose() { + const promise = deferred(); + + const ac = new AbortController(); + const listeningPromise = deferred(); + + const server = Deno.serve({ + handler: (req) => { + const { response, socket } = Deno.upgradeWebSocket(req); + let called = false; + socket.onopen = () => socket.send("Hello"); + socket.onmessage = () => { + assert(!called); + called = true; + socket.send("bye"); + socket.close(); + }; + socket.onclose = () => ac.abort(); + socket.onerror = () => fail(); + return response; + }, + signal: ac.signal, + onListen: () => listeningPromise.resolve(), + hostname: "localhost", + port: 4247, + }); + + await listeningPromise; + + const ws = new WebSocket("ws://localhost:4247/"); + assertEquals(ws.url, "ws://localhost:4247/"); + ws.onerror = () => fail(); + ws.onmessage = () => ws.send("bye"); + ws.onclose = () => { + promise.resolve(); + }; + await Promise.all([promise, server]); +}); |