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