|
| 1 | +import type { TimeoutHandlerPluginOptions } from './timeout' |
| 2 | +import { COMMON_ERROR_STATUS_MAP, ORPCError } from '@orpc/client' |
| 3 | +import { AbortError } from '@orpc/shared' |
| 4 | +import { RPCHandler } from '../adapters/fetch' |
| 5 | +import { os } from '../builder' |
| 6 | +import { TimeoutHandlerPlugin } from './timeout' |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + vi.clearAllMocks() |
| 10 | + vi.useFakeTimers() |
| 11 | +}) |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + expect(vi.getTimerCount()).toBe(0) |
| 15 | + vi.useRealTimers() |
| 16 | +}) |
| 17 | + |
| 18 | +describe('timeoutHandlerPlugin', () => { |
| 19 | + const procedure_handler = vi.fn() |
| 20 | + const procedure = os.handler(procedure_handler) |
| 21 | + |
| 22 | + function makeHandler(options: TimeoutHandlerPluginOptions<any>, router: any = procedure) { |
| 23 | + return new RPCHandler(router, { |
| 24 | + allowMethods: ['GET'], // tests below send GET requests |
| 25 | + plugins: [new TimeoutHandlerPlugin(options)], |
| 26 | + }) |
| 27 | + } |
| 28 | + |
| 29 | + /** A sleep that honors the signal by rejecting with the abort reason, or ignores it when no signal is given. */ |
| 30 | + function sleepWithSignal(ms: number, signal?: AbortSignal): Promise<void> { |
| 31 | + return new Promise((resolve, reject) => { |
| 32 | + const timer = setTimeout(resolve, ms) |
| 33 | + signal?.addEventListener('abort', () => { |
| 34 | + clearTimeout(timer) |
| 35 | + reject(signal.reason) |
| 36 | + }, { once: true }) |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + it('should respond and clear the timeout when the procedure finishes in time', async () => { |
| 41 | + procedure_handler.mockResolvedValueOnce('success') |
| 42 | + const handler = makeHandler({ timeout: 5000 }) |
| 43 | + |
| 44 | + const { response } = await handler.handle(new Request('http://localhost')) |
| 45 | + expect(response?.status).toBe(200) |
| 46 | + |
| 47 | + const { signal } = procedure_handler.mock.calls[0]![0] |
| 48 | + expect(signal).toBeInstanceOf(AbortSignal) |
| 49 | + expect(signal.aborted).toBe(false) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should abort the signal with an AbortError and respond once the procedure honors it', async () => { |
| 53 | + procedure_handler.mockImplementationOnce(({ signal }) => sleepWithSignal(100_000, signal)) |
| 54 | + const handler = makeHandler({ timeout: 1000 }) |
| 55 | + |
| 56 | + const promise = handler.handle(new Request('http://localhost')) |
| 57 | + await vi.advanceTimersByTimeAsync(1000) |
| 58 | + |
| 59 | + const { response } = await promise |
| 60 | + expect(response?.status).toBe(COMMON_ERROR_STATUS_MAP.INTERNAL_SERVER_ERROR) |
| 61 | + |
| 62 | + const { signal } = procedure_handler.mock.calls[0]![0] |
| 63 | + expect(signal.aborted).toBe(true) |
| 64 | + expect(signal.reason).toBeInstanceOf(AbortError) |
| 65 | + expect(signal.reason.message).toBe('Request timed out after 1000ms') |
| 66 | + }) |
| 67 | + |
| 68 | + it('should not preempt a procedure that ignores the abort signal', async () => { |
| 69 | + procedure_handler.mockImplementationOnce(async () => { |
| 70 | + await sleepWithSignal(100_000) |
| 71 | + return 'late success' |
| 72 | + }) |
| 73 | + const handler = makeHandler({ timeout: 1000 }) |
| 74 | + |
| 75 | + const promise = handler.handle(new Request('http://localhost')) |
| 76 | + |
| 77 | + await vi.advanceTimersByTimeAsync(1000) |
| 78 | + expect(procedure_handler.mock.calls[0]![0].signal.aborted).toBe(true) |
| 79 | + |
| 80 | + await vi.advanceTimersByTimeAsync(99_000) |
| 81 | + const { response } = await promise |
| 82 | + expect(response?.status).toBe(200) // the late result is still delivered |
| 83 | + }) |
| 84 | + |
| 85 | + it('should not mask procedure errors thrown after the timeout', async () => { |
| 86 | + procedure_handler.mockImplementationOnce(async () => { |
| 87 | + await sleepWithSignal(100_000) |
| 88 | + throw new ORPCError('NOT_ACCEPTABLE') |
| 89 | + }) |
| 90 | + const handler = makeHandler({ timeout: 1000 }) |
| 91 | + |
| 92 | + const promise = handler.handle(new Request('http://localhost')) |
| 93 | + await vi.advanceTimersByTimeAsync(100_000) |
| 94 | + |
| 95 | + const { response } = await promise |
| 96 | + expect(response?.status).toBe(COMMON_ERROR_STATUS_MAP.NOT_ACCEPTABLE) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should support dynamic timeout based on interceptor options', async () => { |
| 100 | + procedure_handler.mockImplementationOnce(({ signal }) => sleepWithSignal(100_000, signal)) |
| 101 | + |
| 102 | + const timeout = vi.fn(({ context }: any) => context.timeout) |
| 103 | + const handler = makeHandler({ timeout }) |
| 104 | + |
| 105 | + const promise = handler.handle(new Request('http://localhost'), { context: { timeout: 1000 } }) |
| 106 | + await vi.advanceTimersByTimeAsync(1000) |
| 107 | + |
| 108 | + const { response } = await promise |
| 109 | + expect(response?.status).toBe(COMMON_ERROR_STATUS_MAP.INTERNAL_SERVER_ERROR) |
| 110 | + |
| 111 | + expect(timeout).toHaveBeenCalledTimes(1) |
| 112 | + expect(timeout).toHaveBeenCalledWith(expect.objectContaining({ |
| 113 | + path: [], |
| 114 | + procedure, |
| 115 | + context: { timeout: 1000 }, |
| 116 | + })) |
| 117 | + }) |
| 118 | + |
| 119 | + it.each([null, undefined])('should dynamically disable timeout when value is %s', async (timeout) => { |
| 120 | + procedure_handler.mockImplementationOnce(() => sleepWithSignal(100_000).then(() => 'success')) |
| 121 | + const handler = makeHandler({ timeout: () => timeout }) |
| 122 | + |
| 123 | + const promise = handler.handle(new Request('http://localhost')) |
| 124 | + await vi.advanceTimersByTimeAsync(100_000) |
| 125 | + |
| 126 | + const { response } = await promise |
| 127 | + expect(response?.status).toBe(200) |
| 128 | + }) |
| 129 | + |
| 130 | + it.each([ |
| 131 | + ['not provided', {}], |
| 132 | + ['null', { streamingTimeout: () => null }], |
| 133 | + ['undefined', { streamingTimeout: () => undefined }], |
| 134 | + ])('should let streaming responses outlive the timeout when streamingTimeout is %s', async (_, options) => { |
| 135 | + const handler = makeHandler({ timeout: 1000, ...options }, os.handler(async function* () { |
| 136 | + yield 'first' |
| 137 | + await sleepWithSignal(100_000) |
| 138 | + yield 'second' |
| 139 | + })) |
| 140 | + |
| 141 | + const { response } = await handler.handle(new Request('http://localhost')) |
| 142 | + expect(response?.status).toBe(200) |
| 143 | + |
| 144 | + const textPromise = response!.text() |
| 145 | + await vi.advanceTimersByTimeAsync(100_000) |
| 146 | + |
| 147 | + const text = await textPromise |
| 148 | + expect(text).toContain('first') |
| 149 | + expect(text).toContain('second') |
| 150 | + expect(text).not.toContain('event: error') |
| 151 | + }) |
| 152 | + |
| 153 | + it('should end event iterator bodies with an error event when streamingTimeout is exceeded', async () => { |
| 154 | + let cleaned = false |
| 155 | + const handler = makeHandler({ timeout: 1000, streamingTimeout: 2000 }, os.handler(async function* ({ signal }) { |
| 156 | + try { |
| 157 | + yield 'first' |
| 158 | + await sleepWithSignal(100_000, signal) |
| 159 | + yield 'second' |
| 160 | + } |
| 161 | + finally { |
| 162 | + cleaned = true |
| 163 | + } |
| 164 | + })) |
| 165 | + |
| 166 | + const { response } = await handler.handle(new Request('http://localhost')) |
| 167 | + expect(response?.status).toBe(200) |
| 168 | + |
| 169 | + const textPromise = response!.text() |
| 170 | + await vi.advanceTimersByTimeAsync(2000) |
| 171 | + |
| 172 | + const text = await textPromise |
| 173 | + expect(text).toContain('first') |
| 174 | + expect(text).not.toContain('second') |
| 175 | + expect(text).toContain('event: error') |
| 176 | + expect(cleaned).toBe(true) |
| 177 | + }) |
| 178 | + |
| 179 | + it('should end readable stream bodies once the producer honors the signal when streamingTimeout is exceeded', async () => { |
| 180 | + const handler = new RPCHandler(procedure, { |
| 181 | + allowMethods: ['GET'], |
| 182 | + interceptors: [async ({ request }) => ({ |
| 183 | + status: 200, |
| 184 | + headers: {}, |
| 185 | + body: new ReadableStream<Uint8Array>({ |
| 186 | + start(controller) { |
| 187 | + controller.enqueue(new TextEncoder().encode('first')) |
| 188 | + request.signal!.addEventListener('abort', () => { |
| 189 | + controller.error(request.signal!.reason) |
| 190 | + }, { once: true }) |
| 191 | + }, |
| 192 | + }), |
| 193 | + })], |
| 194 | + plugins: [new TimeoutHandlerPlugin({ timeout: 1000, streamingTimeout: 2000 })], |
| 195 | + }) |
| 196 | + |
| 197 | + const { response } = await handler.handle(new Request('http://localhost')) |
| 198 | + expect(response?.status).toBe(200) |
| 199 | + |
| 200 | + const reader = response!.body!.getReader() |
| 201 | + await expect(reader.read()).resolves.toEqual({ done: false, value: new TextEncoder().encode('first') }) |
| 202 | + |
| 203 | + const nextRead = reader.read() |
| 204 | + nextRead.catch(() => {}) |
| 205 | + await vi.advanceTimersByTimeAsync(2000) |
| 206 | + |
| 207 | + await expect(nextRead).rejects.toSatisfy(error => |
| 208 | + error instanceof AbortError && error.message === 'Request timed out after 2000ms', |
| 209 | + ) |
| 210 | + }) |
| 211 | + |
| 212 | + it('should let streaming responses finish in time and clear the streaming timeout', async () => { |
| 213 | + const handler = makeHandler({ timeout: 1000, streamingTimeout: 5000 }, os.handler(async function* () { |
| 214 | + yield 'first' |
| 215 | + await sleepWithSignal(1000) |
| 216 | + yield 'second' |
| 217 | + })) |
| 218 | + |
| 219 | + const { response } = await handler.handle(new Request('http://localhost')) |
| 220 | + const textPromise = response!.text() |
| 221 | + await vi.advanceTimersByTimeAsync(1000) |
| 222 | + |
| 223 | + const text = await textPromise |
| 224 | + expect(text).toContain('first') |
| 225 | + expect(text).toContain('second') |
| 226 | + expect(text).not.toContain('event: error') |
| 227 | + }) |
| 228 | + |
| 229 | + it('should support dynamic streamingTimeout based on interceptor options', async () => { |
| 230 | + const streamingTimeout = vi.fn(({ context }: any) => context.streamingTimeout) |
| 231 | + const handler = makeHandler({ timeout: 1000, streamingTimeout }, os.handler(async function* ({ signal }) { |
| 232 | + yield 'first' |
| 233 | + await sleepWithSignal(100_000, signal) |
| 234 | + yield 'second' |
| 235 | + })) |
| 236 | + |
| 237 | + const { response } = await handler.handle(new Request('http://localhost'), { |
| 238 | + context: { streamingTimeout: 2000 }, |
| 239 | + }) |
| 240 | + |
| 241 | + const textPromise = response!.text() |
| 242 | + await vi.advanceTimersByTimeAsync(2000) |
| 243 | + |
| 244 | + const text = await textPromise |
| 245 | + expect(text).toContain('first') |
| 246 | + expect(text).not.toContain('second') |
| 247 | + expect(text).toContain('event: error') |
| 248 | + |
| 249 | + expect(streamingTimeout).toHaveBeenCalledTimes(1) |
| 250 | + expect(streamingTimeout).toHaveBeenCalledWith(expect.objectContaining({ |
| 251 | + context: { streamingTimeout: 2000 }, |
| 252 | + })) |
| 253 | + }) |
| 254 | + |
| 255 | + it('should forward abort from the request signal while the timeout is still active', async () => { |
| 256 | + procedure_handler.mockImplementationOnce(({ signal }) => sleepWithSignal(100_000, signal)) |
| 257 | + const handler = makeHandler({ timeout: 5000 }) |
| 258 | + |
| 259 | + const controller = new AbortController() |
| 260 | + const promise = handler.handle(new Request('http://localhost', { signal: controller.signal })) |
| 261 | + |
| 262 | + await vi.advanceTimersByTimeAsync(500) |
| 263 | + controller.abort(new Error('user cancelled')) |
| 264 | + |
| 265 | + const { response } = await promise |
| 266 | + expect(response?.status).toBe(500) // not a timeout response |
| 267 | + }) |
| 268 | +}) |
0 commit comments