|
| 1 | +import 'jest' |
| 2 | +import { nanoid } from 'nanoid' |
| 3 | +import { sign } from 'jsonwebtoken' |
| 4 | + |
| 5 | +import { FunctionsClient } from '../../src/index' |
| 6 | + |
| 7 | +import { Relay, runRelay } from '../relay/container' |
| 8 | +import { log } from '../utils/jest-custom-reporter' |
| 9 | + |
| 10 | +describe('timeout tests (slow function)', () => { |
| 11 | + let relay: Relay |
| 12 | + const jwtSecret = nanoid(10) |
| 13 | + const apiKey = sign({ name: 'anon' }, jwtSecret) |
| 14 | + |
| 15 | + beforeAll(async () => { |
| 16 | + relay = await runRelay('slow', jwtSecret) |
| 17 | + }) |
| 18 | + |
| 19 | + afterAll(async () => { |
| 20 | + if (relay) { |
| 21 | + await relay.stop() |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + test('invoke slow function without timeout should succeed', async () => { |
| 26 | + /** |
| 27 | + * @feature timeout |
| 28 | + */ |
| 29 | + log('create FunctionsClient') |
| 30 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, { |
| 31 | + headers: { |
| 32 | + Authorization: `Bearer ${apiKey}`, |
| 33 | + }, |
| 34 | + }) |
| 35 | + |
| 36 | + log('invoke slow without timeout') |
| 37 | + const { data, error } = await fclient.invoke<string>('slow', {}) |
| 38 | + |
| 39 | + log('assert no error') |
| 40 | + expect(error).toBeNull() |
| 41 | + log(`assert ${data} is equal to 'Slow Response'`) |
| 42 | + expect(data).toEqual('Slow Response') |
| 43 | + }) |
| 44 | + |
| 45 | + test('invoke slow function with short timeout should fail', async () => { |
| 46 | + /** |
| 47 | + * @feature timeout |
| 48 | + */ |
| 49 | + log('create FunctionsClient') |
| 50 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, { |
| 51 | + headers: { |
| 52 | + Authorization: `Bearer ${apiKey}`, |
| 53 | + }, |
| 54 | + }) |
| 55 | + |
| 56 | + log('invoke slow with 1000ms timeout (function takes 3000ms)') |
| 57 | + const { data, error } = await fclient.invoke<string>('slow', { |
| 58 | + timeout: 1000, |
| 59 | + }) |
| 60 | + |
| 61 | + log('assert error occurred') |
| 62 | + expect(error).not.toBeNull() |
| 63 | + expect(error?.name).toEqual('FunctionsFetchError') |
| 64 | + expect(data).toBeNull() |
| 65 | + }) |
| 66 | + |
| 67 | + test('invoke slow function with long timeout should succeed', async () => { |
| 68 | + /** |
| 69 | + * @feature timeout |
| 70 | + */ |
| 71 | + log('create FunctionsClient') |
| 72 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, { |
| 73 | + headers: { |
| 74 | + Authorization: `Bearer ${apiKey}`, |
| 75 | + }, |
| 76 | + }) |
| 77 | + |
| 78 | + log('invoke slow with 5000ms timeout (function takes 3000ms)') |
| 79 | + const { data, error } = await fclient.invoke<string>('slow', { |
| 80 | + timeout: 5000, |
| 81 | + }) |
| 82 | + |
| 83 | + log('assert no error') |
| 84 | + expect(error).toBeNull() |
| 85 | + log(`assert ${data} is equal to 'Slow Response'`) |
| 86 | + expect(data).toEqual('Slow Response') |
| 87 | + }) |
| 88 | + |
| 89 | + test('invoke slow function with timeout and custom AbortSignal', async () => { |
| 90 | + /** |
| 91 | + * @feature timeout |
| 92 | + */ |
| 93 | + log('create FunctionsClient') |
| 94 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, { |
| 95 | + headers: { |
| 96 | + Authorization: `Bearer ${apiKey}`, |
| 97 | + }, |
| 98 | + }) |
| 99 | + |
| 100 | + const abortController = new AbortController() |
| 101 | + |
| 102 | + log('invoke slow with both timeout and AbortSignal') |
| 103 | + const invokePromise = fclient.invoke<string>('slow', { |
| 104 | + timeout: 5000, // 5 second timeout |
| 105 | + signal: abortController.signal, |
| 106 | + }) |
| 107 | + |
| 108 | + // Abort after 500ms |
| 109 | + setTimeout(() => { |
| 110 | + log('aborting request via AbortController') |
| 111 | + abortController.abort() |
| 112 | + }, 500) |
| 113 | + |
| 114 | + const { data, error } = await invokePromise |
| 115 | + |
| 116 | + log('assert error occurred from abort') |
| 117 | + expect(error).not.toBeNull() |
| 118 | + expect(error?.name).toEqual('FunctionsFetchError') |
| 119 | + expect(data).toBeNull() |
| 120 | + }) |
| 121 | +}) |
0 commit comments