2025-05-30 10:57:00 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
2025-10-03 09:08:27 -07:00
|
|
|
import { ApiError } from '@google/genai';
|
2025-10-03 13:24:50 -07:00
|
|
|
import { AuthType } from '../core/contentGenerator.js';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { HttpError } from './retry.js';
|
|
|
|
|
import { retryWithBackoff } from './retry.js';
|
2025-06-24 18:48:55 -04:00
|
|
|
import { setSimulate429 } from './testUtils.js';
|
2025-10-03 13:24:50 -07:00
|
|
|
import {
|
|
|
|
|
TerminalQuotaError,
|
|
|
|
|
RetryableQuotaError,
|
|
|
|
|
} from './googleQuotaErrors.js';
|
2025-05-30 10:57:00 -07:00
|
|
|
|
|
|
|
|
// Helper to create a mock function that fails a certain number of times
|
|
|
|
|
const createFailingFunction = (
|
|
|
|
|
failures: number,
|
|
|
|
|
successValue: string = 'success',
|
|
|
|
|
) => {
|
|
|
|
|
let attempts = 0;
|
|
|
|
|
return vi.fn(async () => {
|
|
|
|
|
attempts++;
|
|
|
|
|
if (attempts <= failures) {
|
|
|
|
|
// Simulate a retryable error
|
|
|
|
|
const error: HttpError = new Error(`Simulated error attempt ${attempts}`);
|
|
|
|
|
error.status = 500; // Simulate a server error
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
return successValue;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Custom error for testing non-retryable conditions
|
|
|
|
|
class NonRetryableError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = 'NonRetryableError';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('retryWithBackoff', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.useFakeTimers();
|
2025-06-24 18:48:55 -04:00
|
|
|
// Disable 429 simulation for tests
|
|
|
|
|
setSimulate429(false);
|
|
|
|
|
// Suppress unhandled promise rejection warnings for tests that expect errors
|
|
|
|
|
console.warn = vi.fn();
|
2025-05-30 10:57:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks();
|
2025-06-24 18:48:55 -04:00
|
|
|
vi.useRealTimers();
|
2025-05-30 10:57:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return the result on the first attempt if successful', async () => {
|
|
|
|
|
const mockFn = createFailingFunction(0);
|
|
|
|
|
const result = await retryWithBackoff(mockFn);
|
|
|
|
|
expect(result).toBe('success');
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should retry and succeed if failures are within maxAttempts', async () => {
|
|
|
|
|
const mockFn = createFailingFunction(2);
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 3,
|
|
|
|
|
initialDelayMs: 10,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await vi.runAllTimersAsync(); // Ensure all delays and retries complete
|
|
|
|
|
|
|
|
|
|
const result = await promise;
|
|
|
|
|
expect(result).toBe('success');
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error if all attempts fail', async () => {
|
|
|
|
|
const mockFn = createFailingFunction(3);
|
|
|
|
|
|
|
|
|
|
// 1. Start the retryable operation, which returns a promise.
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 3,
|
|
|
|
|
initialDelayMs: 10,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-03 09:08:27 -07:00
|
|
|
// 2. Run timers and await expectation in parallel.
|
|
|
|
|
await Promise.all([
|
|
|
|
|
expect(promise).rejects.toThrow('Simulated error attempt 3'),
|
|
|
|
|
vi.runAllTimersAsync(),
|
|
|
|
|
]);
|
2025-05-30 10:57:00 -07:00
|
|
|
|
2025-10-03 09:08:27 -07:00
|
|
|
// 3. Finally, assert the number of calls.
|
2025-05-30 10:57:00 -07:00
|
|
|
expect(mockFn).toHaveBeenCalledTimes(3);
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-13 16:26:51 -07:00
|
|
|
it('should default to 3 maxAttempts if no options are provided', async () => {
|
|
|
|
|
// This function will fail more than 3 times to ensure all retries are used.
|
2025-09-24 15:25:15 -07:00
|
|
|
const mockFn = createFailingFunction(10);
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn);
|
|
|
|
|
|
|
|
|
|
// Expect it to fail with the error from the 5th attempt.
|
2025-10-03 09:08:27 -07:00
|
|
|
await Promise.all([
|
2025-10-13 16:26:51 -07:00
|
|
|
expect(promise).rejects.toThrow('Simulated error attempt 3'),
|
2025-10-03 09:08:27 -07:00
|
|
|
vi.runAllTimersAsync(),
|
|
|
|
|
]);
|
2025-09-25 10:33:00 -07:00
|
|
|
|
2025-10-13 16:26:51 -07:00
|
|
|
expect(mockFn).toHaveBeenCalledTimes(3);
|
2025-09-25 10:33:00 -07:00
|
|
|
});
|
|
|
|
|
|
2025-10-13 16:26:51 -07:00
|
|
|
it('should default to 3 maxAttempts if options.maxAttempts is undefined', async () => {
|
|
|
|
|
// This function will fail more than 3 times to ensure all retries are used.
|
2025-09-25 10:33:00 -07:00
|
|
|
const mockFn = createFailingFunction(10);
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, { maxAttempts: undefined });
|
|
|
|
|
|
|
|
|
|
// Expect it to fail with the error from the 5th attempt.
|
2025-10-03 09:08:27 -07:00
|
|
|
await Promise.all([
|
2025-10-13 16:26:51 -07:00
|
|
|
expect(promise).rejects.toThrow('Simulated error attempt 3'),
|
2025-10-03 09:08:27 -07:00
|
|
|
vi.runAllTimersAsync(),
|
|
|
|
|
]);
|
2025-09-24 15:25:15 -07:00
|
|
|
|
2025-10-13 16:26:51 -07:00
|
|
|
expect(mockFn).toHaveBeenCalledTimes(3);
|
2025-09-24 15:25:15 -07:00
|
|
|
});
|
|
|
|
|
|
2025-05-30 10:57:00 -07:00
|
|
|
it('should not retry if shouldRetry returns false', async () => {
|
|
|
|
|
const mockFn = vi.fn(async () => {
|
|
|
|
|
throw new NonRetryableError('Non-retryable error');
|
|
|
|
|
});
|
2025-09-29 12:27:15 -07:00
|
|
|
const shouldRetryOnError = (error: Error) =>
|
|
|
|
|
!(error instanceof NonRetryableError);
|
2025-05-30 10:57:00 -07:00
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
2025-09-29 12:27:15 -07:00
|
|
|
shouldRetryOnError,
|
2025-05-30 10:57:00 -07:00
|
|
|
initialDelayMs: 10,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(promise).rejects.toThrow('Non-retryable error');
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-24 15:25:15 -07:00
|
|
|
it('should throw an error if maxAttempts is not a positive number', async () => {
|
|
|
|
|
const mockFn = createFailingFunction(1);
|
|
|
|
|
|
|
|
|
|
// Test with 0
|
|
|
|
|
await expect(retryWithBackoff(mockFn, { maxAttempts: 0 })).rejects.toThrow(
|
|
|
|
|
'maxAttempts must be a positive number.',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The function should not be called at all if validation fails
|
|
|
|
|
expect(mockFn).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-03 09:08:27 -07:00
|
|
|
it('should use default shouldRetry if not provided, retrying on ApiError 429', async () => {
|
|
|
|
|
const mockFn = vi.fn(async () => {
|
|
|
|
|
throw new ApiError({ message: 'Too Many Requests', status: 429 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 2,
|
|
|
|
|
initialDelayMs: 10,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
expect(promise).rejects.toThrow('Too Many Requests'),
|
|
|
|
|
vi.runAllTimersAsync(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should use default shouldRetry if not provided, not retrying on ApiError 400', async () => {
|
|
|
|
|
const mockFn = vi.fn(async () => {
|
|
|
|
|
throw new ApiError({ message: 'Bad Request', status: 400 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 2,
|
|
|
|
|
initialDelayMs: 10,
|
|
|
|
|
});
|
|
|
|
|
await expect(promise).rejects.toThrow('Bad Request');
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should use default shouldRetry if not provided, retrying on generic error with status 429', async () => {
|
2025-05-30 10:57:00 -07:00
|
|
|
const mockFn = vi.fn(async () => {
|
|
|
|
|
const error = new Error('Too Many Requests') as any;
|
|
|
|
|
error.status = 429;
|
|
|
|
|
throw error;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 2,
|
|
|
|
|
initialDelayMs: 10,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-03 09:08:27 -07:00
|
|
|
// Run timers and await expectation in parallel.
|
|
|
|
|
await Promise.all([
|
|
|
|
|
expect(promise).rejects.toThrow('Too Many Requests'),
|
|
|
|
|
vi.runAllTimersAsync(),
|
|
|
|
|
]);
|
2025-05-30 10:57:00 -07:00
|
|
|
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(2);
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-03 09:08:27 -07:00
|
|
|
it('should use default shouldRetry if not provided, not retrying on generic error with status 400', async () => {
|
2025-05-30 10:57:00 -07:00
|
|
|
const mockFn = vi.fn(async () => {
|
|
|
|
|
const error = new Error('Bad Request') as any;
|
|
|
|
|
error.status = 400;
|
|
|
|
|
throw error;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 2,
|
|
|
|
|
initialDelayMs: 10,
|
|
|
|
|
});
|
|
|
|
|
await expect(promise).rejects.toThrow('Bad Request');
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should respect maxDelayMs', async () => {
|
|
|
|
|
const mockFn = createFailingFunction(3);
|
|
|
|
|
const setTimeoutSpy = vi.spyOn(global, 'setTimeout');
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 4,
|
|
|
|
|
initialDelayMs: 100,
|
|
|
|
|
maxDelayMs: 250, // Max delay is less than 100 * 2 * 2 = 400
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await vi.advanceTimersByTimeAsync(1000); // Advance well past all delays
|
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
|
|
const delays = setTimeoutSpy.mock.calls.map((call) => call[1] as number);
|
|
|
|
|
|
|
|
|
|
// Delays should be around initial, initial*2, maxDelay (due to cap)
|
|
|
|
|
// Jitter makes exact assertion hard, so we check ranges / caps
|
|
|
|
|
expect(delays.length).toBe(3);
|
|
|
|
|
expect(delays[0]).toBeGreaterThanOrEqual(100 * 0.7);
|
|
|
|
|
expect(delays[0]).toBeLessThanOrEqual(100 * 1.3);
|
|
|
|
|
expect(delays[1]).toBeGreaterThanOrEqual(200 * 0.7);
|
|
|
|
|
expect(delays[1]).toBeLessThanOrEqual(200 * 1.3);
|
|
|
|
|
// The third delay should be capped by maxDelayMs (250ms), accounting for jitter
|
|
|
|
|
expect(delays[2]).toBeGreaterThanOrEqual(250 * 0.7);
|
|
|
|
|
expect(delays[2]).toBeLessThanOrEqual(250 * 1.3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle jitter correctly, ensuring varied delays', async () => {
|
|
|
|
|
let mockFn = createFailingFunction(5);
|
|
|
|
|
const setTimeoutSpy = vi.spyOn(global, 'setTimeout');
|
|
|
|
|
|
|
|
|
|
// Run retryWithBackoff multiple times to observe jitter
|
|
|
|
|
const runRetry = () =>
|
|
|
|
|
retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 2, // Only one retry, so one delay
|
|
|
|
|
initialDelayMs: 100,
|
|
|
|
|
maxDelayMs: 1000,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// We expect rejections as mockFn fails 5 times
|
|
|
|
|
const promise1 = runRetry();
|
2025-10-03 09:08:27 -07:00
|
|
|
// Run timers and await expectation in parallel.
|
|
|
|
|
await Promise.all([
|
|
|
|
|
expect(promise1).rejects.toThrow(),
|
|
|
|
|
vi.runAllTimersAsync(),
|
|
|
|
|
]);
|
2025-05-30 10:57:00 -07:00
|
|
|
|
|
|
|
|
const firstDelaySet = setTimeoutSpy.mock.calls.map(
|
|
|
|
|
(call) => call[1] as number,
|
|
|
|
|
);
|
|
|
|
|
setTimeoutSpy.mockClear(); // Clear calls for the next run
|
|
|
|
|
|
|
|
|
|
// Reset mockFn to reset its internal attempt counter for the next run
|
|
|
|
|
mockFn = createFailingFunction(5); // Re-initialize with 5 failures
|
|
|
|
|
|
|
|
|
|
const promise2 = runRetry();
|
2025-10-03 09:08:27 -07:00
|
|
|
// Run timers and await expectation in parallel.
|
|
|
|
|
await Promise.all([
|
|
|
|
|
expect(promise2).rejects.toThrow(),
|
|
|
|
|
vi.runAllTimersAsync(),
|
|
|
|
|
]);
|
2025-05-30 10:57:00 -07:00
|
|
|
|
|
|
|
|
const secondDelaySet = setTimeoutSpy.mock.calls.map(
|
|
|
|
|
(call) => call[1] as number,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check that the delays are not exactly the same due to jitter
|
|
|
|
|
// This is a probabilistic test, but with +/-30% jitter, it's highly likely they differ.
|
|
|
|
|
if (firstDelaySet.length > 0 && secondDelaySet.length > 0) {
|
|
|
|
|
// Check the first delay of each set
|
|
|
|
|
expect(firstDelaySet[0]).not.toBe(secondDelaySet[0]);
|
|
|
|
|
} else {
|
|
|
|
|
// If somehow no delays were captured (e.g. test setup issue), fail explicitly
|
|
|
|
|
throw new Error('Delays were not captured for jitter test');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure delays are within the expected jitter range [70, 130] for initialDelayMs = 100
|
|
|
|
|
[...firstDelaySet, ...secondDelaySet].forEach((d) => {
|
|
|
|
|
expect(d).toBeGreaterThanOrEqual(100 * 0.7);
|
|
|
|
|
expect(d).toBeLessThanOrEqual(100 * 1.3);
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-06-24 18:48:55 -04:00
|
|
|
|
|
|
|
|
describe('Flash model fallback for OAuth users', () => {
|
2025-10-03 13:24:50 -07:00
|
|
|
it('should trigger fallback for OAuth personal users on TerminalQuotaError', async () => {
|
2025-06-24 18:48:55 -04:00
|
|
|
const fallbackCallback = vi.fn().mockResolvedValue('gemini-2.5-flash');
|
|
|
|
|
|
|
|
|
|
let fallbackOccurred = false;
|
|
|
|
|
const mockFn = vi.fn().mockImplementation(async () => {
|
|
|
|
|
if (!fallbackOccurred) {
|
2025-10-03 13:24:50 -07:00
|
|
|
throw new TerminalQuotaError('Daily limit reached', {} as any);
|
2025-06-24 18:48:55 -04:00
|
|
|
}
|
|
|
|
|
return 'success';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 3,
|
|
|
|
|
initialDelayMs: 100,
|
2025-10-03 13:24:50 -07:00
|
|
|
onPersistent429: async (authType?: string, error?: unknown) => {
|
2025-06-24 18:48:55 -04:00
|
|
|
fallbackOccurred = true;
|
2025-10-03 13:24:50 -07:00
|
|
|
return await fallbackCallback(authType, error);
|
2025-06-24 18:48:55 -04:00
|
|
|
},
|
|
|
|
|
authType: 'oauth-personal',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await vi.runAllTimersAsync();
|
|
|
|
|
|
|
|
|
|
await expect(promise).resolves.toBe('success');
|
2025-07-09 10:18:15 -04:00
|
|
|
expect(fallbackCallback).toHaveBeenCalledWith(
|
|
|
|
|
'oauth-personal',
|
2025-10-03 13:24:50 -07:00
|
|
|
expect.any(TerminalQuotaError),
|
2025-07-09 10:18:15 -04:00
|
|
|
);
|
2025-10-03 13:24:50 -07:00
|
|
|
expect(mockFn).toHaveBeenCalledTimes(2);
|
2025-06-24 18:48:55 -04:00
|
|
|
});
|
|
|
|
|
|
2025-10-03 13:24:50 -07:00
|
|
|
it('should use retryDelayMs from RetryableQuotaError', async () => {
|
|
|
|
|
const setTimeoutSpy = vi.spyOn(global, 'setTimeout');
|
2025-06-24 18:48:55 -04:00
|
|
|
const mockFn = vi.fn().mockImplementation(async () => {
|
2025-10-03 13:24:50 -07:00
|
|
|
throw new RetryableQuotaError('Per-minute limit', {} as any, 12.345);
|
2025-06-24 18:48:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
2025-10-03 13:24:50 -07:00
|
|
|
maxAttempts: 2,
|
2025-06-24 18:48:55 -04:00
|
|
|
initialDelayMs: 100,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-03 13:24:50 -07:00
|
|
|
// Attach the rejection expectation *before* running timers
|
|
|
|
|
// eslint-disable-next-line vitest/valid-expect
|
|
|
|
|
const assertionPromise = expect(promise).rejects.toThrow();
|
2025-06-24 18:48:55 -04:00
|
|
|
await vi.runAllTimersAsync();
|
2025-10-03 13:24:50 -07:00
|
|
|
await assertionPromise;
|
2025-06-24 18:48:55 -04:00
|
|
|
|
2025-10-03 13:24:50 -07:00
|
|
|
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 12345);
|
2025-06-24 18:48:55 -04:00
|
|
|
});
|
2025-10-03 13:24:50 -07:00
|
|
|
|
|
|
|
|
it.each([[AuthType.USE_GEMINI], [AuthType.USE_VERTEX_AI], [undefined]])(
|
|
|
|
|
'should not trigger fallback for non-Google auth users (authType: %s) on TerminalQuotaError',
|
|
|
|
|
async (authType) => {
|
|
|
|
|
const fallbackCallback = vi.fn();
|
|
|
|
|
const mockFn = vi.fn().mockImplementation(async () => {
|
|
|
|
|
throw new TerminalQuotaError('Daily limit reached', {} as any);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promise = retryWithBackoff(mockFn, {
|
|
|
|
|
maxAttempts: 3,
|
|
|
|
|
onPersistent429: fallbackCallback,
|
|
|
|
|
authType,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(promise).rejects.toThrow('Daily limit reached');
|
|
|
|
|
expect(fallbackCallback).not.toHaveBeenCalled();
|
|
|
|
|
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-06-24 18:48:55 -04:00
|
|
|
});
|
2025-05-30 10:57:00 -07:00
|
|
|
});
|