Consolidate sleep usages into a tested utility function

This commit is contained in:
Evan Hahn 2020-10-13 17:21:42 -05:00 committed by Scott Nonnenberg
parent c4de9436f3
commit 60f2422e2a
10 changed files with 357 additions and 84 deletions

View file

@ -8,6 +8,7 @@ import {
} from '../util/zkgroup';
import { GroupCredentialType } from '../textsecure/WebAPI';
import { sleep } from '../util/sleep';
export const GROUP_CREDENTIALS_KEY = 'groupCredentials';
@ -30,10 +31,6 @@ function getTodayInEpoch() {
return Math.floor(Date.now() / DAY);
}
async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let started = false;
export async function initializeGroupCredentialFetcher(): Promise<void> {

View file

@ -0,0 +1,33 @@
import { assert } from 'chai';
import { useFakeTimers } from 'sinon';
import { sleep } from '../../util/sleep';
describe('sleep', () => {
beforeEach(function beforeEach() {
// This isn't a hook.
// eslint-disable-next-line react-hooks/rules-of-hooks
this.clock = useFakeTimers();
});
afterEach(function afterEach() {
this.clock.restore();
});
it('returns a promise that resolves after the specified number of milliseconds', async function test() {
let isDone = false;
(async () => {
await sleep(123);
isDone = true;
})();
assert.isFalse(isDone);
await this.clock.tickAsync(100);
assert.isFalse(isDone);
await this.clock.tickAsync(25);
assert.isTrue(isDone);
});
});

View file

@ -1,5 +1,7 @@
import PQueue from 'p-queue';
import { sleep } from './sleep';
declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@ -28,10 +30,6 @@ export type BatcherType<ItemType> = {
unregister: () => void;
};
async function sleep(ms: number): Promise<void> {
await new Promise(resolve => setTimeout(resolve, ms));
}
export function createBatcher<ItemType>(
options: BatcherOptionsType<ItemType>
): BatcherType<ItemType> {

View file

@ -1429,21 +1429,243 @@
"updated": "2018-09-15T00:38:04.183Z"
},
{
"rule": "DOM-innerHTML",
"path": "node_modules/@sinonjs/formatio/lib/formatio.js",
"line": " // SVG elements have undefined innerHTML",
"lineNumber": 204,
"reasonCategory": "falseMatch",
"updated": "2018-09-15T00:38:04.183Z"
"rule": "jQuery-wrap(",
"path": "node_modules/@sinonjs/commons/lib/deprecated.js",
"line": " * Returns a string which can be supplied to `wrap()` to notify the user that a",
"lineNumber": 24,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-wrap(",
"path": "node_modules/@sinonjs/commons/lib/deprecated.test.js",
"line": " wrapped = deprecated.wrap(method, msg);",
"lineNumber": 66,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-wrap(",
"path": "node_modules/@sinonjs/commons/lib/deprecated.test.js",
"line": " wrapped = deprecated.wrap(method, msg);",
"lineNumber": 80,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-before(",
"path": "node_modules/@sinonjs/commons/lib/deprecated.test.js",
"line": " before(function() {",
"lineNumber": 86,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "eval",
"path": "node_modules/@sinonjs/commons/lib/function-name.test.js",
"line": " fn = eval(\"(function*() {})\")().constructor;",
"lineNumber": 67,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z"
},
{
"rule": "jQuery-before(",
"path": "node_modules/@sinonjs/commons/lib/global.test.js",
"line": " before(function() {",
"lineNumber": 7,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-wrap(",
"path": "node_modules/@sinonjs/fake-timers/fake-timers.js",
"line": "// defaultMsg returns a string which can be supplied to `wrap()` to notify the user that a particular part of the",
"lineNumber": 75,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "eval",
"path": "node_modules/@sinonjs/fake-timers/fake-timers.js",
"line": " eval(timer.func);",
"lineNumber": 1729,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z"
},
{
"rule": "eval",
"path": "node_modules/@sinonjs/fake-timers/src/fake-timers-src.js",
"line": " eval(timer.func);",
"lineNumber": 425,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z"
},
{
"rule": "DOM-innerHTML",
"path": "node_modules/@sinonjs/formatio/lib/formatio.js",
"line": " var content = element.innerHTML || \"\";",
"lineNumber": 205,
"reasonCategory": "usageTrusted",
"updated": "2020-09-11T17:24:56.124Z",
"reasonDetail": "Read, not write"
"line": " // SVG elements have undefined innerHTML",
"lineNumber": 253,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "DOM-innerHTML",
"path": "node_modules/@sinonjs/formatio/lib/formatio.js",
"line": " var content = element.innerHTML || \"\";",
"lineNumber": 254,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z"
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 1439,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(buffer);",
"lineNumber": 1738,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend([gb18030_second, bite]);",
"lineNumber": 1758,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 1798,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 2007,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 2175,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 2492,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 2515,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend([lead, bite]);",
"lineNumber": 2570,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(code_point);",
"lineNumber": 2612,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(code_point);",
"lineNumber": 2665,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(code_point);",
"lineNumber": 2676,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(code_point);",
"lineNumber": 2697,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 2790,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(bite);",
"lineNumber": 2957,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "jQuery-prepend(",
"path": "node_modules/@sinonjs/text-encoding/lib/encoding.js",
"line": " stream.prepend(convertCodeUnitToBytes(code_unit, utf16_be));",
"lineNumber": 3139,
"reasonCategory": "testCode",
"updated": "2020-10-09T22:28:48.591Z",
"reasonDetail": "Test code. Also a false match."
},
{
"rule": "eval",

5
ts/util/sleep.ts Normal file
View file

@ -0,0 +1,5 @@
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}

View file

@ -1,5 +1,7 @@
import PQueue from 'p-queue';
import { sleep } from './sleep';
declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@ -39,10 +41,6 @@ type BatcherType<ItemType> = {
unregister: () => void;
};
async function sleep(ms: number): Promise<void> {
await new Promise(resolve => setTimeout(resolve, ms));
}
export function createWaitBatcher<ItemType>(
options: BatcherOptionsType<ItemType>
): BatcherType<ItemType> {