From b1c31d1d7fac98b83477b98dc1739bb2c84734b0 Mon Sep 17 00:00:00 2001 From: arnabrahman Date: Wed, 22 May 2024 19:55:19 +0600 Subject: [PATCH 1/3] feat: round the getExpiryTimestamp return value --- packages/idempotency/src/persistence/BasePersistenceLayer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/idempotency/src/persistence/BasePersistenceLayer.ts b/packages/idempotency/src/persistence/BasePersistenceLayer.ts index 1456d70f12..7bf4034ff3 100644 --- a/packages/idempotency/src/persistence/BasePersistenceLayer.ts +++ b/packages/idempotency/src/persistence/BasePersistenceLayer.ts @@ -260,7 +260,7 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface { private getExpiryTimestamp(): number { const currentTime: number = Date.now() / 1000; - return currentTime + this.expiresAfterSeconds; + return Math.round(currentTime + this.expiresAfterSeconds); } private getFromCache(idempotencyKey: string): IdempotencyRecord | undefined { From b4b7f4581b4088b67a2466e904ec7782c01f3039 Mon Sep 17 00:00:00 2001 From: arnabrahman Date: Wed, 22 May 2024 19:56:39 +0600 Subject: [PATCH 2/3] fix: tests for rounding off expiryTimestamp --- .../tests/unit/persistence/BasePersistenceLayer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts index c5ee2e8491..76de4ff9e3 100644 --- a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts +++ b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts @@ -373,7 +373,7 @@ describe('Class: BasePersistenceLayer', () => { expect.objectContaining({ idempotencyKey: 'my-lambda-function#mocked-hash', status: IdempotencyRecordStatus.INPROGRESS, - expiryTimestamp: Date.now() / 1000 + 3600, + expiryTimestamp: Math.round(Date.now() / 1000 + 3600), payloadHash: '', inProgressExpiryTimestamp: Date.now() + remainingTimeInMs, responseData: undefined, @@ -443,7 +443,7 @@ describe('Class: BasePersistenceLayer', () => { expect.objectContaining({ idempotencyKey: 'my-lambda-function#mocked-hash', status: IdempotencyRecordStatus.COMPLETED, - expiryTimestamp: Date.now() / 1000 + 3600, + expiryTimestamp: Math.round(Date.now() / 1000 + 3600), payloadHash: '', inProgressExpiryTimestamp: undefined, responseData: result, From 2b08310cd95c382cf78c5af5b70c7736d7e6eef8 Mon Sep 17 00:00:00 2001 From: arnabrahman Date: Sat, 25 May 2024 11:17:48 +0600 Subject: [PATCH 3/3] fix: integration tests due to rounding off expiration --- .../tests/e2e/makeIdempotent.test.ts | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/idempotency/tests/e2e/makeIdempotent.test.ts b/packages/idempotency/tests/e2e/makeIdempotent.test.ts index f9d5e5bf6f..c28124d854 100644 --- a/packages/idempotency/tests/e2e/makeIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeIdempotent.test.ts @@ -137,9 +137,14 @@ describe(`Idempotency E2E tests, wrapper function usage`, () => { ); // Since records 1 and 3 have the same payload, only 2 records should be created expect(idempotencyRecords?.Items?.length).toEqual(2); - const idempotencyRecordsItems = idempotencyRecords.Items?.sort((a, b) => - a.expiration > b.expiration ? 1 : -1 - ); + const idempotencyRecordsItems = [ + idempotencyRecords.Items?.find( + (record) => record.id === `${functionNameDefault}#${payloadHashes[0]}` + ), + idempotencyRecords.Items?.find( + (record) => record.id === `${functionNameDefault}#${payloadHashes[1]}` + ), + ]; expect(idempotencyRecordsItems?.[0]).toStrictEqual({ id: `${functionNameDefault}#${payloadHashes[0]}`, @@ -197,14 +202,27 @@ describe(`Idempotency E2E tests, wrapper function usage`, () => { ); /** * Each record should have a corresponding entry in the persistence store, - * if so then we sort the entries by expiry time and compare them to the - * expected values. Expiry times should be in the same order as the - * payload records. + * if so then we retrieve the records based on their custom IDs + * The records are retrieved in the same order as the payload records. */ expect(idempotencyRecords.Items?.length).toEqual(3); - const idempotencyRecordsItems = idempotencyRecords.Items?.sort((a, b) => - a.expiryAttr > b.expiryAttr ? 1 : -1 - ); + const idempotencyRecordsItems = [ + idempotencyRecords.Items?.find( + (record) => + record.customId === + `${functionNameCustomConfig}#${payloadHashes[0]}` + ), + idempotencyRecords.Items?.find( + (record) => + record.customId === + `${functionNameCustomConfig}#${payloadHashes[1]}` + ), + idempotencyRecords.Items?.find( + (record) => + record.customId === + `${functionNameCustomConfig}#${payloadHashes[2]}` + ), + ]; expect(idempotencyRecordsItems?.[0]).toStrictEqual({ customId: `${functionNameCustomConfig}#${payloadHashes[0]}`,