| 1 | import { vi } from "vitest"; |
| 2 | |
| 3 | import { ExponentialBackoffOptions } from "./exponentialBackoff.js"; |
| 4 | |
| 5 | // Since the functions are not exported, we need to recreate them for testing |
| 6 | function isRetryableError(error: any): boolean { |
| 7 | // Handle null/undefined |
| 8 | if (!error) { |
| 9 | return false; |
| 10 | } |
| 11 | |
| 12 | // Network errors are retryable |
| 13 | if ( |
| 14 | error.code === "ECONNRESET" || |
| 15 | error.code === "ENOTFOUND" || |
| 16 | error.code === "ETIMEDOUT" || |
| 17 | error.code === "EPIPE" || |
| 18 | error.code === "ECONNREFUSED" |
| 19 | ) { |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | // HTTP status codes that are retryable |
| 24 | if (error.status) { |
| 25 | const status = error.status; |
| 26 | // 429 (Too Many Requests), 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout) |
| 27 | return status === 429 || status === 502 || status === 503 || status === 504; |
| 28 | } |
| 29 | |
| 30 | // OpenAI specific errors |
| 31 | if (error.type === "server_error" || error.type === "rate_limit_exceeded") { |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | // Anthropic specific errors |
| 36 | const lower = error.message?.toLowerCase(); |
| 37 | if (lower?.includes("overloaded")) { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | // Check for premature close errors by message content |
| 42 | if ( |
| 43 | lower?.includes("premature close") || |
| 44 | lower?.includes("premature end") || |
| 45 | lower?.includes("connection reset") || |
| 46 | lower?.includes("socket hang up") || |
| 47 | lower?.includes("aborted") |
| 48 | ) { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | function calculateDelay( |
| 56 | attempt: number, |
| 57 | options: Required<ExponentialBackoffOptions>, |
| 58 | ): number { |
| 59 | const baseDelay = |
| 60 | options.initialDelay * Math.pow(options.backoffMultiplier, attempt); |
| 61 | const cappedDelay = Math.min(baseDelay, options.maxDelay); |
| 62 | |
| 63 | if (options.jitter) { |
| 64 | // Add jitter: randomize between 50% and 100% of the calculated delay |
| 65 | return Math.floor(cappedDelay * (0.5 + Math.random() * 0.5)); |
| 66 | } |
| 67 | |
| 68 | return cappedDelay; |
| 69 | } |
| 70 | |
| 71 | describe("exponentialBackoff utilities", () => { |
| 72 | describe("isRetryableError", () => { |
| 73 | it("should return true for network connection errors", () => { |
| 74 | const error = { code: "ECONNRESET" }; |
| 75 | expect(isRetryableError(error)).toBe(true); |
| 76 | }); |
| 77 | |
| 78 | it("should return true for DNS resolution errors", () => { |
| 79 | const error = { code: "ENOTFOUND" }; |
| 80 | expect(isRetryableError(error)).toBe(true); |
| 81 | }); |
| 82 | |
| 83 | it("should return true for timeout errors", () => { |
| 84 | const error = { code: "ETIMEDOUT" }; |
| 85 | expect(isRetryableError(error)).toBe(true); |
| 86 | }); |
| 87 | |
| 88 | it("should return true for 429 Too Many Requests", () => { |
| 89 | const error = { status: 429 }; |
| 90 | expect(isRetryableError(error)).toBe(true); |
| 91 | }); |
| 92 | |
| 93 | it("should return true for 502 Bad Gateway", () => { |
| 94 | const error = { status: 502 }; |
| 95 | expect(isRetryableError(error)).toBe(true); |
| 96 | }); |
| 97 | |
| 98 | it("should return true for 503 Service Unavailable", () => { |
| 99 | const error = { status: 503 }; |
| 100 | expect(isRetryableError(error)).toBe(true); |
| 101 | }); |
| 102 | |
| 103 | it("should return true for 504 Gateway Timeout", () => { |
| 104 | const error = { status: 504 }; |
| 105 | expect(isRetryableError(error)).toBe(true); |
| 106 | }); |
| 107 | |
| 108 | it("should return true for server errors", () => { |
| 109 | const error = { type: "server_error" }; |
| 110 | expect(isRetryableError(error)).toBe(true); |
| 111 | }); |
| 112 | |
| 113 | it("should return true for rate limit errors", () => { |
| 114 | const error = { type: "rate_limit_exceeded" }; |
| 115 | expect(isRetryableError(error)).toBe(true); |
| 116 | }); |
| 117 | |
| 118 | it("should return true for EPIPE errors", () => { |
| 119 | const error = { code: "EPIPE" }; |
| 120 | expect(isRetryableError(error)).toBe(true); |
| 121 | }); |
| 122 | |
| 123 | it("should return true for ECONNREFUSED errors", () => { |
| 124 | const error = { code: "ECONNREFUSED" }; |
| 125 | expect(isRetryableError(error)).toBe(true); |
| 126 | }); |
| 127 | |
| 128 | it("should return true for Anthropic overloaded errors", () => { |
| 129 | const error = { message: "The service is overloaded" }; |
| 130 | expect(isRetryableError(error)).toBe(true); |
| 131 | }); |
| 132 | |
| 133 | it("should return true for premature close errors", () => { |
| 134 | const error = { message: "Error: Premature close" }; |
| 135 | expect(isRetryableError(error)).toBe(true); |
| 136 | }); |
| 137 | |
| 138 | it("should return true for premature end errors", () => { |
| 139 | const error = { message: "Premature end of response" }; |
| 140 | expect(isRetryableError(error)).toBe(true); |
| 141 | }); |
| 142 | |
| 143 | it("should return true for connection reset errors", () => { |
| 144 | const error = { message: "Connection reset by peer" }; |
| 145 | expect(isRetryableError(error)).toBe(true); |
| 146 | }); |
| 147 | |
| 148 | it("should return true for socket hang up errors", () => { |
| 149 | const error = { message: "socket hang up" }; |
| 150 | expect(isRetryableError(error)).toBe(true); |
| 151 | }); |
| 152 | |
| 153 | it("should return true for aborted connection errors", () => { |
| 154 | const error = { message: "Request aborted" }; |
| 155 | expect(isRetryableError(error)).toBe(true); |
| 156 | }); |
| 157 | |
| 158 | it("should handle case insensitive error messages", () => { |
| 159 | const error = { message: "PREMATURE CLOSE" }; |
| 160 | expect(isRetryableError(error)).toBe(true); |
| 161 | }); |
| 162 | |
| 163 | it("should return false for 400 Bad Request", () => { |
| 164 | const error = { status: 400 }; |
| 165 | expect(isRetryableError(error)).toBe(false); |
| 166 | }); |
| 167 | |
| 168 | it("should return false for 401 Unauthorized", () => { |
| 169 | const error = { status: 401 }; |
| 170 | expect(isRetryableError(error)).toBe(false); |
| 171 | }); |
| 172 | |
| 173 | it("should return false for 403 Forbidden", () => { |
| 174 | const error = { status: 403 }; |
| 175 | expect(isRetryableError(error)).toBe(false); |
| 176 | }); |
| 177 | |
| 178 | it("should return false for 404 Not Found", () => { |
| 179 | const error = { status: 404 }; |
| 180 | expect(isRetryableError(error)).toBe(false); |
| 181 | }); |
| 182 | |
| 183 | it("should return false for client errors", () => { |
| 184 | const error = { type: "client_error" }; |
| 185 | expect(isRetryableError(error)).toBe(false); |
| 186 | }); |
| 187 | |
| 188 | it("should return false for validation errors", () => { |
| 189 | const error = { type: "validation_error" }; |
| 190 | expect(isRetryableError(error)).toBe(false); |
| 191 | }); |
| 192 | |
| 193 | it("should return false for unknown errors", () => { |
| 194 | const error = { message: "Unknown error" }; |
| 195 | expect(isRetryableError(error)).toBe(false); |
| 196 | }); |
| 197 | |
| 198 | it("should return false for errors without relevant properties", () => { |
| 199 | const error = {}; |
| 200 | expect(isRetryableError(error)).toBe(false); |
| 201 | }); |
| 202 | |
| 203 | it("should return false for null error", () => { |
| 204 | expect(isRetryableError(null)).toBe(false); |
| 205 | }); |
| 206 | |
| 207 | it("should return false for undefined error", () => { |
| 208 | expect(isRetryableError(undefined)).toBe(false); |
| 209 | }); |
| 210 | |
| 211 | it("should return false for string errors", () => { |
| 212 | expect(isRetryableError("Error message")).toBe(false); |
| 213 | }); |
| 214 | |
| 215 | it("should handle errors with both code and status", () => { |
| 216 | const retryableError = { code: "ECONNRESET", status: 200 }; |
| 217 | expect(isRetryableError(retryableError)).toBe(true); |
| 218 | |
| 219 | const nonRetryableError = { code: "INVALID_REQUEST", status: 429 }; |
| 220 | expect(isRetryableError(nonRetryableError)).toBe(true); // Status takes precedence |
| 221 | }); |
| 222 | |
| 223 | it("should handle errors with both status and type", () => { |
| 224 | const error = { status: 429, type: "client_error" }; |
| 225 | expect(isRetryableError(error)).toBe(true); // Status check comes first |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | describe("calculateDelay", () => { |
| 230 | const defaultOptions: Required<ExponentialBackoffOptions> = { |
| 231 | maxRetries: 3, |
| 232 | initialDelay: 1000, |
| 233 | maxDelay: 30000, |
| 234 | backoffMultiplier: 2, |
| 235 | jitter: false, |
| 236 | hiddenRetries: 2, |
| 237 | }; |
| 238 | |
| 239 | it("should calculate exponential backoff without jitter", () => { |
| 240 | expect(calculateDelay(0, defaultOptions)).toBe(1000); |
| 241 | expect(calculateDelay(1, defaultOptions)).toBe(2000); |
| 242 | expect(calculateDelay(2, defaultOptions)).toBe(4000); |
| 243 | expect(calculateDelay(3, defaultOptions)).toBe(8000); |
| 244 | }); |
| 245 | |
| 246 | it("should respect maximum delay", () => { |
| 247 | const options = { ...defaultOptions, maxDelay: 5000 }; |
| 248 | expect(calculateDelay(0, options)).toBe(1000); |
| 249 | expect(calculateDelay(1, options)).toBe(2000); |
| 250 | expect(calculateDelay(2, options)).toBe(4000); |
| 251 | expect(calculateDelay(3, options)).toBe(5000); // Capped at maxDelay |
| 252 | expect(calculateDelay(4, options)).toBe(5000); // Still capped |
| 253 | }); |
| 254 | |
| 255 | it("should handle different backoff multipliers", () => { |
| 256 | const options = { ...defaultOptions, backoffMultiplier: 3 }; |
| 257 | expect(calculateDelay(0, options)).toBe(1000); |
| 258 | expect(calculateDelay(1, options)).toBe(3000); |
| 259 | expect(calculateDelay(2, options)).toBe(9000); |
| 260 | expect(calculateDelay(3, options)).toBe(27000); |
| 261 | }); |
| 262 | |
| 263 | it("should handle different initial delays", () => { |
| 264 | const options = { ...defaultOptions, initialDelay: 500 }; |
| 265 | expect(calculateDelay(0, options)).toBe(500); |
| 266 | expect(calculateDelay(1, options)).toBe(1000); |
| 267 | expect(calculateDelay(2, options)).toBe(2000); |
| 268 | }); |
| 269 | |
| 270 | it("should add jitter when enabled", () => { |
| 271 | const options = { ...defaultOptions, jitter: true }; |
| 272 | |
| 273 | // Mock Math.random to return 0.5 (middle of jitter range) |
| 274 | const originalRandom = Math.random; |
| 275 | vi.spyOn(Math, "random").mockReturnValue(0.5); |
| 276 | |
| 277 | // With jitter factor of 0.5 + 0.5 * 0.5 = 0.75 |
| 278 | expect(calculateDelay(0, options)).toBe(750); // 1000 * 0.75 |
| 279 | expect(calculateDelay(1, options)).toBe(1500); // 2000 * 0.75 |
| 280 | |
| 281 | // Restore Math.random |
| 282 | Math.random = originalRandom; |
| 283 | }); |
| 284 | |
| 285 | it("should handle minimum jitter", () => { |
| 286 | const options = { ...defaultOptions, jitter: true }; |
| 287 | |
| 288 | // Mock Math.random to return 0 (minimum jitter) |
| 289 | const originalRandom = Math.random; |
| 290 | Math.random = vi.fn(() => 0); |
| 291 | |
| 292 | // With jitter factor of 0.5 + 0.5 * 0 = 0.5 |
| 293 | expect(calculateDelay(0, options)).toBe(500); // 1000 * 0.5 |
| 294 | expect(calculateDelay(1, options)).toBe(1000); // 2000 * 0.5 |
| 295 | |
| 296 | // Restore Math.random |
| 297 | Math.random = originalRandom; |
| 298 | }); |
| 299 | |
| 300 | it("should handle maximum jitter", () => { |
| 301 | const options = { ...defaultOptions, jitter: true }; |
| 302 | |
| 303 | // Mock Math.random to return 1 (maximum jitter) |
| 304 | const originalRandom = Math.random; |
| 305 | Math.random = vi.fn(() => 1); |
| 306 | |
| 307 | // With jitter factor of 0.5 + 0.5 * 1 = 1.0 |
| 308 | expect(calculateDelay(0, options)).toBe(1000); // 1000 * 1.0 |
| 309 | expect(calculateDelay(1, options)).toBe(2000); // 2000 * 1.0 |
| 310 | |
| 311 | // Restore Math.random |
| 312 | Math.random = originalRandom; |
| 313 | }); |
| 314 | |
| 315 | it("should apply jitter after applying max delay cap", () => { |
| 316 | const options = { ...defaultOptions, maxDelay: 3000, jitter: true }; |
| 317 | |
| 318 | // Mock Math.random to return 0.5 |
| 319 | const originalRandom = Math.random; |
| 320 | Math.random = vi.fn(() => 0.5); |
| 321 | |
| 322 | // calculateDelay(2, options) would be 4000, but capped at 3000 |
| 323 | // Then jitter applied: 3000 * 0.75 = 2250 |
| 324 | expect(calculateDelay(2, options)).toBe(2250); |
| 325 | |
| 326 | // Restore Math.random |
| 327 | Math.random = originalRandom; |
| 328 | }); |
| 329 | |
| 330 | it("should handle zero attempt", () => { |
| 331 | expect(calculateDelay(0, defaultOptions)).toBe(1000); |
| 332 | }); |
| 333 | |
| 334 | it("should handle fractional multipliers", () => { |
| 335 | const options = { ...defaultOptions, backoffMultiplier: 1.5 }; |
| 336 | expect(calculateDelay(0, options)).toBe(1000); |
| 337 | expect(calculateDelay(1, options)).toBe(1500); |
| 338 | expect(calculateDelay(2, options)).toBe(2250); |
| 339 | }); |
| 340 | |
| 341 | it("should handle edge case with multiplier of 1", () => { |
| 342 | const options = { ...defaultOptions, backoffMultiplier: 1 }; |
| 343 | expect(calculateDelay(0, options)).toBe(1000); |
| 344 | expect(calculateDelay(1, options)).toBe(1000); |
| 345 | expect(calculateDelay(2, options)).toBe(1000); |
| 346 | }); |
| 347 | |
| 348 | it("should handle very large attempt numbers", () => { |
| 349 | const options = { ...defaultOptions, maxDelay: 60000 }; |
| 350 | expect(calculateDelay(10, options)).toBe(60000); // Should be capped |
| 351 | }); |
| 352 | }); |
| 353 | }); |