Files
promptfoo--promptfoo/test/rateLimit.test.ts
T
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

135 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { fetchWithRetries } from '../src/util/fetch/index';
import { mockGlobal } from './util/utils';
const mockedFetchResponse = (ok: boolean, response: object, headers: object = {}) => {
const responseText = JSON.stringify(response);
return {
ok,
status: ok ? 200 : 429,
statusText: ok ? 'OK' : 'Too Many Requests',
text: () => Promise.resolve(responseText),
json: () => Promise.resolve(response),
headers: new Headers({
'content-type': 'application/json',
...headers,
}),
} as Response;
};
const mockedSetTimeout = (reqTimeout: number) =>
vi.spyOn(global, 'setTimeout').mockImplementation((cb: () => void, ms?: number) => {
if (ms !== reqTimeout) {
cb();
}
return 0 as any;
});
// Create a mock function that will be used for fetch
const mockFetch = vi.fn();
describe('fetchWithRetries', () => {
let restoreFetch: (() => void) | undefined;
beforeEach(() => {
vi.useFakeTimers();
restoreFetch = mockGlobal('fetch', mockFetch);
});
afterEach(() => {
mockFetch.mockReset();
vi.useRealTimers();
restoreFetch?.();
restoreFetch = undefined;
});
it('should fetch data', async () => {
const url = 'https://api.example.com/data';
const response = { data: 'test data' };
mockFetch.mockResolvedValueOnce(mockedFetchResponse(true, response));
const result = await fetchWithRetries(url, {}, 1000);
expect(mockFetch).toHaveBeenCalledTimes(1);
await expect(result.json()).resolves.toEqual(response);
});
it('should retry after given time if rate limited, using X-Limit headers', async () => {
const url = 'https://api.example.com/data';
const response = { data: 'test data' };
const rateLimitReset = 47_000;
const timeout = 1234;
const now = Date.now();
const setTimeoutMock = mockedSetTimeout(timeout);
mockFetch
.mockResolvedValueOnce(
mockedFetchResponse(false, response, {
'X-RateLimit-Remaining': '0',
'X-RateLimit-Reset': `${(now + rateLimitReset) / 1000}`,
}),
)
.mockResolvedValueOnce(mockedFetchResponse(true, response));
const result = await fetchWithRetries(url, {}, timeout);
const waitTime = setTimeoutMock.mock.calls[1][1];
expect(mockFetch).toHaveBeenCalledTimes(2);
// Base wait = `(reset_seconds * 1000) - now + 1000` from
// computeRateLimitWaitMs. parseInt on the seconds value truncates the
// sub-second portion, so the base sits in [rateLimitReset, rateLimitReset+1000].
// handleRateLimit then adds 0999ms of jitter, so the captured wait
// spans approximately [rateLimitReset, rateLimitReset+2000).
expect(waitTime).toBeGreaterThan(rateLimitReset);
expect(waitTime).toBeLessThan(rateLimitReset + 2000);
await expect(result.json()).resolves.toEqual(response);
});
it('should retry after given time if rate limited, using status and Retry-After', async () => {
const url = 'https://api.example.com/data';
const response = { data: 'test data' };
const retryAfter = 15;
const timeout = 1234;
const setTimeoutMock = mockedSetTimeout(timeout);
mockFetch
.mockResolvedValueOnce(
mockedFetchResponse(false, response, { 'Retry-After': String(retryAfter) }),
)
.mockResolvedValueOnce(mockedFetchResponse(true, response));
const result = await fetchWithRetries(url, {}, timeout);
const waitTime = setTimeoutMock.mock.calls[1][1];
expect(mockFetch).toHaveBeenCalledTimes(2);
// Base wait = Retry-After seconds; handleRateLimit adds 0999ms jitter.
expect(waitTime).toBeGreaterThanOrEqual(retryAfter * 1000);
expect(waitTime).toBeLessThan(retryAfter * 1000 + 1000);
await expect(result.json()).resolves.toEqual(response);
});
it('should retry after default wait time if rate limited and wait time not found', async () => {
const url = 'https://api.example.com/data';
const response = { data: 'test data' };
const timeout = 1234;
const setTimeoutMock = mockedSetTimeout(timeout);
mockFetch
.mockResolvedValueOnce(mockedFetchResponse(false, response))
.mockResolvedValueOnce(mockedFetchResponse(true, response));
const result = await fetchWithRetries(url, {}, timeout);
const waitTime = setTimeoutMock.mock.calls[1][1];
expect(mockFetch).toHaveBeenCalledTimes(2);
// Default base = 60s; handleRateLimit adds 0999ms jitter.
expect(waitTime).toBeGreaterThanOrEqual(60_000);
expect(waitTime).toBeLessThan(61_000);
await expect(result.json()).resolves.toEqual(response);
});
});