Path: blob/main/extensions/copilot/src/util/common/test/async.spec.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import sinon from 'sinon';6import { TaskQueue } from '../async';78import { assert, beforeEach, describe, it } from 'vitest';9import { isCancellationError } from '../../vs/base/common/errors';101112describe('TaskQueue', () => {13let taskQueue: TaskQueue;1415beforeEach(() => {16taskQueue = new TaskQueue();17});1819it('should schedule and run a single task', async () => {20const task = sinon.stub().resolves('result');21const result = await taskQueue.schedule(task);22sinon.assert.calledOnce(task);23assert.strictEqual(result, 'result');24});2526it('should schedule and run multiple tasks in order', async () => {27const results: string[] = [];28const task1 = sinon.stub().callsFake(async () => { results.push('task1'); });29const task2 = sinon.stub().callsFake(async () => { results.push('task2'); });3031await taskQueue.schedule(task1);32await taskQueue.schedule(task2);3334sinon.assert.callOrder(task1, task2);35assert.deepStrictEqual(results, ['task1', 'task2']);36});3738it('should clear pending tasks', async () => {39try {40const task1 = sinon.stub().resolves('task1');41const task2 = sinon.stub().resolves('task2');4243const p1 = taskQueue.schedule(task1);44const p2 = taskQueue.schedule(task2);45taskQueue.clearPending();4647sinon.assert.calledOnce(task1);48sinon.assert.notCalled(task2);49await p1;50await p2;51} catch (e) {52if (!isCancellationError(e)) {53throw e;54}55}56});5758});596061