Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/node/test/debounce.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 assert from 'assert';
7
import { suite, test } from 'vitest';
8
import { Debouncer } from '../../common/debounce';
9
10
suite('Debouncing', function () {
11
test('single debounce call', async function () {
12
const debouncer = new Debouncer();
13
await debouncer.debounce(1);
14
});
15
test('repeated call within time limit', async function () {
16
const debouncer = new Debouncer();
17
let result: boolean | undefined;
18
(async () => {
19
try {
20
await debouncer.debounce(10);
21
result = true;
22
} catch {
23
// we should end up here as the debounce call should get cancelled by the subsequent one
24
result = false;
25
}
26
})();
27
await debouncer.debounce(1);
28
assert.deepStrictEqual(result, false);
29
});
30
test('repeated call outside time limit', async function () {
31
const debouncer = new Debouncer();
32
let result: boolean | undefined;
33
(async () => {
34
try {
35
await debouncer.debounce(1);
36
// we should end up here as the debounce call should have time to finish before the next one
37
result = true;
38
} catch {
39
result = false;
40
}
41
})();
42
await new Promise(resolve => setTimeout(resolve, 5));
43
await debouncer.debounce(1);
44
assert.deepStrictEqual(result, true);
45
});
46
test('multiple debounce objects are independent', async function () {
47
const debouncer1 = new Debouncer();
48
const debouncer2 = new Debouncer();
49
let result: boolean | undefined;
50
(async () => {
51
try {
52
await debouncer1.debounce(10);
53
// we should end up here as the debounce call won't be cancelled by the second one even though
54
// they run in parallel.
55
result = true;
56
} catch {
57
result = false;
58
}
59
})();
60
await debouncer2.debounce(1);
61
await new Promise(resolve => setTimeout(resolve, 20));
62
assert.deepStrictEqual(result, true);
63
});
64
});
65
66