|
| 1 | +import { describe, expect } from "vitest"; |
| 2 | +import { test } from "@fast-check/vitest"; |
| 3 | +import type { AxiosError } from "axios"; |
| 4 | +import { toAxiosRetryConfig } from "."; |
| 5 | + |
| 6 | +describe("Ensure retry configuration functions work as expected", () => { |
| 7 | + test("Delay function returns fixed delay when useExponentialBackoff is false", () => { |
| 8 | + const retryDelay = 1234; |
| 9 | + const axiosConfig = toAxiosRetryConfig({ |
| 10 | + retries: 3, |
| 11 | + retryDelay, |
| 12 | + useExponentialBackoff: false, |
| 13 | + }); |
| 14 | + const delayFunction = axiosConfig.retryDelay; |
| 15 | + expect(delayFunction?.(3, {} as AxiosError)).toEqual(retryDelay); |
| 16 | + }); |
| 17 | + test("Delay function returns retryDelay when useExponentialBackoff is true on first retry", () => { |
| 18 | + const retryDelay = 1234; |
| 19 | + const axiosConfig = toAxiosRetryConfig({ retries: 3, retryDelay, useExponentialBackoff: true }); |
| 20 | + const delayFunction = axiosConfig.retryDelay; |
| 21 | + expect(delayFunction?.(1, {} as AxiosError)).toEqual(retryDelay); |
| 22 | + }); |
| 23 | + test("Delay function returns retryDelay * 2 * 2 when useExponentialBackoff is true on fourth retry", () => { |
| 24 | + const retryDelay = 1234; |
| 25 | + const axiosConfig = toAxiosRetryConfig({ retries: 4, retryDelay, useExponentialBackoff: true }); |
| 26 | + const delayFunction = axiosConfig.retryDelay; |
| 27 | + expect(delayFunction?.(4, {} as AxiosError)).toEqual(retryDelay * 2 ** 3); |
| 28 | + }); |
| 29 | + test("Delay function returns exponential delay when useExponentialBackoff is true and retryDelay is not set", () => { |
| 30 | + const axiosConfig = toAxiosRetryConfig({ retries: 3, useExponentialBackoff: true }); |
| 31 | + const delayFunction = axiosConfig.retryDelay; |
| 32 | + const delay1 = delayFunction?.(1, {} as AxiosError); |
| 33 | + const delay2 = delayFunction?.(2, {} as AxiosError); |
| 34 | + const delay3 = delayFunction?.(3, {} as AxiosError); |
| 35 | + |
| 36 | + // By default, axios-retry's exponentialDelay function returns 200 (+20% random jitter) on first retry, |
| 37 | + // 400 (+20% random jitter) on second retry, 800 (+20% random jitter) on third retry, etc. |
| 38 | + expect(delay1).toBeGreaterThanOrEqual(100 * 2); |
| 39 | + expect(delay1).toBeLessThanOrEqual(100 * 2 * 1.2); |
| 40 | + expect(delay2).toBeGreaterThanOrEqual(100 * 4); |
| 41 | + expect(delay2).toBeLessThanOrEqual(100 * 4 * 1.2); |
| 42 | + expect(delay3).toBeGreaterThanOrEqual(100 * 8); |
| 43 | + expect(delay3).toBeLessThanOrEqual(100 * 8 * 1.2); |
| 44 | + }); |
| 45 | +}); |
0 commit comments