Skip to content

Commit d1dad17

Browse files
vgphoenixcamposjeffrey-baker-vg
authored andcommitted
feat: change how time is measured to seconds
1 parent b4165bd commit d1dad17

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

Diff for: packages/idempotency/src/persistence/DynamoDbPersistenceLayer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DynamoDBPersistenceLayer extends PersistenceLayer {
4747
const notInProgress = 'NOT #status = :inprogress';
4848
const conditionalExpression = `${idempotencyKeyDoesNotExist} OR ${idempotencyKeyExpired} OR ${notInProgress}`;
4949
try {
50-
await table.put({ TableName: this.tableName, Item: item, ExpressionAttributeNames: { '#id': this.key_attr, '#expiry': this.expiry_attr, '#status': this.status_attr }, ExpressionAttributeValues: { ':now': Date.now(), ':inprogress': IdempotencyRecordStatus.INPROGRESS }, ConditionExpression: conditionalExpression });
50+
await table.put({ TableName: this.tableName, Item: item, ExpressionAttributeNames: { '#id': this.key_attr, '#expiry': this.expiry_attr, '#status': this.status_attr }, ExpressionAttributeValues: { ':now': Date.now() / 1000, ':inprogress': IdempotencyRecordStatus.INPROGRESS }, ConditionExpression: conditionalExpression });
5151
} catch (e){
5252
if ((e as DynamoDBServiceException).name === 'ConditionalCheckFailedException'){
5353
throw new IdempotencyItemAlreadyExistsError();

Diff for: packages/idempotency/src/persistence/IdempotencyRecord.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class IdempotencyRecord {
2525
}
2626

2727
private isExpired(): boolean {
28-
return this.expiryTimestamp !== undefined && (Date.now() > this.expiryTimestamp);
28+
return this.expiryTimestamp !== undefined && ((Date.now() / 1000) > this.expiryTimestamp);
2929
}
3030
}
3131

Diff for: packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ describe('Class: DynamoDbPersistenceLayer', () => {
3636
});
3737

3838
describe('Method: _putRecord', () => {
39+
const currentDateInMilliseconds = 1000;
40+
const currentDateInSeconds = 1;
41+
42+
beforeEach(() => {
43+
jest.spyOn(Date, 'now').mockReturnValue(currentDateInMilliseconds);
44+
});
45+
3946
test('when called with a record that succeeds condition, it puts record in dynamo table', async () => {
4047
// Prepare
4148
const tableName = 'tableName';
@@ -46,10 +53,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
4653
const expiryTimestamp = 0;
4754
const inProgressExpiryTimestamp = 0;
4855
const record = new IdempotencyRecord(key, status, expiryTimestamp, inProgressExpiryTimestamp, undefined, undefined);
49-
50-
const currentDate = 1;
51-
jest.spyOn(Date, 'now').mockReturnValue(currentDate);
52-
5356
const dynamoClient = mockClient(DynamoDBDocument).on(PutCommand).resolves({});
5457

5558
// Act
@@ -60,7 +63,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
6063
TableName: tableName,
6164
Item: { 'id': key, 'expiration': expiryTimestamp, status: status },
6265
ExpressionAttributeNames: { '#id': 'id', '#expiry': 'expiration', '#status': 'status' },
63-
ExpressionAttributeValues: { ':now': currentDate, ':inprogress': IdempotencyRecordStatus.INPROGRESS },
66+
ExpressionAttributeValues: { ':now': currentDateInSeconds, ':inprogress': IdempotencyRecordStatus.INPROGRESS },
6467
ConditionExpression: 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
6568
});
6669
});
@@ -76,9 +79,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
7679
const inProgressExpiryTimestamp = 0;
7780
const record = new IdempotencyRecord(key, status, expiryTimestamp, inProgressExpiryTimestamp, undefined, undefined);
7881

79-
const currentDate = 1;
80-
jest.spyOn(Date, 'now').mockReturnValue(currentDate);
81-
8282
const dynamoClient = mockClient(DynamoDBDocument).on(PutCommand).rejects({ name: 'ConditionalCheckFailedException' });
8383

8484
// Act
@@ -94,7 +94,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
9494
TableName: tableName,
9595
Item: { 'id': key, 'expiration': expiryTimestamp, status: status },
9696
ExpressionAttributeNames: { '#id': 'id', '#expiry': 'expiration', '#status': 'status' },
97-
ExpressionAttributeValues: { ':now': currentDate, ':inprogress': IdempotencyRecordStatus.INPROGRESS },
97+
ExpressionAttributeValues: { ':now': currentDateInSeconds, ':inprogress': IdempotencyRecordStatus.INPROGRESS },
9898
ConditionExpression: 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
9999
});
100100
expect(error).toBeInstanceOf(IdempotencyItemAlreadyExistsError);
@@ -111,9 +111,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
111111
const inProgressExpiryTimestamp = 0;
112112
const record = new IdempotencyRecord(key, status, expiryTimestamp, inProgressExpiryTimestamp, undefined, undefined);
113113

114-
const currentDate = 1;
115-
jest.spyOn(Date, 'now').mockReturnValue(currentDate);
116-
117114
const dynamoClient = mockClient(DynamoDBDocument).on(PutCommand).rejects(new Error());
118115

119116
// Act
@@ -129,7 +126,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
129126
TableName: tableName,
130127
Item: { 'id': key, 'expiration': expiryTimestamp, status: status },
131128
ExpressionAttributeNames: { '#id': 'id', '#expiry': 'expiration', '#status': 'status' },
132-
ExpressionAttributeValues: { ':now': currentDate, ':inprogress': IdempotencyRecordStatus.INPROGRESS },
129+
ExpressionAttributeValues: { ':now': currentDateInSeconds, ':inprogress': IdempotencyRecordStatus.INPROGRESS },
133130
ConditionExpression: 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
134131
});
135132
expect(error).toBe(error);

Diff for: packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const mockPayloadHash = '123';
1414
describe('Given an idempotency record that is expired', () => {
1515
let idempotencyRecord: IdempotencyRecord;
1616
beforeEach(() => {
17-
const mockNowAfterExiryTime = 1487076708000;
18-
const expiryTimeBeforeNow = 1487076707000;
19-
Date.now = jest.fn(() => mockNowAfterExiryTime);
17+
const mockNowAfterExpiryTime = 1487076708000;
18+
const expiryTimeBeforeNow = 1487076707;
19+
Date.now = jest.fn(() => mockNowAfterExpiryTime);
2020
idempotencyRecord = new IdempotencyRecord(mockIdempotencyKey, IdempotencyRecordStatus.INPROGRESS, expiryTimeBeforeNow, mockInProgressExpiry, mockData, mockPayloadHash);
2121
});
2222
describe('When checking the status of the idempotency record', () => {
@@ -35,7 +35,7 @@ describe('Given an idempotency record that is not expired', () => {
3535
let idempotencyRecord: IdempotencyRecord;
3636
beforeEach(() => {
3737
const mockNowBeforeExiryTime = 1487076707000;
38-
const expiryTimeAfterNow = 1487076708000;
38+
const expiryTimeAfterNow = 1487076708;
3939
Date.now = jest.fn(() => mockNowBeforeExiryTime);
4040
idempotencyRecord = new IdempotencyRecord(mockIdempotencyKey, IdempotencyRecordStatus.INPROGRESS, expiryTimeAfterNow, mockInProgressExpiry, mockData, mockPayloadHash);
4141
});
@@ -54,7 +54,7 @@ describe('Given an idempotency record that has a status not in the IdempotencyRe
5454
let idempotencyRecord: IdempotencyRecord;
5555
beforeEach(() => {
5656
const mockNowBeforeExiryTime = 1487076707000;
57-
const expiryTimeAfterNow = 1487076708000;
57+
const expiryTimeAfterNow = 1487076708;
5858
Date.now = jest.fn(() => mockNowBeforeExiryTime);
5959
idempotencyRecord = new IdempotencyRecord(mockIdempotencyKey, 'NOT_A_STATUS' as IdempotencyRecordStatus, expiryTimeAfterNow, mockInProgressExpiry, mockData, mockPayloadHash);
6060
});

0 commit comments

Comments
 (0)