Skip to content

chore(idempotency): expiration timestamp rounded to the nearest second #2574

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 4 commits into from
May 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 27 additions & 9 deletions packages/idempotency/tests/e2e/makeIdempotent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`,
Expand Down Expand Up @@ -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]}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down