Fix flaky TaskWithTimeout test
This commit is contained in:
parent
7b9a68b7f9
commit
377cdb3281
1 changed files with 15 additions and 22 deletions
|
@ -5,6 +5,7 @@ import { assert } from 'chai';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
|
|
||||||
import { sleep } from '../util/sleep';
|
import { sleep } from '../util/sleep';
|
||||||
|
import { explodePromise } from '../util/explodePromise';
|
||||||
import createTaskWithTimeout, {
|
import createTaskWithTimeout, {
|
||||||
suspendTasksWithTimeout,
|
suspendTasksWithTimeout,
|
||||||
resumeTasksWithTimeout,
|
resumeTasksWithTimeout,
|
||||||
|
@ -40,15 +41,15 @@ describe('createTaskWithTimeout', () => {
|
||||||
it('rejects if promise takes too long (this one logs error to console)', async () => {
|
it('rejects if promise takes too long (this one logs error to console)', async () => {
|
||||||
const clock = sandbox.useFakeTimers();
|
const clock = sandbox.useFakeTimers();
|
||||||
|
|
||||||
const task = async () => {
|
const { promise: pause } = explodePromise<void>();
|
||||||
await sleep(3000000);
|
|
||||||
};
|
// Never resolves
|
||||||
|
const task = () => pause;
|
||||||
const taskWithTimeout = createTaskWithTimeout(task, 'slow-task');
|
const taskWithTimeout = createTaskWithTimeout(task, 'slow-task');
|
||||||
|
|
||||||
const promise = assert.isRejected(taskWithTimeout());
|
const promise = assert.isRejected(taskWithTimeout());
|
||||||
|
|
||||||
await clock.nextAsync();
|
await clock.runToLastAsync();
|
||||||
await clock.nextAsync();
|
|
||||||
|
|
||||||
await promise;
|
await promise;
|
||||||
});
|
});
|
||||||
|
@ -61,7 +62,7 @@ describe('createTaskWithTimeout', () => {
|
||||||
throw error;
|
throw error;
|
||||||
};
|
};
|
||||||
const taskWithTimeout = createTaskWithTimeout(task, 'throwing-task');
|
const taskWithTimeout = createTaskWithTimeout(task, 'throwing-task');
|
||||||
await clock.nextAsync();
|
await clock.runToLastAsync();
|
||||||
await assert.isRejected(taskWithTimeout(), 'Task is throwing!');
|
await assert.isRejected(taskWithTimeout(), 'Task is throwing!');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -94,11 +95,11 @@ describe('createTaskWithTimeout', () => {
|
||||||
assert.strictEqual(state, 1);
|
assert.strictEqual(state, 1);
|
||||||
|
|
||||||
suspendTasksWithTimeout();
|
suspendTasksWithTimeout();
|
||||||
await clock.nextAsync();
|
await clock.tickAsync(900);
|
||||||
assert.strictEqual(state, 2);
|
assert.strictEqual(state, 2);
|
||||||
|
|
||||||
resumeTasksWithTimeout();
|
resumeTasksWithTimeout();
|
||||||
await clock.nextAsync();
|
await clock.tickAsync(900);
|
||||||
assert.strictEqual(state, 3);
|
assert.strictEqual(state, 3);
|
||||||
|
|
||||||
await promise;
|
await promise;
|
||||||
|
@ -107,29 +108,21 @@ describe('createTaskWithTimeout', () => {
|
||||||
it('suspends and resumes timing out task', async () => {
|
it('suspends and resumes timing out task', async () => {
|
||||||
const clock = sandbox.useFakeTimers();
|
const clock = sandbox.useFakeTimers();
|
||||||
|
|
||||||
let state = 0;
|
const { promise: pause } = explodePromise<void>();
|
||||||
|
|
||||||
const task = async () => {
|
// Never resolves
|
||||||
state = 1;
|
const task = () => pause;
|
||||||
await sleep(3000000);
|
|
||||||
state = 2;
|
|
||||||
await sleep(3000000);
|
|
||||||
state = 3;
|
|
||||||
};
|
|
||||||
const taskWithTimeout = createTaskWithTimeout(task, 'suspend-slow-task');
|
const taskWithTimeout = createTaskWithTimeout(task, 'suspend-slow-task');
|
||||||
|
|
||||||
const promise = assert.isRejected(taskWithTimeout());
|
const promise = assert.isRejected(taskWithTimeout());
|
||||||
|
|
||||||
assert.strictEqual(state, 1);
|
|
||||||
|
|
||||||
suspendTasksWithTimeout();
|
suspendTasksWithTimeout();
|
||||||
await clock.nextAsync();
|
|
||||||
assert.strictEqual(state, 2);
|
await clock.runToLastAsync();
|
||||||
|
|
||||||
resumeTasksWithTimeout();
|
resumeTasksWithTimeout();
|
||||||
await clock.nextAsync();
|
|
||||||
|
|
||||||
assert.strictEqual(state, 2);
|
await clock.runToLastAsync();
|
||||||
|
|
||||||
await promise;
|
await promise;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue