|
| 1 | +import { PollingWithCockatielPolicy } from './polling-with-policy'; |
| 2 | +import { delay } from '../tests/utils'; |
| 3 | +import { HttpError } from '@metamask/controller-utils'; |
| 4 | + |
| 5 | +describe('PollingWithCockatielPolicy', () => { |
| 6 | + it('should return the success result', async () => { |
| 7 | + const policy = new PollingWithCockatielPolicy(); |
| 8 | + const result = await policy.start('test', async () => { |
| 9 | + return 'test'; |
| 10 | + }); |
| 11 | + expect(result).toBe('test'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should retry the request and complete successfully', async () => { |
| 15 | + const policy = new PollingWithCockatielPolicy(); |
| 16 | + let invocationCount = 0; |
| 17 | + const mockRequestFn = jest |
| 18 | + .fn() |
| 19 | + .mockImplementation(async (_abortSignal: AbortSignal) => { |
| 20 | + invocationCount += 1; |
| 21 | + return new Promise((resolve, reject) => { |
| 22 | + setTimeout(() => { |
| 23 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 24 | + if (invocationCount < 3) { |
| 25 | + reject(new HttpError(412)); |
| 26 | + } |
| 27 | + resolve('test'); |
| 28 | + }, 100); |
| 29 | + }); |
| 30 | + }); |
| 31 | + const result = await policy.start('test', mockRequestFn); |
| 32 | + expect(result).toBe('test'); |
| 33 | + expect(mockRequestFn).toHaveBeenCalledTimes(3); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should not retry when the error is not retryable', async () => { |
| 37 | + const policy = new PollingWithCockatielPolicy(); |
| 38 | + const mockRequestFn = jest |
| 39 | + .fn() |
| 40 | + .mockImplementation(async (_abortSignal: AbortSignal) => { |
| 41 | + return new Promise((_resolve, reject) => { |
| 42 | + reject(new HttpError(500, 'Internal server error')); |
| 43 | + }); |
| 44 | + }); |
| 45 | + await expect(policy.start('test', mockRequestFn)).rejects.toThrow( |
| 46 | + 'Internal server error', |
| 47 | + ); |
| 48 | + expect(mockRequestFn).toHaveBeenCalledTimes(1); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should throw an error when the retry exceeds the max retries', async () => { |
| 52 | + const policy = new PollingWithCockatielPolicy({ |
| 53 | + maxRetries: 3, |
| 54 | + }); |
| 55 | + |
| 56 | + const requestFn = jest |
| 57 | + .fn() |
| 58 | + .mockImplementation(async (_abortSignal: AbortSignal) => { |
| 59 | + return new Promise((_resolve, reject) => { |
| 60 | + setTimeout(() => { |
| 61 | + reject(new HttpError(412, 'Results are not available yet')); |
| 62 | + }, 100); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + const result = policy.start('test', requestFn); |
| 67 | + await expect(result).rejects.toThrow('Results are not available yet'); |
| 68 | + expect(requestFn).toHaveBeenCalledTimes(4); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should throw a `Request Cancelled` error when the request is aborted', async () => { |
| 72 | + const policy = new PollingWithCockatielPolicy({ |
| 73 | + maxRetries: 3, |
| 74 | + }); |
| 75 | + |
| 76 | + const requestFn = jest |
| 77 | + .fn() |
| 78 | + .mockImplementation(async (abortSignal: AbortSignal) => { |
| 79 | + return new Promise((_resolve, reject) => { |
| 80 | + setTimeout(() => { |
| 81 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 82 | + if (abortSignal.aborted) { |
| 83 | + reject(new Error('test error')); |
| 84 | + } |
| 85 | + reject(new Error('test error')); |
| 86 | + }, 100); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + const result = policy.start('test', requestFn); |
| 91 | + await delay(10); |
| 92 | + policy.abortPendingRequest('test'); |
| 93 | + await expect(result).rejects.toThrow('Request cancelled'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should throw a `Request Cancelled` error when a new request is started with the same request id', async () => { |
| 97 | + const policy = new PollingWithCockatielPolicy(); |
| 98 | + |
| 99 | + const requestFn = jest |
| 100 | + .fn() |
| 101 | + .mockImplementation(async (abortSignal: AbortSignal) => { |
| 102 | + return new Promise((resolve, reject) => { |
| 103 | + setTimeout(() => { |
| 104 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 105 | + if (abortSignal.aborted) { |
| 106 | + reject(new Error('test error')); |
| 107 | + } |
| 108 | + resolve('test'); |
| 109 | + }, 100); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + const result = policy.start('test', requestFn); |
| 114 | + await delay(10); |
| 115 | + const secondResult = policy.start('test', requestFn); |
| 116 | + await expect(result).rejects.toThrow('Request cancelled'); |
| 117 | + expect(await secondResult).toBe('test'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should resolve the result when two requests are started with the different request ids', async () => { |
| 121 | + const policy = new PollingWithCockatielPolicy(); |
| 122 | + |
| 123 | + const requestFn = (result: string) => |
| 124 | + jest.fn().mockImplementation(async (abortSignal: AbortSignal) => { |
| 125 | + return new Promise((resolve, reject) => { |
| 126 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 127 | + if (abortSignal.aborted) { |
| 128 | + reject(new Error('test error')); |
| 129 | + } |
| 130 | + setTimeout(() => { |
| 131 | + resolve(result); |
| 132 | + }, 100); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + const result = policy.start('test', requestFn('test')); |
| 137 | + const secondResult = policy.start('test2', requestFn('test2')); |
| 138 | + expect(await result).toBe('test'); |
| 139 | + expect(await secondResult).toBe('test2'); |
| 140 | + }); |
| 141 | +}); |
0 commit comments