mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-04-18 23:49:20 +01:00
Simplify TaskWithTimeout
Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Co-authored-by: trevor-signal <131492920+trevor-signal@users.noreply.github.com>
This commit is contained in:
@@ -6,12 +6,14 @@ import * as sinon from 'sinon';
|
||||
|
||||
import { sleep } from '../util/sleep.std.js';
|
||||
import { explodePromise } from '../util/explodePromise.std.js';
|
||||
import createTaskWithTimeout, {
|
||||
import { MINUTE } from '../util/durations/index.std.js';
|
||||
import {
|
||||
runTaskWithTimeout,
|
||||
suspendTasksWithTimeout,
|
||||
resumeTasksWithTimeout,
|
||||
} from '../textsecure/TaskWithTimeout.std.js';
|
||||
|
||||
describe('createTaskWithTimeout', () => {
|
||||
describe('runTaskWithTimeout', () => {
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -24,18 +26,16 @@ describe('createTaskWithTimeout', () => {
|
||||
|
||||
it('resolves when promise resolves', async () => {
|
||||
const task = () => Promise.resolve('hi!');
|
||||
const taskWithTimeout = createTaskWithTimeout(task, 'resolving-task');
|
||||
|
||||
const result = await taskWithTimeout();
|
||||
const result = await runTaskWithTimeout(task, 'resolving-task');
|
||||
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');
|
||||
const taskWithTimeout = runTaskWithTimeout(task, 'rejecting-task');
|
||||
|
||||
await assert.isRejected(taskWithTimeout(), 'original');
|
||||
await assert.isRejected(taskWithTimeout, 'original');
|
||||
});
|
||||
|
||||
it('rejects if promise takes too long (this one logs error to console)', async () => {
|
||||
@@ -45,11 +45,11 @@ describe('createTaskWithTimeout', () => {
|
||||
|
||||
// Never resolves
|
||||
const task = () => pause;
|
||||
const taskWithTimeout = createTaskWithTimeout(task, 'slow-task');
|
||||
const taskWithTimeout = runTaskWithTimeout(task, 'slow-task');
|
||||
|
||||
const promise = assert.isRejected(taskWithTimeout());
|
||||
const promise = assert.isRejected(taskWithTimeout);
|
||||
|
||||
await clock.runToLastAsync();
|
||||
await clock.runAllAsync();
|
||||
|
||||
await promise;
|
||||
});
|
||||
@@ -61,17 +61,9 @@ describe('createTaskWithTimeout', () => {
|
||||
const task = () => {
|
||||
throw error;
|
||||
};
|
||||
const taskWithTimeout = createTaskWithTimeout(task, 'throwing-task');
|
||||
const taskWithTimeout = runTaskWithTimeout(task, 'throwing-task');
|
||||
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!');
|
||||
await assert.isRejected(taskWithTimeout, 'Task is throwing!');
|
||||
});
|
||||
|
||||
it('suspends and resumes tasks', async () => {
|
||||
@@ -81,25 +73,21 @@ describe('createTaskWithTimeout', () => {
|
||||
|
||||
const task = async () => {
|
||||
state = 1;
|
||||
await sleep(900);
|
||||
await sleep(2 * MINUTE - 100);
|
||||
state = 2;
|
||||
await sleep(900);
|
||||
await sleep(2 * MINUTE - 100);
|
||||
state = 3;
|
||||
};
|
||||
const taskWithTimeout = createTaskWithTimeout(task, 'suspend-task', {
|
||||
timeout: 1000,
|
||||
});
|
||||
|
||||
const promise = taskWithTimeout();
|
||||
const promise = runTaskWithTimeout(task, 'suspend-task', 'short-lived');
|
||||
|
||||
assert.strictEqual(state, 1);
|
||||
|
||||
suspendTasksWithTimeout();
|
||||
await clock.tickAsync(900);
|
||||
await clock.tickAsync(2 * MINUTE - 100);
|
||||
assert.strictEqual(state, 2);
|
||||
|
||||
resumeTasksWithTimeout();
|
||||
await clock.tickAsync(900);
|
||||
await clock.tickAsync(2 * MINUTE - 100);
|
||||
assert.strictEqual(state, 3);
|
||||
|
||||
await promise;
|
||||
@@ -112,17 +100,17 @@ describe('createTaskWithTimeout', () => {
|
||||
|
||||
// Never resolves
|
||||
const task = () => pause;
|
||||
const taskWithTimeout = createTaskWithTimeout(task, 'suspend-slow-task');
|
||||
const taskWithTimeout = runTaskWithTimeout(task, 'suspend-slow-task');
|
||||
|
||||
const promise = assert.isRejected(taskWithTimeout());
|
||||
const promise = assert.isRejected(taskWithTimeout);
|
||||
|
||||
suspendTasksWithTimeout();
|
||||
|
||||
await clock.runToLastAsync();
|
||||
await clock.runAllAsync();
|
||||
|
||||
resumeTasksWithTimeout();
|
||||
|
||||
await clock.runToLastAsync();
|
||||
await clock.runAllAsync();
|
||||
|
||||
await promise;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user