D
Deno

help

Closing a websocket server in a unit test

Vv1rtl11/7/2022
Hello, I'm an author of rpc lib and I'm currently writing unit tests for it. I am facing a problem of closing a server. How should I do it without deno hanging forever? my test case:
import { send, App } from './mod.ts'

Deno.test('send', async (t) => {
await t.step('should send a message with websocket', async () => {
const app = new App()
await app.listen({ port: 8881, hostname: 'localhost' })

const socket = new WebSocket('ws://localhost:8881')
socket.onopen = () => {
send(socket, { hello: 'world' })
socket.close()
app.close()
}
})
})
import { send, App } from './mod.ts'

Deno.test('send', async (t) => {
await t.step('should send a message with websocket', async () => {
const app = new App()
await app.listen({ port: 8881, hostname: 'localhost' })

const socket = new WebSocket('ws://localhost:8881')
socket.onopen = () => {
send(socket, { hello: 'world' })
socket.close()
app.close()
}
})
})
app.listen here both starts the Deno.listener and Deno.serveHttp
GitHub
GitHub - deno-libs/rpc: 📡 JSONRPC server implementation for Deno
📡 JSONRPC server implementation for Deno. Contribute to deno-libs/rpc development by creating an account on GitHub.

Looking for more? Join the community!