Skip to content

fix(idempotency): return correct value from in-memory cache #2311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/idempotency/src/persistence/BasePersistenceLayer.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const handlerLambda = makeIdempotent(
persistenceStore: dynamoDBPersistenceLayer,
config: new IdempotencyConfig({
eventKeyJmesPath: 'foo',
useLocalCache: true,
}),
}
);
Original file line number Diff line number Diff line change
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