2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-10-13 22:21:42 +00:00
|
|
|
import { assert } from 'chai';
|
|
|
|
import { useFakeTimers } from 'sinon';
|
|
|
|
|
|
|
|
import { sleep } from '../../util/sleep';
|
|
|
|
|
|
|
|
describe('sleep', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(function (this: Mocha.Context) {
|
2020-10-13 22:21:42 +00:00
|
|
|
// This isn't a hook.
|
|
|
|
this.clock = useFakeTimers();
|
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
afterEach(function (this: Mocha.Context) {
|
2020-10-13 22:21:42 +00:00
|
|
|
this.clock.restore();
|
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
it('returns a promise that resolves after the specified number of milliseconds', async function (this: Mocha.Context) {
|
2020-10-13 22:21:42 +00:00
|
|
|
let isDone = false;
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void (async () => {
|
2020-10-13 22:21:42 +00:00
|
|
|
await sleep(123);
|
|
|
|
isDone = true;
|
|
|
|
})();
|
|
|
|
|
|
|
|
assert.isFalse(isDone);
|
|
|
|
|
|
|
|
await this.clock.tickAsync(100);
|
|
|
|
assert.isFalse(isDone);
|
|
|
|
|
|
|
|
await this.clock.tickAsync(25);
|
|
|
|
assert.isTrue(isDone);
|
|
|
|
});
|
|
|
|
});
|