Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
fix: close WebSocket while listening test
Browse files Browse the repository at this point in the history
  • Loading branch information
kratico committed Nov 17, 2022
1 parent b2421fc commit 7e9130b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
21 changes: 12 additions & 9 deletions rpc/provider/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deferred, delay } from "../../deps/std/async.ts"
import { deferred } from "../../deps/std/async.ts"
import * as A from "../../deps/std/testing/asserts.ts"
import * as T from "../../test_util/mod.ts"
import { proxyProvider } from "./proxy.ts"
Expand Down Expand Up @@ -49,25 +49,28 @@ Deno.test({

await t.step({
name: "close WebSocket while listening",
ignore: true,
async fn() {
const server = createWebSocketServer()
const stopped = deferred()
const provider = proxyProvider(server.url, (message) => {
A.assertInstanceOf(message, Error)
stopped.resolve()
const server = createWebSocketServer(function() {
this.close()
})
const stopped = deferred()
const provider = proxyProvider(
server.url,
(message) => {
A.assertInstanceOf(message, Error)
stopped.resolve()
},
)
provider.send({
jsonrpc: "2.0",
id: provider.nextId(),
method: "system_health",
params: [],
})
await delay(0)
server.close()
await stopped
const providerRelease = await provider.release()
A.assertNotInstanceOf(providerRelease, Error)
server.close()
},
})

Expand Down
28 changes: 17 additions & 11 deletions rpc/provider/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ function connection(
ws.addEventListener("error", (e) => {
conn!.forEachListener(new ProviderHandlerError(e))
}, controller)
ws.addEventListener("close", (e) => {
conn!.forEachListener(new ProviderHandlerError(e))
}, controller)
return new ProviderConnection(ws, () => controller.abort())
})
conn.addListener(listener)
Expand All @@ -74,18 +77,21 @@ function connection(
function ensureWsOpen(ws: WebSocket): Promise<undefined | Event> {
if (ws.readyState === WebSocket.OPEN) {
return Promise.resolve(undefined)
} else if (ws.readyState === WebSocket.CLOSING || ws.readyState === WebSocket.CLOSED) {
return Promise.resolve(new Event("error"))
} else {
return new Promise<undefined | Event>((resolve) => {
const controller = new AbortController()
ws.addEventListener("open", () => {
controller.abort()
resolve(undefined)
}, controller)
ws.addEventListener("error", (e) => {
controller.abort()
resolve(e)
}, controller)
})
}
return new Promise<undefined | Event>((resolve) => {
const controller = new AbortController()
ws.addEventListener("open", () => {
controller.abort()
resolve(undefined)
}, controller)
ws.addEventListener("error", (e) => {
controller.abort()
resolve(e)
}, controller)
})
}

function closeWs(socket: WebSocket): Promise<undefined | ProviderCloseError<Event>> {
Expand Down

0 comments on commit 7e9130b

Please sign in to comment.