Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/test/async.spec.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import sinon from 'sinon';
7
import { TaskQueue } from '../async';
8
9
import { assert, beforeEach, describe, it } from 'vitest';
10
import { isCancellationError } from '../../vs/base/common/errors';
11
12
13
describe('TaskQueue', () => {
14
let taskQueue: TaskQueue;
15
16
beforeEach(() => {
17
taskQueue = new TaskQueue();
18
});
19
20
it('should schedule and run a single task', async () => {
21
const task = sinon.stub().resolves('result');
22
const result = await taskQueue.schedule(task);
23
sinon.assert.calledOnce(task);
24
assert.strictEqual(result, 'result');
25
});
26
27
it('should schedule and run multiple tasks in order', async () => {
28
const results: string[] = [];
29
const task1 = sinon.stub().callsFake(async () => { results.push('task1'); });
30
const task2 = sinon.stub().callsFake(async () => { results.push('task2'); });
31
32
await taskQueue.schedule(task1);
33
await taskQueue.schedule(task2);
34
35
sinon.assert.callOrder(task1, task2);
36
assert.deepStrictEqual(results, ['task1', 'task2']);
37
});
38
39
it('should clear pending tasks', async () => {
40
try {
41
const task1 = sinon.stub().resolves('task1');
42
const task2 = sinon.stub().resolves('task2');
43
44
const p1 = taskQueue.schedule(task1);
45
const p2 = taskQueue.schedule(task2);
46
taskQueue.clearPending();
47
48
sinon.assert.calledOnce(task1);
49
sinon.assert.notCalled(task2);
50
await p1;
51
await p2;
52
} catch (e) {
53
if (!isCancellationError(e)) {
54
throw e;
55
}
56
}
57
});
58
59
});
60
61