|
| 1 | +import { Logger } from '@nestjs/common'; |
| 2 | +import { retryWithBackoff, RetryError } from './retry'; |
| 3 | + |
| 4 | +describe('retryWithBackoff', () => { |
| 5 | + let logger: any; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + logger = { debug: jest.fn() }; |
| 9 | + }); |
| 10 | + |
| 11 | + it('should return value on first attempt success', async () => { |
| 12 | + const fn = jest.fn().mockResolvedValue('success'); |
| 13 | + |
| 14 | + const result = await retryWithBackoff(fn, 3, 100, logger); |
| 15 | + |
| 16 | + expect(result).toBe('success'); |
| 17 | + expect(fn).toHaveBeenCalledTimes(1); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should retry and succeed on second attempt', async () => { |
| 21 | + const fn = jest |
| 22 | + .fn() |
| 23 | + .mockRejectedValueOnce(new Error('Network error')) |
| 24 | + .mockResolvedValueOnce('success'); |
| 25 | + |
| 26 | + const result = await retryWithBackoff(fn, 3, 100, logger); |
| 27 | + |
| 28 | + expect(result).toBe('success'); |
| 29 | + expect(fn).toHaveBeenCalledTimes(2); |
| 30 | + expect(logger.debug).toHaveBeenCalledTimes(1); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should exhausts all retries and throw last error', async () => { |
| 34 | + const error = new Error('Persistent error'); |
| 35 | + const fn = jest.fn().mockRejectedValue(error); |
| 36 | + |
| 37 | + await expect(retryWithBackoff(fn, 3, 100, logger)).rejects.toThrow( |
| 38 | + 'Persistent error', |
| 39 | + ); |
| 40 | + |
| 41 | + expect(fn).toHaveBeenCalledTimes(3); |
| 42 | + expect(logger.debug).toHaveBeenCalledTimes(2); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not retry on 400 client error', async () => { |
| 46 | + const error = new Error('Bad request'); |
| 47 | + (error as any).status = 400; |
| 48 | + const fn = jest.fn().mockRejectedValue(error); |
| 49 | + |
| 50 | + await expect(retryWithBackoff(fn, 3, 100, logger)).rejects.toThrow( |
| 51 | + 'Bad request', |
| 52 | + ); |
| 53 | + |
| 54 | + expect(fn).toHaveBeenCalledTimes(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should not retry on 403 forbidden error', async () => { |
| 58 | + const error = new Error('Forbidden'); |
| 59 | + (error as any).status = 403; |
| 60 | + const fn = jest.fn().mockRejectedValue(error); |
| 61 | + |
| 62 | + await expect(retryWithBackoff(fn, 3, 100, logger)).rejects.toThrow( |
| 63 | + 'Forbidden', |
| 64 | + ); |
| 65 | + |
| 66 | + expect(fn).toHaveBeenCalledTimes(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should retry on 500 server error', async () => { |
| 70 | + const error = new Error('Server error'); |
| 71 | + (error as any).status = 500; |
| 72 | + const fn = jest |
| 73 | + .fn() |
| 74 | + .mockRejectedValueOnce(error) |
| 75 | + .mockResolvedValueOnce('success'); |
| 76 | + |
| 77 | + const result = await retryWithBackoff(fn, 3, 100, logger); |
| 78 | + |
| 79 | + expect(result).toBe('success'); |
| 80 | + expect(fn).toHaveBeenCalledTimes(2); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should log retry attempts', async () => { |
| 84 | + const fn = jest |
| 85 | + .fn() |
| 86 | + .mockRejectedValueOnce(new Error('Error 1')) |
| 87 | + .mockResolvedValueOnce('success'); |
| 88 | + |
| 89 | + await retryWithBackoff(fn, 3, 100, logger); |
| 90 | + |
| 91 | + expect(logger.debug).toHaveBeenCalled(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments