Skip to content

Commit

Permalink
added check to ensure websocket was still active before closing
Browse files Browse the repository at this point in the history
  • Loading branch information
synapse committed May 9, 2024
1 parent 0752b26 commit 01b1cb6
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ test('Should expose a websocket', async (t) => {
await fastify.listen({ port: 0 })

const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

const chunkPromise = once(ws, 'message')
await once(ws, 'open')
Expand Down Expand Up @@ -94,7 +98,11 @@ test('Should run custom errorHandler on wildcard route handler error', async (t)
await fastify.listen({ port: 0 })

const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

await p
})
Expand Down Expand Up @@ -128,7 +136,11 @@ test('Should run custom errorHandler on error inside websocket handler', async (
await fastify.listen({ port: 0 })
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)

t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

await p
})
Expand Down Expand Up @@ -161,7 +173,11 @@ test('Should run custom errorHandler on error inside async websocket handler', a

await fastify.listen({ port: 0 })
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

await p
})
Expand Down Expand Up @@ -193,7 +209,11 @@ test('Should be able to pass custom options to ws', async (t) => {
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port, clientOptions)
const chunkPromise = once(ws, 'message')
await once(ws, 'open')
t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

ws.send('hello')

Expand Down Expand Up @@ -233,7 +253,11 @@ test('Should warn if path option is provided to ws', async (t) => {
const ws = new WebSocket('ws://localhost:' + fastify.server.address().port, clientOptions)
const chunkPromise = once(ws, 'message')
await once(ws, 'open')
t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

ws.send('hello')

Expand Down Expand Up @@ -274,7 +298,11 @@ test('Should be able to pass a custom server option to ws', async (t) => {
const ws = new WebSocket('ws://localhost:' + externalServerPort)
const chunkPromise = once(ws, 'message')
await once(ws, 'open')
t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

ws.send('hello')

Expand Down Expand Up @@ -335,7 +363,11 @@ test('Should be able to pass preClose option to override default', async (t) =>
await fastify.listen({ port: 0 })

const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
t.teardown(() => ws.close())
t.teardown(() => {
if (ws.readyState) {
ws.close()
}
})

const chunkPromise = once(ws, 'message')
await once(ws, 'open')
Expand Down

0 comments on commit 01b1cb6

Please sign in to comment.