stream.test.ts
This file
Path
src/utils/stream.test.ts
212 lines 路 7.2 KiB 路 TypeScript
Last changed by
fix(utils/stream): do not let abort listeners crash abort() (#5274)Tushar Pagarbefore adoption5e5b83d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213import { StreamingApi } from './stream'
describe('StreamingApi', () => { it('write(string)', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const reader = api.responseReadable.getReader() api.write('foo') expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo')) api.write('bar') expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar')) })
it('write(Uint8Array)', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const reader = api.responseReadable.getReader() api.write(new Uint8Array([1, 2, 3])) expect((await reader.read()).value).toEqual(new Uint8Array([1, 2, 3])) api.write(new Uint8Array([4, 5, 6])) expect((await reader.read()).value).toEqual(new Uint8Array([4, 5, 6])) })
it('writeln(string)', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const reader = api.responseReadable.getReader() api.writeln('foo') expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo\n')) api.writeln('bar') expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar\n')) })
it('pipe()', async () => { const { readable: senderReadable, writable: senderWritable } = new TransformStream()
// send data to readable in other scope ;(async () => { const writer = senderWritable.getWriter() await writer.write(new TextEncoder().encode('foo')) await writer.write(new TextEncoder().encode('bar')) // await writer.close() })()
const { readable: receiverReadable, writable: receiverWritable } = new TransformStream()
const api = new StreamingApi(receiverWritable, receiverReadable)
// pipe readable to api in other scope ;(async () => { await api.pipe(senderReadable) })()
// read data from api const reader = api.responseReadable.getReader() expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo')) expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar')) })
it('pipe() re-acquires writer lock when pipeTo() throws', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const reader = api.responseReadable.getReader()
const erroringReadable = new ReadableStream({ start(controller) { controller.error(new Error('upstream error')) }, })
await expect(api.pipe(erroringReadable)).rejects.toThrow('upstream error') api.write('after pipe') expect((await reader.read()).value).toEqual(new TextEncoder().encode('after pipe')) })
it('pipe() can be called again after a previous pipe() throws', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const reader = api.responseReadable.getReader()
const erroringReadable = new ReadableStream({ start(controller) { controller.error(new Error('upstream error')) }, }) await expect(api.pipe(erroringReadable)).rejects.toThrow('upstream error')
const src = new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode('second pipe')) controller.close() }, }) await api.pipe(src) expect((await reader.read()).value).toEqual(new TextEncoder().encode('second pipe')) })
it('close()', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const reader = api.responseReadable.getReader() await api.close() expect((await reader.read()).done).toBe(true) })
it('should not throw an error in write()', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) await api.close() const write = () => api.write('foo') expect(write).not.toThrow() })
it('should not throw an error in close()', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const close = async () => { await api.close() await api.close() } expect(close).not.toThrow() })
it('onAbort()', async () => { const { readable, writable } = new TransformStream() const handleAbort1 = vi.fn() const handleAbort2 = vi.fn() const api = new StreamingApi(writable, readable) api.onAbort(handleAbort1) api.onAbort(handleAbort2) expect(handleAbort1).not.toBeCalled() expect(handleAbort2).not.toBeCalled() await api.responseReadable.cancel() expect(handleAbort1).toBeCalled() expect(handleAbort2).toBeCalled() })
it('abort()', async () => { const { readable, writable } = new TransformStream() const handleAbort1 = vi.fn() const handleAbort2 = vi.fn() const api = new StreamingApi(writable, readable) api.onAbort(handleAbort1) api.onAbort(handleAbort2) expect(handleAbort1).not.toBeCalled() expect(handleAbort2).not.toBeCalled() expect(api.aborted).toBe(false)
api.abort() expect(handleAbort1).toHaveBeenCalledOnce() expect(handleAbort2).toHaveBeenCalledOnce() expect(api.aborted).toBe(true)
api.abort() expect(handleAbort1).toHaveBeenCalledOnce() expect(handleAbort2).toHaveBeenCalledOnce() expect(api.aborted).toBe(true) })
it('abort() continues when an abort listener throws', async () => { const { readable, writable } = new TransformStream() const handleAbort = vi.fn() const api = new StreamingApi(writable, readable) api.onAbort(() => { throw new Error('cleanup failed') }) api.onAbort(handleAbort) api.abort() expect(api.aborted).toBe(true) expect(handleAbort).toHaveBeenCalledOnce() })
it('abort() handles a rejecting thenable returned by a listener', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const unhandled = vi.fn() process.on('unhandledRejection', unhandled) try { api.onAbort( () => ({ then: (_resolve: unknown, reject: (reason: unknown) => void) => reject(new Error('thenable cleanup failed')), }) as unknown as Promise<void> ) api.abort() expect(api.aborted).toBe(true) await new Promise((resolve) => setTimeout(resolve, 0)) expect(unhandled).not.toHaveBeenCalled() } finally { process.off('unhandledRejection', unhandled) } })
it('abort() does not leave an unhandled rejection when an async listener rejects', async () => { const { readable, writable } = new TransformStream() const api = new StreamingApi(writable, readable) const unhandled = vi.fn() process.on('unhandledRejection', unhandled) try { api.onAbort(async () => { throw new Error('async cleanup failed') }) api.abort() expect(api.aborted).toBe(true) await new Promise((resolve) => setTimeout(resolve, 0)) expect(unhandled).not.toHaveBeenCalled() } finally { process.off('unhandledRejection', unhandled) } })})