Skip to content

Commit b908aa1

Browse files
dreamorosiam29d
andauthored
fix(idempotency): return correct value from in-memory cache (#2311)
* fix(idempotency): return correct value from in-memory cache * tests: add unit tests * tests: add integration tests * chore: rename variable clarify usage * chore: remove unused temp code --------- Co-authored-by: Alexander Schueren <[email protected]>
1 parent edd483f commit b908aa1

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

Diff for: packages/idempotency/src/persistence/BasePersistenceLayer.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,11 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
178178
);
179179
}
180180

181-
if (this.getFromCache(idempotencyRecord.idempotencyKey)) {
181+
const cachedRecord = this.getFromCache(idempotencyRecord.idempotencyKey);
182+
if (cachedRecord) {
182183
throw new IdempotencyItemAlreadyExistsError(
183184
`Failed to put record for already existing idempotency key: ${idempotencyRecord.idempotencyKey}`,
184-
idempotencyRecord
185+
cachedRecord
185186
);
186187
}
187188

Diff for: packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export const handlerLambda = makeIdempotent(
100100
persistenceStore: dynamoDBPersistenceLayer,
101101
config: new IdempotencyConfig({
102102
eventKeyJmesPath: 'foo',
103+
useLocalCache: true,
103104
}),
104105
}
105106
);

Diff for: packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts

+14-29
Original file line numberDiff line numberDiff line change
@@ -411,36 +411,20 @@ describe('Class: BasePersistenceLayer', () => {
411411
await persistenceLayer.saveSuccess({ foo: 'bar' }, { bar: 'baz' });
412412

413413
// Act & Assess
414-
await expect(
415-
persistenceLayer.saveInProgress({ foo: 'bar' })
416-
).rejects.toThrow(IdempotencyItemAlreadyExistsError);
417414
expect(putRecordSpy).toHaveBeenCalledTimes(0);
418-
});
419-
420-
test('when called and there is an in-progress record in the cache, it returns', async () => {
421-
// Prepare
422-
const persistenceLayer = new PersistenceLayerTestClass();
423-
persistenceLayer.configure({
424-
config: new IdempotencyConfig({
425-
useLocalCache: true,
426-
}),
427-
});
428-
jest.spyOn(persistenceLayer, '_getRecord').mockImplementationOnce(
429-
() =>
430-
new IdempotencyRecord({
431-
idempotencyKey: 'my-lambda-function#mocked-hash',
432-
status: IdempotencyRecordStatus.INPROGRESS,
433-
payloadHash: 'different-hash',
434-
expiryTimestamp: Date.now() / 1000 + 3600,
435-
inProgressExpiryTimestamp: Date.now() + 2000,
436-
})
437-
);
438-
await persistenceLayer.getRecord({ foo: 'bar' });
439-
440-
// Act & Assess
441-
await expect(
442-
persistenceLayer.saveInProgress({ foo: 'bar' })
443-
).resolves.toBeUndefined();
415+
try {
416+
await persistenceLayer.saveInProgress({ foo: 'bar' });
417+
} catch (error) {
418+
if (error instanceof IdempotencyItemAlreadyExistsError) {
419+
expect(error.existingRecord).toEqual(
420+
expect.objectContaining({
421+
idempotencyKey: 'my-lambda-function#mocked-hash',
422+
status: IdempotencyRecordStatus.COMPLETED,
423+
})
424+
);
425+
}
426+
}
427+
expect.assertions(2);
444428
});
445429
});
446430

@@ -500,6 +484,7 @@ describe('Class: BasePersistenceLayer', () => {
500484
persistenceLayer.configure({
501485
config: new IdempotencyConfig({
502486
payloadValidationJmesPath: 'foo',
487+
useLocalCache: true,
503488
}),
504489
});
505490
const existingRecord = new IdempotencyRecord({

0 commit comments

Comments
 (0)