signal-desktop/ts/test-both/TaskWithTimeout_test.ts

130 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2017 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import * as sinon from 'sinon';
import { sleep } from '../util/sleep';
2021-09-28 16:31:12 +00:00
import { explodePromise } from '../util/explodePromise';
import createTaskWithTimeout, {
suspendTasksWithTimeout,
resumeTasksWithTimeout,
} from '../textsecure/TaskWithTimeout';
describe('createTaskWithTimeout', () => {
let sandbox: sinon.SinonSandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
it('resolves when promise resolves', async () => {
const task = () => Promise.resolve('hi!');
const taskWithTimeout = createTaskWithTimeout(task, 'resolving-task');
const result = await taskWithTimeout();
assert.strictEqual(result, 'hi!');
});
it('flows error from promise back', async () => {
const error = new Error('original');
const task = () => Promise.reject(error);
const taskWithTimeout = createTaskWithTimeout(task, 'rejecting-task');
await assert.isRejected(taskWithTimeout(), 'original');
});
it('rejects if promise takes too long (this one logs error to console)', async () => {
const clock = sandbox.useFakeTimers();
2021-09-28 16:31:12 +00:00
const { promise: pause } = explodePromise<void>();
// Never resolves
const task = () => pause;
const taskWithTimeout = createTaskWithTimeout(task, 'slow-task');
const promise = assert.isRejected(taskWithTimeout());
2021-09-28 16:31:12 +00:00
await clock.runToLastAsync();
await promise;
});
it('rejects if task throws (and does not log about taking too long)', async () => {
const clock = sandbox.useFakeTimers();
const error = new Error('Task is throwing!');
const task = () => {
throw error;
};
const taskWithTimeout = createTaskWithTimeout(task, 'throwing-task');
2021-09-28 16:31:12 +00:00
await clock.runToLastAsync();
await assert.isRejected(taskWithTimeout(), 'Task is throwing!');
});
it('passes arguments to the underlying function', async () => {
const task = (arg: string) => Promise.resolve(arg);
const taskWithTimeout = createTaskWithTimeout(task, 'arguments-task');
const result = await taskWithTimeout('hi!');
assert.strictEqual(result, 'hi!');
});
it('suspends and resumes tasks', async () => {
const clock = sandbox.useFakeTimers();
let state = 0;
const task = async () => {
state = 1;
await sleep(900);
state = 2;
await sleep(900);
state = 3;
};
const taskWithTimeout = createTaskWithTimeout(task, 'suspend-task', {
timeout: 1000,
});
const promise = taskWithTimeout();
assert.strictEqual(state, 1);
suspendTasksWithTimeout();
2021-09-28 16:31:12 +00:00
await clock.tickAsync(900);
assert.strictEqual(state, 2);
resumeTasksWithTimeout();
2021-09-28 16:31:12 +00:00
await clock.tickAsync(900);
assert.strictEqual(state, 3);
await promise;
});
it('suspends and resumes timing out task', async () => {
const clock = sandbox.useFakeTimers();
2021-09-28 16:31:12 +00:00
const { promise: pause } = explodePromise<void>();
2021-09-28 16:31:12 +00:00
// Never resolves
const task = () => pause;
const taskWithTimeout = createTaskWithTimeout(task, 'suspend-slow-task');
const promise = assert.isRejected(taskWithTimeout());
suspendTasksWithTimeout();
2021-09-28 16:31:12 +00:00
await clock.runToLastAsync();
resumeTasksWithTimeout();
2021-09-28 16:31:12 +00:00
await clock.runToLastAsync();
await promise;
});
});