|
| 1 | +/** |
| 2 | + * Test Idempotency Handler |
| 3 | + * |
| 4 | + * @group unit/idempotency/IdempotencyHandler |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + IdempotencyAlreadyInProgressError, IdempotencyInconsistentStateError, |
| 9 | + IdempotencyItemAlreadyExistsError, |
| 10 | + IdempotencyPersistenceLayerError |
| 11 | +} from '../../src/Exceptions'; |
| 12 | +import { IdempotencyOptions, IdempotencyRecordStatus } from '../../src/types'; |
| 13 | +import { BasePersistenceLayer, IdempotencyRecord } from '../../src/persistence'; |
| 14 | +import { IdempotencyHandler } from '../../src/IdempotencyHandler'; |
| 15 | + |
| 16 | +class PersistenceLayerTestClass extends BasePersistenceLayer { |
| 17 | + protected _deleteRecord = jest.fn(); |
| 18 | + protected _getRecord = jest.fn(); |
| 19 | + protected _putRecord = jest.fn(); |
| 20 | + protected _updateRecord = jest.fn(); |
| 21 | +} |
| 22 | + |
| 23 | +const mockFunctionToMakeIdempotent = jest.fn(); |
| 24 | +const mockFunctionPayloadToBeHashed = {}; |
| 25 | +const mockIdempotencyOptions: IdempotencyOptions = { |
| 26 | + persistenceStore: new PersistenceLayerTestClass(), |
| 27 | + dataKeywordArgument: 'testingKey' |
| 28 | +}; |
| 29 | +const mockFullFunctionPayload = {}; |
| 30 | + |
| 31 | +const idempotentHandler = new IdempotencyHandler( |
| 32 | + mockFunctionToMakeIdempotent, |
| 33 | + mockFunctionPayloadToBeHashed, |
| 34 | + mockIdempotencyOptions, |
| 35 | + mockFullFunctionPayload, |
| 36 | +); |
| 37 | + |
| 38 | +describe('Class IdempotencyHandler', () => { |
| 39 | + beforeEach(() => jest.resetAllMocks()); |
| 40 | + |
| 41 | + describe('Method: determineResultFromIdempotencyRecord', () => { |
| 42 | + test('when record is in progress and within expiry window, it rejects with IdempotencyAlreadyInProgressError', async () => { |
| 43 | + |
| 44 | + const stubRecord = new IdempotencyRecord({ |
| 45 | + idempotencyKey: 'idempotencyKey', |
| 46 | + expiryTimestamp: Date.now() + 1000, // should be in the future |
| 47 | + inProgressExpiryTimestamp: 0, // less than current time in milliseconds |
| 48 | + responseData: { responseData: 'responseData' }, |
| 49 | + payloadHash: 'payloadHash', |
| 50 | + status: IdempotencyRecordStatus.INPROGRESS |
| 51 | + }); |
| 52 | + |
| 53 | + expect(stubRecord.isExpired()).toBe(false); |
| 54 | + expect(stubRecord.getStatus()).toBe(IdempotencyRecordStatus.INPROGRESS); |
| 55 | + |
| 56 | + try { |
| 57 | + await idempotentHandler.determineResultFromIdempotencyRecord(stubRecord); |
| 58 | + } catch (e) { |
| 59 | + expect(e).toBeInstanceOf(IdempotencyAlreadyInProgressError); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + test('when record is in progress and outside expiry window, it rejects with IdempotencyInconsistentStateError', async () => { |
| 64 | + |
| 65 | + const stubRecord = new IdempotencyRecord({ |
| 66 | + idempotencyKey: 'idempotencyKey', |
| 67 | + expiryTimestamp: Date.now() + 1000, // should be in the future |
| 68 | + inProgressExpiryTimestamp: new Date().getUTCMilliseconds() - 1000, // should be in the past |
| 69 | + responseData: { responseData: 'responseData' }, |
| 70 | + payloadHash: 'payloadHash', |
| 71 | + status: IdempotencyRecordStatus.INPROGRESS |
| 72 | + }); |
| 73 | + |
| 74 | + expect(stubRecord.isExpired()).toBe(false); |
| 75 | + expect(stubRecord.getStatus()).toBe(IdempotencyRecordStatus.INPROGRESS); |
| 76 | + |
| 77 | + try { |
| 78 | + await idempotentHandler.determineResultFromIdempotencyRecord(stubRecord); |
| 79 | + } catch (e) { |
| 80 | + expect(e).toBeInstanceOf(IdempotencyInconsistentStateError); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + test('when record is expired, it rejects with IdempotencyInconsistentStateError', async () => { |
| 85 | + |
| 86 | + const stubRecord = new IdempotencyRecord({ |
| 87 | + idempotencyKey: 'idempotencyKey', |
| 88 | + expiryTimestamp: new Date().getUTCMilliseconds() - 1000, // should be in the past |
| 89 | + inProgressExpiryTimestamp: 0, // less than current time in milliseconds |
| 90 | + responseData: { responseData: 'responseData' }, |
| 91 | + payloadHash: 'payloadHash', |
| 92 | + status: IdempotencyRecordStatus.EXPIRED |
| 93 | + }); |
| 94 | + |
| 95 | + expect(stubRecord.isExpired()).toBe(true); |
| 96 | + expect(stubRecord.getStatus()).toBe(IdempotencyRecordStatus.EXPIRED); |
| 97 | + |
| 98 | + try { |
| 99 | + await idempotentHandler.determineResultFromIdempotencyRecord(stubRecord); |
| 100 | + } catch (e) { |
| 101 | + expect(e).toBeInstanceOf(IdempotencyInconsistentStateError); |
| 102 | + } |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('Method: handle', () => { |
| 107 | + |
| 108 | + afterAll(() => jest.restoreAllMocks()); // restore processIdempotency for other tests |
| 109 | + |
| 110 | + test('when IdempotencyAlreadyInProgressError is thrown, it retries two times', async () => { |
| 111 | + const mockProcessIdempotency = jest.spyOn(IdempotencyHandler.prototype, 'processIdempotency').mockRejectedValue(new IdempotencyAlreadyInProgressError('There is already an execution in progress')); |
| 112 | + await expect( |
| 113 | + idempotentHandler.handle() |
| 114 | + ).rejects.toThrow(IdempotencyAlreadyInProgressError); |
| 115 | + expect(mockProcessIdempotency).toHaveBeenCalledTimes(2); |
| 116 | + }); |
| 117 | + |
| 118 | + test('when non IdempotencyAlreadyInProgressError is thrown, it rejects', async () => { |
| 119 | + |
| 120 | + const mockProcessIdempotency = jest.spyOn(IdempotencyHandler.prototype, 'processIdempotency').mockRejectedValue(new Error('Some other error')); |
| 121 | + |
| 122 | + await expect( |
| 123 | + idempotentHandler.handle() |
| 124 | + ).rejects.toThrow(Error); |
| 125 | + expect(mockProcessIdempotency).toHaveBeenCalledTimes(1); |
| 126 | + }); |
| 127 | + |
| 128 | + }); |
| 129 | + |
| 130 | + describe('Method: processIdempotency', () => { |
| 131 | + |
| 132 | + test('when persistenceStore saves successfuly, it resolves', async () => { |
| 133 | + const mockSaveInProgress = jest.spyOn(mockIdempotencyOptions.persistenceStore, 'saveInProgress').mockResolvedValue(); |
| 134 | + |
| 135 | + mockFunctionToMakeIdempotent.mockImplementation(() => Promise.resolve('result')); |
| 136 | + |
| 137 | + await expect( |
| 138 | + idempotentHandler.processIdempotency() |
| 139 | + ).resolves.toBe('result'); |
| 140 | + expect(mockSaveInProgress).toHaveBeenCalledTimes(1); |
| 141 | + }); |
| 142 | + |
| 143 | + test('when persistences store throws any error, it wraps the error to IdempotencyPersistencesLayerError', async () => { |
| 144 | + const mockSaveInProgress = jest.spyOn(mockIdempotencyOptions.persistenceStore, 'saveInProgress').mockRejectedValue(new Error('Some error')); |
| 145 | + const mockDetermineResultFromIdempotencyRecord = jest.spyOn(IdempotencyHandler.prototype, 'determineResultFromIdempotencyRecord').mockResolvedValue('result'); |
| 146 | + |
| 147 | + await expect( |
| 148 | + idempotentHandler.processIdempotency() |
| 149 | + ).rejects.toThrow(IdempotencyPersistenceLayerError); |
| 150 | + expect(mockSaveInProgress).toHaveBeenCalledTimes(1); |
| 151 | + expect(mockDetermineResultFromIdempotencyRecord).toHaveBeenCalledTimes(0); |
| 152 | + }); |
| 153 | + |
| 154 | + test('when idempotency item already exists, it returns the existing record', async () => { |
| 155 | + const mockSaveInProgress = jest.spyOn(mockIdempotencyOptions.persistenceStore, 'saveInProgress').mockRejectedValue(new IdempotencyItemAlreadyExistsError('There is already an execution in progress')); |
| 156 | + |
| 157 | + const stubRecord = new IdempotencyRecord({ |
| 158 | + idempotencyKey: 'idempotencyKey', |
| 159 | + expiryTimestamp: 0, |
| 160 | + inProgressExpiryTimestamp: 0, |
| 161 | + responseData: { responseData: 'responseData' }, |
| 162 | + payloadHash: 'payloadHash', |
| 163 | + status: IdempotencyRecordStatus.INPROGRESS |
| 164 | + }); |
| 165 | + const mockGetRecord = jest.spyOn(mockIdempotencyOptions.persistenceStore, 'getRecord').mockImplementation(() => Promise.resolve(stubRecord)); |
| 166 | + const mockDetermineResultFromIdempotencyRecord = jest.spyOn(IdempotencyHandler.prototype, 'determineResultFromIdempotencyRecord').mockResolvedValue('result'); |
| 167 | + |
| 168 | + await expect( |
| 169 | + idempotentHandler.processIdempotency() |
| 170 | + ).resolves.toBe('result'); |
| 171 | + expect(mockSaveInProgress).toHaveBeenCalledTimes(1); |
| 172 | + expect(mockGetRecord).toHaveBeenCalledTimes(1); |
| 173 | + expect(mockDetermineResultFromIdempotencyRecord).toHaveBeenCalledTimes(1); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | +}); |
| 178 | + |
0 commit comments