Skip to content

Commit d350b95

Browse files
chore(idempotency): expiration timestamp rounded to the nearest second (#2574)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent fe92fef commit d350b95

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

packages/idempotency/src/persistence/BasePersistenceLayer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
260260
private getExpiryTimestamp(): number {
261261
const currentTime: number = Date.now() / 1000;
262262

263-
return currentTime + this.expiresAfterSeconds;
263+
return Math.round(currentTime + this.expiresAfterSeconds);
264264
}
265265

266266
private getFromCache(idempotencyKey: string): IdempotencyRecord | undefined {

packages/idempotency/tests/e2e/makeIdempotent.test.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ describe(`Idempotency E2E tests, wrapper function usage`, () => {
137137
);
138138
// Since records 1 and 3 have the same payload, only 2 records should be created
139139
expect(idempotencyRecords?.Items?.length).toEqual(2);
140-
const idempotencyRecordsItems = idempotencyRecords.Items?.sort((a, b) =>
141-
a.expiration > b.expiration ? 1 : -1
142-
);
140+
const idempotencyRecordsItems = [
141+
idempotencyRecords.Items?.find(
142+
(record) => record.id === `${functionNameDefault}#${payloadHashes[0]}`
143+
),
144+
idempotencyRecords.Items?.find(
145+
(record) => record.id === `${functionNameDefault}#${payloadHashes[1]}`
146+
),
147+
];
143148

144149
expect(idempotencyRecordsItems?.[0]).toStrictEqual({
145150
id: `${functionNameDefault}#${payloadHashes[0]}`,
@@ -197,14 +202,27 @@ describe(`Idempotency E2E tests, wrapper function usage`, () => {
197202
);
198203
/**
199204
* Each record should have a corresponding entry in the persistence store,
200-
* if so then we sort the entries by expiry time and compare them to the
201-
* expected values. Expiry times should be in the same order as the
202-
* payload records.
205+
* if so then we retrieve the records based on their custom IDs
206+
* The records are retrieved in the same order as the payload records.
203207
*/
204208
expect(idempotencyRecords.Items?.length).toEqual(3);
205-
const idempotencyRecordsItems = idempotencyRecords.Items?.sort((a, b) =>
206-
a.expiryAttr > b.expiryAttr ? 1 : -1
207-
);
209+
const idempotencyRecordsItems = [
210+
idempotencyRecords.Items?.find(
211+
(record) =>
212+
record.customId ===
213+
`${functionNameCustomConfig}#${payloadHashes[0]}`
214+
),
215+
idempotencyRecords.Items?.find(
216+
(record) =>
217+
record.customId ===
218+
`${functionNameCustomConfig}#${payloadHashes[1]}`
219+
),
220+
idempotencyRecords.Items?.find(
221+
(record) =>
222+
record.customId ===
223+
`${functionNameCustomConfig}#${payloadHashes[2]}`
224+
),
225+
];
208226

209227
expect(idempotencyRecordsItems?.[0]).toStrictEqual({
210228
customId: `${functionNameCustomConfig}#${payloadHashes[0]}`,

packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ describe('Class: BasePersistenceLayer', () => {
373373
expect.objectContaining({
374374
idempotencyKey: 'my-lambda-function#mocked-hash',
375375
status: IdempotencyRecordStatus.INPROGRESS,
376-
expiryTimestamp: Date.now() / 1000 + 3600,
376+
expiryTimestamp: Math.round(Date.now() / 1000 + 3600),
377377
payloadHash: '',
378378
inProgressExpiryTimestamp: Date.now() + remainingTimeInMs,
379379
responseData: undefined,
@@ -443,7 +443,7 @@ describe('Class: BasePersistenceLayer', () => {
443443
expect.objectContaining({
444444
idempotencyKey: 'my-lambda-function#mocked-hash',
445445
status: IdempotencyRecordStatus.COMPLETED,
446-
expiryTimestamp: Date.now() / 1000 + 3600,
446+
expiryTimestamp: Math.round(Date.now() / 1000 + 3600),
447447
payloadHash: '',
448448
inProgressExpiryTimestamp: undefined,
449449
responseData: result,

0 commit comments

Comments
 (0)