Rewrite WallClockListener
as onTimeTravel
This commit is contained in:
parent
11cfb4f76f
commit
6668348197
5 changed files with 82 additions and 30 deletions
59
ts/test-both/util/startTimeTravelDetector_test.ts
Normal file
59
ts/test-both/util/startTimeTravelDetector_test.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
// Copyright 2022 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import * as sinon from 'sinon';
|
||||
|
||||
import { startTimeTravelDetector } from '../../util/startTimeTravelDetector';
|
||||
|
||||
describe('startTimeTravelDetector', () => {
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox({ useFakeTimers: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('calls the callback when the time between checks is more than 2 seconds', async function test() {
|
||||
const callback = sandbox.fake();
|
||||
|
||||
startTimeTravelDetector(callback);
|
||||
|
||||
// Normal clock behavior
|
||||
await sandbox.clock.tickAsync(1234);
|
||||
await sandbox.clock.tickAsync(5678);
|
||||
sinon.assert.notCalled(callback);
|
||||
|
||||
// Time travel ≤2s
|
||||
sandbox.clock.setSystemTime(Date.now() + 1000);
|
||||
await sandbox.clock.tickAsync(1000);
|
||||
sinon.assert.notCalled(callback);
|
||||
sandbox.clock.setSystemTime(Date.now() + 1999);
|
||||
await sandbox.clock.tickAsync(1);
|
||||
sinon.assert.notCalled(callback);
|
||||
|
||||
// Time travel >2s
|
||||
sandbox.clock.setSystemTime(Date.now() + 2001);
|
||||
await sandbox.clock.nextAsync();
|
||||
sinon.assert.calledOnce(callback);
|
||||
sandbox.clock.setSystemTime(Date.now() + 9999);
|
||||
await sandbox.clock.nextAsync();
|
||||
sinon.assert.calledTwice(callback);
|
||||
|
||||
// Normal clock behavior
|
||||
await sandbox.clock.tickAsync(9876);
|
||||
sinon.assert.calledTwice(callback);
|
||||
});
|
||||
|
||||
it('can detect time travel right after initialization', async () => {
|
||||
const callback = sandbox.fake();
|
||||
|
||||
startTimeTravelDetector(callback);
|
||||
|
||||
sandbox.clock.setSystemTime(Date.now() + 2001);
|
||||
await sandbox.clock.nextAsync();
|
||||
sinon.assert.calledOnce(callback);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue