|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import { backoffDelay } from '../src/utils' |
| 3 | + |
| 4 | +describe('backoffDelay', () => { |
| 5 | + test('returns no delay when backoff is unset', () => { |
| 6 | + expect(backoffDelay(undefined, 1)).toBe(0) |
| 7 | + }) |
| 8 | + |
| 9 | + test('fixed backoff waits the same delay on every attempt', () => { |
| 10 | + const backoff = { type: 'fixed', delay: 5000 } as const |
| 11 | + |
| 12 | + expect(backoffDelay(backoff, 1)).toBe(5000) |
| 13 | + expect(backoffDelay(backoff, 2)).toBe(5000) |
| 14 | + expect(backoffDelay(backoff, 9)).toBe(5000) |
| 15 | + }) |
| 16 | + |
| 17 | + test('exponential backoff doubles from the base delay', () => { |
| 18 | + const backoff = { type: 'exponential', delay: 1000 } as const |
| 19 | + |
| 20 | + expect(backoffDelay(backoff, 1)).toBe(1000) |
| 21 | + expect(backoffDelay(backoff, 2)).toBe(2000) |
| 22 | + expect(backoffDelay(backoff, 3)).toBe(4000) |
| 23 | + expect(backoffDelay(backoff, 4)).toBe(8000) |
| 24 | + }) |
| 25 | + |
| 26 | + test('an array is an explicit per-attempt schedule in milliseconds', () => { |
| 27 | + const backoff = [1000, 5000, 30000] |
| 28 | + |
| 29 | + expect(backoffDelay(backoff, 1)).toBe(1000) |
| 30 | + expect(backoffDelay(backoff, 2)).toBe(5000) |
| 31 | + expect(backoffDelay(backoff, 3)).toBe(30000) |
| 32 | + }) |
| 33 | + |
| 34 | + test('an exhausted schedule clamps to its last entry', () => { |
| 35 | + expect(backoffDelay([1000, 5000], 3)).toBe(5000) |
| 36 | + expect(backoffDelay([1000, 5000], 100)).toBe(5000) |
| 37 | + }) |
| 38 | + |
| 39 | + test('an empty schedule retries immediately', () => { |
| 40 | + expect(backoffDelay([], 1)).toBe(0) |
| 41 | + }) |
| 42 | + |
| 43 | + test('a zero or negative attempt count reads the first entry', () => { |
| 44 | + expect(backoffDelay([1000, 5000], 0)).toBe(1000) |
| 45 | + expect(backoffDelay({ type: 'exponential', delay: 1000 }, 0)).toBe(1000) |
| 46 | + }) |
| 47 | +}) |
0 commit comments