Add durations utility for computing durations

This commit is contained in:
Evan Hahn
2021-08-26 09:10:58 -05:00
committed by GitHub
parent c6aa668a9b
commit f86f753df9
28 changed files with 99 additions and 95 deletions
+7 -11
View File
@@ -1,18 +1,14 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as durations from '../../util/durations';
export type TestExpireTimer = Readonly<{ value: number; label: string }>;
const SECOND = 1;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
export const EXPIRE_TIMERS: ReadonlyArray<TestExpireTimer> = [
{ value: 42 * SECOND, label: '42 seconds' },
{ value: 5 * MINUTE, label: '5 minutes' },
{ value: 1 * HOUR, label: '1 hour' },
{ value: 6 * DAY, label: '6 days' },
{ value: 3 * WEEK, label: '3 weeks' },
{ value: 42 * durations.SECOND, label: '42 seconds' },
{ value: 5 * durations.MINUTE, label: '5 minutes' },
{ value: 1 * durations.HOUR, label: '1 hour' },
{ value: 6 * durations.DAY, label: '6 days' },
{ value: 3 * durations.WEEK, label: '3 weeks' },
];
+3 -4
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import * as moment from 'moment';
import * as durations from '../../util/durations';
import {
exponentialBackoffSleepTime,
@@ -20,7 +20,7 @@ describe('exponential backoff utilities', () => {
});
it('plateaus at a maximum after 15 attempts', () => {
const maximum = moment.duration(15, 'minutes').asMilliseconds();
const maximum = 15 * durations.MINUTE;
for (let attempt = 16; attempt < 100; attempt += 1) {
assert.strictEqual(exponentialBackoffSleepTime(attempt), maximum);
}
@@ -39,8 +39,7 @@ describe('exponential backoff utilities', () => {
it('returns 110 attempts for 1 day', () => {
// This is a test case that is lifted from iOS's codebase.
const oneDay = moment.duration(24, 'hours').asMilliseconds();
assert.strictEqual(exponentialBackoffMaxAttempts(oneDay), 110);
assert.strictEqual(exponentialBackoffMaxAttempts(durations.DAY), 110);
});
});
});
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import * as durations from '../../util/durations';
import { isConversationUnregistered } from '../../util/isConversationUnregistered';
@@ -33,12 +34,10 @@ describe('isConversationUnregistered', () => {
});
it('returns false if passed a time more than 6 hours ago', () => {
const oneMinute = 1000 * 60;
const sixHours = 1000 * 60 * 60 * 6;
assert.isFalse(
isConversationUnregistered({
discoveredUnregisteredAt: Date.now() - sixHours - oneMinute,
discoveredUnregisteredAt:
Date.now() - 6 * durations.HOUR - durations.MINUTE,
})
);
assert.isFalse(