Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–59why, one step back
1import { StreamingApi } from './stream'
23describe('StreamingApi', () => {4 it('write(string)', async () => {5 const { readable, writable } = new TransformStream()6 const api = new StreamingApi(writable, readable)7 const reader = api.responseReadable.getReader()8 api.write('foo')9 expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo'))10 api.write('bar')11 expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar'))12 })1314 it('write(Uint8Array)', async () => {15 const { readable, writable } = new TransformStream()16 const api = new StreamingApi(writable, readable)17 const reader = api.responseReadable.getReader()18 api.write(new Uint8Array([1, 2, 3]))19 expect((await reader.read()).value).toEqual(new Uint8Array([1, 2, 3]))20 api.write(new Uint8Array([4, 5, 6]))21 expect((await reader.read()).value).toEqual(new Uint8Array([4, 5, 6]))22 })2324 it('writeln(string)', async () => {25 const { readable, writable } = new TransformStream()26 const api = new StreamingApi(writable, readable)27 const reader = api.responseReadable.getReader()28 api.writeln('foo')29 expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo\n'))30 api.writeln('bar')31 expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar\n'))32 })3334 it('pipe()', async () => {35 const { readable: senderReadable, writable: senderWritable } = new TransformStream()3637 // send data to readable in other scope38 ;(async () => {39 const writer = senderWritable.getWriter()40 await writer.write(new TextEncoder().encode('foo'))41 await writer.write(new TextEncoder().encode('bar'))42 // await writer.close()43 })()4445 const { readable: receiverReadable, writable: receiverWritable } = new TransformStream()4647 const api = new StreamingApi(receiverWritable, receiverReadable)4849 // pipe readable to api in other scope50 ;(async () => {51 await api.pipe(senderReadable)52 })()5354 // read data from api55 const reader = api.responseReadable.getReader()56 expect((await reader.read()).value).toEqual(new TextEncoder().encode('foo'))57 expect((await reader.read()).value).toEqual(new TextEncoder().encode('bar'))58 })59

fix(utils/stream): re-acquire writer lock when pipe() throws (#4988)Sriharsha-dev369before adoptioncd31bc1

lines 60–97why, one step back
60 it('pipe() re-acquires writer lock when pipeTo() throws', async () => {
61 const { readable, writable } = new TransformStream()62 const api = new StreamingApi(writable, readable)63 const reader = api.responseReadable.getReader()6465 const erroringReadable = new ReadableStream({66 start(controller) {67 controller.error(new Error('upstream error'))68 },69 })7071 await expect(api.pipe(erroringReadable)).rejects.toThrow('upstream error')72 api.write('after pipe')73 expect((await reader.read()).value).toEqual(new TextEncoder().encode('after pipe'))74 })7576 it('pipe() can be called again after a previous pipe() throws', async () => {77 const { readable, writable } = new TransformStream()78 const api = new StreamingApi(writable, readable)79 const reader = api.responseReadable.getReader()8081 const erroringReadable = new ReadableStream({82 start(controller) {83 controller.error(new Error('upstream error'))84 },85 })86 await expect(api.pipe(erroringReadable)).rejects.toThrow('upstream error')8788 const src = new ReadableStream({89 start(controller) {90 controller.enqueue(new TextEncoder().encode('second pipe'))91 controller.close()92 },93 })94 await api.pipe(src)95 expect((await reader.read()).value).toEqual(new TextEncoder().encode('second pipe'))96 })97

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 98–158why, one step back
98 it('close()', async () => {
99 const { readable, writable } = new TransformStream()100 const api = new StreamingApi(writable, readable)101 const reader = api.responseReadable.getReader()102 await api.close()103 expect((await reader.read()).done).toBe(true)104 })105106 it('should not throw an error in write()', async () => {107 const { readable, writable } = new TransformStream()108 const api = new StreamingApi(writable, readable)109 await api.close()110 const write = () => api.write('foo')111 expect(write).not.toThrow()112 })113114 it('should not throw an error in close()', async () => {115 const { readable, writable } = new TransformStream()116 const api = new StreamingApi(writable, readable)117 const close = async () => {118 await api.close()119 await api.close()120 }121 expect(close).not.toThrow()122 })123124 it('onAbort()', async () => {125 const { readable, writable } = new TransformStream()126 const handleAbort1 = vi.fn()127 const handleAbort2 = vi.fn()128 const api = new StreamingApi(writable, readable)129 api.onAbort(handleAbort1)130 api.onAbort(handleAbort2)131 expect(handleAbort1).not.toBeCalled()132 expect(handleAbort2).not.toBeCalled()133 await api.responseReadable.cancel()134 expect(handleAbort1).toBeCalled()135 expect(handleAbort2).toBeCalled()136 })137138 it('abort()', async () => {139 const { readable, writable } = new TransformStream()140 const handleAbort1 = vi.fn()141 const handleAbort2 = vi.fn()142 const api = new StreamingApi(writable, readable)143 api.onAbort(handleAbort1)144 api.onAbort(handleAbort2)145 expect(handleAbort1).not.toBeCalled()146 expect(handleAbort2).not.toBeCalled()147 expect(api.aborted).toBe(false)148149 api.abort()150 expect(handleAbort1).toHaveBeenCalledOnce()151 expect(handleAbort2).toHaveBeenCalledOnce()152 expect(api.aborted).toBe(true)153154 api.abort()155 expect(handleAbort1).toHaveBeenCalledOnce()156 expect(handleAbort2).toHaveBeenCalledOnce()157 expect(api.aborted).toBe(true)158 })

fix(utils/stream): do not let abort listeners crash abort() (#5274)Tushar Pagarbefore adoption5e5b83d

lines 159–211why, one step back
159
160 it('abort() continues when an abort listener throws', async () => {161 const { readable, writable } = new TransformStream()162 const handleAbort = vi.fn()163 const api = new StreamingApi(writable, readable)164 api.onAbort(() => {165 throw new Error('cleanup failed')166 })167 api.onAbort(handleAbort)168 api.abort()169 expect(api.aborted).toBe(true)170 expect(handleAbort).toHaveBeenCalledOnce()171 })172173 it('abort() handles a rejecting thenable returned by a listener', async () => {174 const { readable, writable } = new TransformStream()175 const api = new StreamingApi(writable, readable)176 const unhandled = vi.fn()177 process.on('unhandledRejection', unhandled)178 try {179 api.onAbort(180 () =>181 ({182 then: (_resolve: unknown, reject: (reason: unknown) => void) =>183 reject(new Error('thenable cleanup failed')),184 }) as unknown as Promise<void>185 )186 api.abort()187 expect(api.aborted).toBe(true)188 await new Promise((resolve) => setTimeout(resolve, 0))189 expect(unhandled).not.toHaveBeenCalled()190 } finally {191 process.off('unhandledRejection', unhandled)192 }193 })194195 it('abort() does not leave an unhandled rejection when an async listener rejects', async () => {196 const { readable, writable } = new TransformStream()197 const api = new StreamingApi(writable, readable)198 const unhandled = vi.fn()199 process.on('unhandledRejection', unhandled)200 try {201 api.onAbort(async () => {202 throw new Error('async cleanup failed')203 })204 api.abort()205 expect(api.aborted).toBe(true)206 await new Promise((resolve) => setTimeout(resolve, 0))207 expect(unhandled).not.toHaveBeenCalled()208 } finally {209 process.off('unhandledRejection', unhandled)210 }211 })

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 212why, one step back
212})