2024-09-04 09:25:06 +02:00
|
|
|
import { assertEquals } from '@std/assert'
|
2024-09-12 05:05:41 +02:00
|
|
|
import { stream, streamSSE } from '../../src/helper/streaming/index.ts'
|
|
|
|
import { Hono } from '../../src/hono.ts'
|
2024-06-29 00:00:28 +02:00
|
|
|
|
2024-10-30 01:44:06 +01:00
|
|
|
Deno.test('Should call onAbort via stream', async () => {
|
2024-06-29 00:00:28 +02:00
|
|
|
const app = new Hono()
|
|
|
|
let aborted = false
|
|
|
|
app.get('/stream', (c) => {
|
2024-09-04 09:25:06 +02:00
|
|
|
return stream(c, (stream) => {
|
2024-06-29 00:00:28 +02:00
|
|
|
stream.onAbort(() => {
|
|
|
|
aborted = true
|
|
|
|
})
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
stream.onAbort(resolve)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const server = Deno.serve({ port: 0 }, app.fetch)
|
|
|
|
const ac = new AbortController()
|
|
|
|
const req = new Request(`http://localhost:${server.addr.port}/stream`, {
|
|
|
|
signal: ac.signal,
|
|
|
|
})
|
|
|
|
const res = fetch(req).catch(() => {})
|
|
|
|
assertEquals(aborted, false)
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
|
|
ac.abort()
|
|
|
|
await res
|
|
|
|
while (!aborted) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve))
|
|
|
|
}
|
|
|
|
assertEquals(aborted, true)
|
|
|
|
|
|
|
|
await server.shutdown()
|
|
|
|
})
|
|
|
|
|
2024-10-30 01:44:06 +01:00
|
|
|
Deno.test('Should not call onAbort via stream if already closed', async () => {
|
2024-07-03 03:50:44 +02:00
|
|
|
const app = new Hono()
|
|
|
|
let aborted = false
|
|
|
|
app.get('/stream', (c) => {
|
|
|
|
return stream(c, async (stream) => {
|
|
|
|
stream.onAbort(() => {
|
|
|
|
aborted = true
|
|
|
|
})
|
|
|
|
await stream.write('Hello')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const server = Deno.serve({ port: 0 }, app.fetch)
|
|
|
|
assertEquals(aborted, false)
|
|
|
|
const res = await fetch(`http://localhost:${server.addr.port}/stream`)
|
|
|
|
assertEquals(await res.text(), 'Hello')
|
|
|
|
assertEquals(aborted, false)
|
|
|
|
await server.shutdown()
|
|
|
|
})
|
|
|
|
|
2024-10-30 01:44:06 +01:00
|
|
|
Deno.test('Should call onAbort via streamSSE', async () => {
|
2024-06-29 00:00:28 +02:00
|
|
|
const app = new Hono()
|
|
|
|
let aborted = false
|
|
|
|
app.get('/stream', (c) => {
|
2024-09-04 09:25:06 +02:00
|
|
|
return streamSSE(c, (stream) => {
|
2024-06-29 00:00:28 +02:00
|
|
|
stream.onAbort(() => {
|
|
|
|
aborted = true
|
|
|
|
})
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
stream.onAbort(resolve)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const server = Deno.serve({ port: 0 }, app.fetch)
|
|
|
|
const ac = new AbortController()
|
|
|
|
const req = new Request(`http://localhost:${server.addr.port}/stream`, {
|
|
|
|
signal: ac.signal,
|
|
|
|
})
|
|
|
|
assertEquals
|
|
|
|
const res = fetch(req).catch(() => {})
|
|
|
|
assertEquals(aborted, false)
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
|
|
ac.abort()
|
|
|
|
await res
|
|
|
|
while (!aborted) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve))
|
|
|
|
}
|
|
|
|
assertEquals(aborted, true)
|
|
|
|
|
|
|
|
await server.shutdown()
|
|
|
|
})
|
2024-07-03 03:50:44 +02:00
|
|
|
|
2024-10-30 01:44:06 +01:00
|
|
|
Deno.test('Should not call onAbort via streamSSE if already closed', async () => {
|
2024-07-03 03:50:44 +02:00
|
|
|
const app = new Hono()
|
|
|
|
let aborted = false
|
|
|
|
app.get('/stream', (c) => {
|
|
|
|
return streamSSE(c, async (stream) => {
|
|
|
|
stream.onAbort(() => {
|
|
|
|
aborted = true
|
|
|
|
})
|
|
|
|
await stream.write('Hello')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const server = Deno.serve({ port: 0 }, app.fetch)
|
|
|
|
assertEquals(aborted, false)
|
|
|
|
const res = await fetch(`http://localhost:${server.addr.port}/stream`)
|
|
|
|
assertEquals(await res.text(), 'Hello')
|
|
|
|
assertEquals(aborted, false)
|
|
|
|
await server.shutdown()
|
|
|
|
})
|