|
| 1 | +import { jest, describe, it, expect, beforeEach } from '@jest/globals' |
| 2 | + |
| 3 | +import * as core from '../../__fixtures__/core.js' |
| 4 | + |
| 5 | +jest.unstable_mockModule('@actions/core', () => core) |
| 6 | + |
| 7 | +const { promiseRetry } = await import('../../src/internal/retry.js') |
| 8 | + |
| 9 | +describe('promiseRetry', () => { |
| 10 | + beforeEach(() => { |
| 11 | + jest.clearAllMocks() |
| 12 | + jest.useFakeTimers() |
| 13 | + }) |
| 14 | + |
| 15 | + it('resolves on first attempt if fn succeeds', async () => { |
| 16 | + const fn = jest.fn<() => Promise<string>>().mockResolvedValue('ok') |
| 17 | + const result = await promiseRetry(fn) |
| 18 | + expect(result).toBe('ok') |
| 19 | + expect(fn).toHaveBeenCalledTimes(1) |
| 20 | + }) |
| 21 | + |
| 22 | + it('retries on failure and resolves on subsequent success', async () => { |
| 23 | + const fn = jest |
| 24 | + .fn<() => Promise<string>>() |
| 25 | + .mockRejectedValueOnce(new Error('fail')) |
| 26 | + .mockResolvedValue('ok') |
| 27 | + |
| 28 | + const promise = promiseRetry(fn, { delay: 100 }) |
| 29 | + await jest.runAllTimersAsync() |
| 30 | + const result = await promise |
| 31 | + |
| 32 | + expect(result).toBe('ok') |
| 33 | + expect(fn).toHaveBeenCalledTimes(2) |
| 34 | + expect(core.info).toHaveBeenCalledWith('fail') |
| 35 | + }) |
| 36 | + |
| 37 | + it('throws after exhausting all retries', async () => { |
| 38 | + jest.useRealTimers() |
| 39 | + const error = new Error('always fails') |
| 40 | + const fn = jest.fn<() => Promise<string>>().mockRejectedValue(error) |
| 41 | + |
| 42 | + await expect(promiseRetry(fn, { maxRetries: 2, delay: 1 })).rejects.toThrow( |
| 43 | + 'always fails' |
| 44 | + ) |
| 45 | + expect(fn).toHaveBeenCalledTimes(3) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should call refreshCreds before retrying', async () => { |
| 49 | + const refreshCreds = jest |
| 50 | + .fn<() => Promise<void>>() |
| 51 | + .mockResolvedValue(undefined) |
| 52 | + const fn = jest |
| 53 | + .fn<() => Promise<string>>() |
| 54 | + .mockRejectedValueOnce(new Error('fail')) |
| 55 | + .mockResolvedValue('ok') |
| 56 | + |
| 57 | + const promise = promiseRetry(fn, { delay: 100, refreshCreds }) |
| 58 | + await jest.runAllTimersAsync() |
| 59 | + await promise |
| 60 | + |
| 61 | + expect(refreshCreds).toHaveBeenCalledTimes(1) |
| 62 | + }) |
| 63 | + |
| 64 | + it('stops retrying when shouldRetry returns false', async () => { |
| 65 | + const error = new Error('do not retry') |
| 66 | + const fn = jest.fn<() => Promise<string>>().mockRejectedValue(error) |
| 67 | + const shouldRetry = jest.fn<(e: Error) => boolean>().mockReturnValue(false) |
| 68 | + |
| 69 | + const promise = promiseRetry(fn, { maxRetries: 3, delay: 100, shouldRetry }) |
| 70 | + |
| 71 | + await expect(promise).rejects.toThrow('do not retry') |
| 72 | + expect(fn).toHaveBeenCalledTimes(1) |
| 73 | + expect(shouldRetry).toHaveBeenCalledWith(error) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should uses exponential backoff for delays', async () => { |
| 77 | + const fn = jest |
| 78 | + .fn<() => Promise<string>>() |
| 79 | + .mockRejectedValueOnce(new Error('1')) |
| 80 | + .mockRejectedValueOnce(new Error('2')) |
| 81 | + .mockResolvedValue('ok') |
| 82 | + |
| 83 | + const spy = jest.spyOn(global, 'setTimeout') |
| 84 | + |
| 85 | + const promise = promiseRetry(fn, { delay: 1000 }) |
| 86 | + await jest.runAllTimersAsync() |
| 87 | + await promise |
| 88 | + |
| 89 | + const delays = spy.mock.calls.map(c => c[1]).filter(d => d !== undefined) |
| 90 | + expect(delays).toContain(1000) |
| 91 | + expect(delays).toContain(4000) |
| 92 | + |
| 93 | + spy.mockRestore() |
| 94 | + }) |
| 95 | +}) |
0 commit comments