Skip to content

Commit

Permalink
Switch away from timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
lramos15 committed Oct 3, 2024
1 parent 32421eb commit 70441b9
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/vs/base/test/common/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,15 @@ suite('Async', () => {

test('waitThrottleDelayBetweenWorkUnits option', async () => {
const handled: number[] = [];
const handler = (units: readonly number[]) => handled.push(...units);
let handledCallback: Function;
let handledPromise = new Promise(resolve => handledCallback = resolve);
let currentTime = 0;

const handler = (units: readonly number[]) => {
handled.push(...units);
handledCallback();
handledPromise = new Promise(resolve => handledCallback = resolve);
};

const worker = store.add(new async.ThrottledWorker<number>({
maxWorkChunkSize: 5,
Expand All @@ -1531,20 +1539,22 @@ suite('Async', () => {
waitThrottleDelayBetweenWorkUnits: true
}, handler));

// Schedule work, it should execute immediately
currentTime = Date.now();
let worked = worker.work([1, 2, 3]);
assert.strictEqual(worked, true);
assertArrayEquals(handled, [1, 2, 3]);
assert.strictEqual(Date.now() - currentTime < 5, true);

await async.timeout(2);

// Add more work immediately, should not be processed due to the `waitThrottleDelayBetweenWorkUnits`. We only waited 2 of the 5ms.
worked = worker.work([4, 5, 6]);
// Schedule work again, it should wait at least throttle delay before executing
currentTime = Date.now();
worked = worker.work([4, 5]);
assert.strictEqual(worked, true);
// Throttle delay hasn't reset so we still must wait
assertArrayEquals(handled, [1, 2, 3]);

// Wait an additional 5 ms and check that the work has now been processed
await async.timeout(5);
assertArrayEquals(handled, [1, 2, 3, 4, 5, 6]);
await handledPromise;
assert.strictEqual(Date.now() - currentTime >= 5, true);
assertArrayEquals(handled, [1, 2, 3, 4, 5]);
});
});

Expand Down

0 comments on commit 70441b9

Please sign in to comment.