Skip to content

Commit

Permalink
fix(idempotency): return correct value from in-memory cache (#2311)
Browse files Browse the repository at this point in the history
* 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 <sha@amazon.com>
  • Loading branch information
dreamorosi and am29d committed Apr 5, 2024
1 parent edd483f commit b908aa1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
5 changes: 3 additions & 2 deletions packages/idempotency/src/persistence/BasePersistenceLayer.ts
Expand Up @@ -178,10 +178,11 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
);
}

if (this.getFromCache(idempotencyRecord.idempotencyKey)) {
const cachedRecord = this.getFromCache(idempotencyRecord.idempotencyKey);
if (cachedRecord) {
throw new IdempotencyItemAlreadyExistsError(
`Failed to put record for already existing idempotency key: ${idempotencyRecord.idempotencyKey}`,
idempotencyRecord
cachedRecord
);
}

Expand Down
Expand Up @@ -100,6 +100,7 @@ export const handlerLambda = makeIdempotent(
persistenceStore: dynamoDBPersistenceLayer,
config: new IdempotencyConfig({
eventKeyJmesPath: 'foo',
useLocalCache: true,
}),
}
);
Expand Up @@ -411,36 +411,20 @@ describe('Class: BasePersistenceLayer', () => {
await persistenceLayer.saveSuccess({ foo: 'bar' }, { bar: 'baz' });

// Act & Assess
await expect(
persistenceLayer.saveInProgress({ foo: 'bar' })
).rejects.toThrow(IdempotencyItemAlreadyExistsError);
expect(putRecordSpy).toHaveBeenCalledTimes(0);
});

test('when called and there is an in-progress record in the cache, it returns', async () => {
// Prepare
const persistenceLayer = new PersistenceLayerTestClass();
persistenceLayer.configure({
config: new IdempotencyConfig({
useLocalCache: true,
}),
});
jest.spyOn(persistenceLayer, '_getRecord').mockImplementationOnce(
() =>
new IdempotencyRecord({
idempotencyKey: 'my-lambda-function#mocked-hash',
status: IdempotencyRecordStatus.INPROGRESS,
payloadHash: 'different-hash',
expiryTimestamp: Date.now() / 1000 + 3600,
inProgressExpiryTimestamp: Date.now() + 2000,
})
);
await persistenceLayer.getRecord({ foo: 'bar' });

// Act & Assess
await expect(
persistenceLayer.saveInProgress({ foo: 'bar' })
).resolves.toBeUndefined();
try {
await persistenceLayer.saveInProgress({ foo: 'bar' });
} catch (error) {
if (error instanceof IdempotencyItemAlreadyExistsError) {
expect(error.existingRecord).toEqual(
expect.objectContaining({
idempotencyKey: 'my-lambda-function#mocked-hash',
status: IdempotencyRecordStatus.COMPLETED,
})
);
}
}
expect.assertions(2);
});
});

Expand Down Expand Up @@ -500,6 +484,7 @@ describe('Class: BasePersistenceLayer', () => {
persistenceLayer.configure({
config: new IdempotencyConfig({
payloadValidationJmesPath: 'foo',
useLocalCache: true,
}),
});
const existingRecord = new IdempotencyRecord({
Expand Down

0 comments on commit b908aa1

Please sign in to comment.