|
1 | | -import { describe, expect, it } from 'vitest'; |
2 | | -import { getWorkflowQueueName } from './helpers'; |
| 1 | +import { WorkflowAPIError } from '@workflow/errors'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { |
| 4 | + getWorkflowQueueName, |
| 5 | + withServerErrorRetry, |
| 6 | + withThrottleRetry, |
| 7 | +} from './helpers.js'; |
| 8 | + |
| 9 | +// Mock the logger to suppress output during tests |
| 10 | +vi.mock('../logger.js', () => ({ |
| 11 | + runtimeLogger: { |
| 12 | + warn: vi.fn(), |
| 13 | + debug: vi.fn(), |
| 14 | + info: vi.fn(), |
| 15 | + error: vi.fn(), |
| 16 | + }, |
| 17 | +})); |
3 | 18 |
|
4 | 19 | describe('getWorkflowQueueName', () => { |
5 | 20 | it('should return a valid queue name for a simple workflow name', () => { |
@@ -70,3 +85,227 @@ describe('getWorkflowQueueName', () => { |
70 | 85 | expect(() => getWorkflowQueueName('')).toThrow('Invalid workflow name'); |
71 | 86 | }); |
72 | 87 | }); |
| 88 | + |
| 89 | +describe('withServerErrorRetry', () => { |
| 90 | + beforeEach(() => { |
| 91 | + vi.useFakeTimers(); |
| 92 | + }); |
| 93 | + |
| 94 | + afterEach(() => { |
| 95 | + vi.useRealTimers(); |
| 96 | + vi.clearAllMocks(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should return the result on success', async () => { |
| 100 | + const fn = vi.fn().mockResolvedValue('ok'); |
| 101 | + const result = await withServerErrorRetry(fn); |
| 102 | + expect(result).toBe('ok'); |
| 103 | + expect(fn).toHaveBeenCalledTimes(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should retry on 5xx WorkflowAPIError and succeed', async () => { |
| 107 | + const fn = vi |
| 108 | + .fn() |
| 109 | + .mockRejectedValueOnce( |
| 110 | + new WorkflowAPIError('Internal Server Error', { status: 500 }) |
| 111 | + ) |
| 112 | + .mockResolvedValueOnce('recovered'); |
| 113 | + |
| 114 | + const promise = withServerErrorRetry(fn); |
| 115 | + await vi.advanceTimersByTimeAsync(500); |
| 116 | + const result = await promise; |
| 117 | + |
| 118 | + expect(result).toBe('recovered'); |
| 119 | + expect(fn).toHaveBeenCalledTimes(2); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should retry up to 3 times with exponential backoff', async () => { |
| 123 | + const fn = vi |
| 124 | + .fn() |
| 125 | + .mockRejectedValueOnce(new WorkflowAPIError('error', { status: 502 })) |
| 126 | + .mockRejectedValueOnce(new WorkflowAPIError('error', { status: 503 })) |
| 127 | + .mockRejectedValueOnce(new WorkflowAPIError('error', { status: 500 })) |
| 128 | + .mockResolvedValueOnce('finally'); |
| 129 | + |
| 130 | + const promise = withServerErrorRetry(fn); |
| 131 | + |
| 132 | + // First retry after 500ms |
| 133 | + await vi.advanceTimersByTimeAsync(500); |
| 134 | + // Second retry after 1000ms |
| 135 | + await vi.advanceTimersByTimeAsync(1000); |
| 136 | + // Third retry after 2000ms |
| 137 | + await vi.advanceTimersByTimeAsync(2000); |
| 138 | + |
| 139 | + const result = await promise; |
| 140 | + expect(result).toBe('finally'); |
| 141 | + expect(fn).toHaveBeenCalledTimes(4); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should throw after exhausting all retries', async () => { |
| 145 | + const error = new WorkflowAPIError('server down', { status: 500 }); |
| 146 | + const fn = vi.fn().mockRejectedValue(error); |
| 147 | + |
| 148 | + const promise = withServerErrorRetry(fn).catch((e) => e); |
| 149 | + |
| 150 | + // Advance through all 3 retry delays |
| 151 | + await vi.advanceTimersByTimeAsync(500); |
| 152 | + await vi.advanceTimersByTimeAsync(1000); |
| 153 | + await vi.advanceTimersByTimeAsync(2000); |
| 154 | + |
| 155 | + const result = await promise; |
| 156 | + expect(result).toBeInstanceOf(WorkflowAPIError); |
| 157 | + expect(result.message).toBe('server down'); |
| 158 | + // 1 initial + 3 retries = 4 total calls |
| 159 | + expect(fn).toHaveBeenCalledTimes(4); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should not retry non-5xx WorkflowAPIErrors', async () => { |
| 163 | + const error = new WorkflowAPIError('Not Found', { status: 404 }); |
| 164 | + const fn = vi.fn().mockRejectedValue(error); |
| 165 | + |
| 166 | + await expect(withServerErrorRetry(fn)).rejects.toThrow('Not Found'); |
| 167 | + expect(fn).toHaveBeenCalledTimes(1); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should not retry non-WorkflowAPIError errors', async () => { |
| 171 | + const error = new Error('some other error'); |
| 172 | + const fn = vi.fn().mockRejectedValue(error); |
| 173 | + |
| 174 | + await expect(withServerErrorRetry(fn)).rejects.toThrow('some other error'); |
| 175 | + expect(fn).toHaveBeenCalledTimes(1); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should not retry 429 errors (handled by withThrottleRetry)', async () => { |
| 179 | + const error = new WorkflowAPIError('Too Many Requests', { |
| 180 | + status: 429, |
| 181 | + retryAfter: 5, |
| 182 | + }); |
| 183 | + const fn = vi.fn().mockRejectedValue(error); |
| 184 | + |
| 185 | + await expect(withServerErrorRetry(fn)).rejects.toThrow('Too Many Requests'); |
| 186 | + expect(fn).toHaveBeenCalledTimes(1); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +describe('withThrottleRetry', () => { |
| 191 | + beforeEach(() => { |
| 192 | + vi.useFakeTimers(); |
| 193 | + }); |
| 194 | + |
| 195 | + afterEach(() => { |
| 196 | + vi.useRealTimers(); |
| 197 | + vi.clearAllMocks(); |
| 198 | + }); |
| 199 | + |
| 200 | + it('should pass through the result on success', async () => { |
| 201 | + const fn = vi.fn().mockResolvedValue(undefined); |
| 202 | + const result = await withThrottleRetry(fn); |
| 203 | + expect(result).toBeUndefined(); |
| 204 | + expect(fn).toHaveBeenCalledTimes(1); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should pass through { timeoutSeconds } returned by fn', async () => { |
| 208 | + const fn = vi.fn().mockResolvedValue({ timeoutSeconds: 42 }); |
| 209 | + const result = await withThrottleRetry(fn); |
| 210 | + expect(result).toEqual({ timeoutSeconds: 42 }); |
| 211 | + expect(fn).toHaveBeenCalledTimes(1); |
| 212 | + }); |
| 213 | + |
| 214 | + it('should re-throw non-429 errors including 5xx', async () => { |
| 215 | + const error = new WorkflowAPIError('Internal Server Error', { |
| 216 | + status: 500, |
| 217 | + }); |
| 218 | + const fn = vi.fn().mockRejectedValue(error); |
| 219 | + |
| 220 | + await expect(withThrottleRetry(fn)).rejects.toThrow( |
| 221 | + 'Internal Server Error' |
| 222 | + ); |
| 223 | + expect(fn).toHaveBeenCalledTimes(1); |
| 224 | + }); |
| 225 | + |
| 226 | + it('should re-throw non-WorkflowAPIError errors', async () => { |
| 227 | + const error = new Error('random failure'); |
| 228 | + const fn = vi.fn().mockRejectedValue(error); |
| 229 | + |
| 230 | + await expect(withThrottleRetry(fn)).rejects.toThrow('random failure'); |
| 231 | + expect(fn).toHaveBeenCalledTimes(1); |
| 232 | + }); |
| 233 | + |
| 234 | + it('should wait in-process and retry once for short retryAfter (<10s)', async () => { |
| 235 | + const fn = vi |
| 236 | + .fn() |
| 237 | + .mockRejectedValueOnce( |
| 238 | + new WorkflowAPIError('Throttled', { status: 429, retryAfter: 5 }) |
| 239 | + ) |
| 240 | + .mockResolvedValueOnce(undefined); |
| 241 | + |
| 242 | + const promise = withThrottleRetry(fn); |
| 243 | + // Advance past the 5s wait |
| 244 | + await vi.advanceTimersByTimeAsync(5000); |
| 245 | + const result = await promise; |
| 246 | + |
| 247 | + expect(result).toBeUndefined(); |
| 248 | + expect(fn).toHaveBeenCalledTimes(2); |
| 249 | + }); |
| 250 | + |
| 251 | + it('should defer to queue when both attempts are throttled (double 429)', async () => { |
| 252 | + const fn = vi |
| 253 | + .fn() |
| 254 | + .mockRejectedValueOnce( |
| 255 | + new WorkflowAPIError('Throttled', { status: 429, retryAfter: 3 }) |
| 256 | + ) |
| 257 | + .mockRejectedValueOnce( |
| 258 | + new WorkflowAPIError('Throttled again', { status: 429, retryAfter: 7 }) |
| 259 | + ); |
| 260 | + |
| 261 | + const promise = withThrottleRetry(fn); |
| 262 | + // Advance past the 3s in-process wait |
| 263 | + await vi.advanceTimersByTimeAsync(3000); |
| 264 | + const result = await promise; |
| 265 | + |
| 266 | + expect(result).toEqual({ timeoutSeconds: 7 }); |
| 267 | + expect(fn).toHaveBeenCalledTimes(2); |
| 268 | + }); |
| 269 | + |
| 270 | + it('should re-throw non-429 error on retry failure', async () => { |
| 271 | + const fn = vi |
| 272 | + .fn() |
| 273 | + .mockRejectedValueOnce( |
| 274 | + new WorkflowAPIError('Throttled', { status: 429, retryAfter: 2 }) |
| 275 | + ) |
| 276 | + .mockRejectedValueOnce(new Error('connection lost')); |
| 277 | + |
| 278 | + // Capture the rejection early to prevent unhandled rejection warning |
| 279 | + const promise = withThrottleRetry(fn).catch((e) => e); |
| 280 | + await vi.advanceTimersByTimeAsync(2000); |
| 281 | + |
| 282 | + const result = await promise; |
| 283 | + expect(result).toBeInstanceOf(Error); |
| 284 | + expect(result.message).toBe('connection lost'); |
| 285 | + expect(fn).toHaveBeenCalledTimes(2); |
| 286 | + }); |
| 287 | + |
| 288 | + it('should defer to queue immediately for long retryAfter (>=10s)', async () => { |
| 289 | + const fn = vi |
| 290 | + .fn() |
| 291 | + .mockRejectedValueOnce( |
| 292 | + new WorkflowAPIError('Throttled', { status: 429, retryAfter: 15 }) |
| 293 | + ); |
| 294 | + |
| 295 | + const result = await withThrottleRetry(fn); |
| 296 | + |
| 297 | + expect(result).toEqual({ timeoutSeconds: 15 }); |
| 298 | + expect(fn).toHaveBeenCalledTimes(1); |
| 299 | + }); |
| 300 | + |
| 301 | + it('should default to 30s (defer to queue) when no retryAfter is provided', async () => { |
| 302 | + const error = new WorkflowAPIError('Throttled', { status: 429 }); |
| 303 | + // retryAfter is undefined, so it defaults to 30 (>=10 → defer) |
| 304 | + const fn = vi.fn().mockRejectedValue(error); |
| 305 | + |
| 306 | + const result = await withThrottleRetry(fn); |
| 307 | + |
| 308 | + expect(result).toEqual({ timeoutSeconds: 30 }); |
| 309 | + expect(fn).toHaveBeenCalledTimes(1); |
| 310 | + }); |
| 311 | +}); |
0 commit comments