Files
promptfoo--promptfoo/test/scheduler/rateLimitRegistry.behavior.test.ts
wehub-resource-sync 0d3cb498a3
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
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
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) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

156 lines
4.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { RateLimitRegistry } from '../../src/scheduler/rateLimitRegistry';
import { getFetchRetryContextMaxRetries } from '../../src/util/fetch/retryContext';
import type { ApiProvider } from '../../src/types/providers';
function createProvider(maxRetries?: unknown, id = 'test-provider'): ApiProvider {
const config = maxRetries === undefined ? {} : { maxRetries };
return {
id: () => id,
config,
callApi: vi.fn(),
} as unknown as ApiProvider;
}
async function runRateLimitedCall(maxRetries?: unknown): Promise<number> {
const registry = new RateLimitRegistry({
maxConcurrency: 1,
queueTimeoutMs: 100,
});
const provider = createProvider(maxRetries);
const callFn = vi.fn().mockResolvedValue({ status: 429 });
try {
await expect(
registry.execute(provider, callFn, {
isRateLimited: (result) => (result as { status?: number } | undefined)?.status === 429,
getRetryAfter: () => 0,
}),
).rejects.toThrow('Rate limit exceeded');
} finally {
registry.dispose();
}
return callFn.mock.calls.length;
}
describe('RateLimitRegistry integration - provider maxRetries', () => {
afterEach(() => {
vi.resetAllMocks();
vi.unstubAllEnvs();
});
it('should propagate provider maxRetries into the fetch retry context', async () => {
const registry = new RateLimitRegistry({
maxConcurrency: 1,
queueTimeoutMs: 100,
});
const provider = createProvider(0);
const callFn = vi.fn().mockImplementation(async () => getFetchRetryContextMaxRetries());
try {
const contextValue = await registry.execute(provider, callFn);
expect(contextValue).toBe(0);
} finally {
registry.dispose();
}
});
it('should clear an outer retry context when a nested provider has no maxRetries', async () => {
// Use distinct provider ids so the outer and inner calls map to separate
// ProviderRateLimitState instances — otherwise shared slot queue state
// could mask the ALS-scope behavior we're asserting.
const registry = new RateLimitRegistry({
maxConcurrency: 2,
queueTimeoutMs: 100,
});
const outerProvider = createProvider(0, 'outer-provider');
const innerProvider = createProvider(undefined, 'inner-provider');
try {
const contextValue = await registry.execute(outerProvider, () =>
registry.execute(innerProvider, async () => getFetchRetryContextMaxRetries()),
);
expect(contextValue).toBeUndefined();
} finally {
registry.dispose();
}
});
it('should propagate fetch retry context when scheduler is disabled', async () => {
vi.stubEnv('PROMPTFOO_DISABLE_ADAPTIVE_SCHEDULER', 'true');
try {
const registry = new RateLimitRegistry({
maxConcurrency: 1,
queueTimeoutMs: 100,
});
const provider = createProvider(0);
const callFn = vi.fn().mockImplementation(async () => getFetchRetryContextMaxRetries());
try {
const contextValue = await registry.execute(provider, callFn);
expect(contextValue).toBe(0);
} finally {
registry.dispose();
}
} finally {
vi.unstubAllEnvs();
}
});
it('should not retry when provider maxRetries is 0', async () => {
expect(await runRateLimitedCall(0)).toBe(1);
});
it('should retry provider maxRetries + 1 total attempts', async () => {
expect(await runRateLimitedCall(2)).toBe(3);
});
it('should parse numeric string maxRetries values', async () => {
expect(await runRateLimitedCall('2')).toBe(3);
});
it('should use default scheduler retries (4 attempts) when provider maxRetries is not set', async () => {
expect(await runRateLimitedCall(undefined)).toBe(4);
});
it('should ignore negative maxRetries and use default scheduler retries', async () => {
expect(await runRateLimitedCall(-1)).toBe(4);
});
it('should ignore non-integer string maxRetries values', async () => {
expect(await runRateLimitedCall('2.5')).toBe(4);
});
it('should ignore non-integer number maxRetries values', async () => {
expect(await runRateLimitedCall(2.5)).toBe(4);
});
it('should isolate retry context per concurrent provider', async () => {
const registry = new RateLimitRegistry({
maxConcurrency: 4,
queueTimeoutMs: 100,
});
async function capture(): Promise<number | undefined> {
const ctx = getFetchRetryContextMaxRetries();
// Yield so both calls can interleave before returning — guards against
// a regression where the context from the later call leaks into the earlier.
await new Promise((resolve) => setImmediate(resolve));
return ctx;
}
try {
const [a, b] = await Promise.all([
registry.execute(createProvider(0, 'p0'), capture),
registry.execute(createProvider(5, 'p5'), capture),
]);
expect(a).toBe(0);
expect(b).toBe(5);
} finally {
registry.dispose();
}
});
});