Fix flaky TaskWithTimeout test

This commit is contained in:
Fedor Indutny 2021-09-28 09:31:12 -07:00 committed by GitHub
parent 7b9a68b7f9
commit 377cdb3281
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
}); });