Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/cache.test.ts
3296 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 { timeout } from '../../common/async.js';
8
import { Cache } from '../../common/cache.js';
9
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
10
11
suite('Cache', () => {
12
13
ensureNoDisposablesAreLeakedInTestSuite();
14
15
test('simple value', () => {
16
let counter = 0;
17
const cache = new Cache(_ => Promise.resolve(counter++));
18
19
return cache.get().promise
20
.then(c => assert.strictEqual(c, 0), () => assert.fail('Unexpected assertion error'))
21
.then(() => cache.get().promise)
22
.then(c => assert.strictEqual(c, 0), () => assert.fail('Unexpected assertion error'));
23
});
24
25
test('simple error', () => {
26
let counter = 0;
27
const cache = new Cache(_ => Promise.reject(new Error(String(counter++))));
28
29
return cache.get().promise
30
.then(() => assert.fail('Unexpected assertion error'), err => assert.strictEqual(err.message, '0'))
31
.then(() => cache.get().promise)
32
.then(() => assert.fail('Unexpected assertion error'), err => assert.strictEqual(err.message, '0'));
33
});
34
35
test('should retry cancellations', () => {
36
let counter1 = 0, counter2 = 0;
37
38
const cache = new Cache(token => {
39
counter1++;
40
return Promise.resolve(timeout(2, token).then(() => counter2++));
41
});
42
43
assert.strictEqual(counter1, 0);
44
assert.strictEqual(counter2, 0);
45
let result = cache.get();
46
assert.strictEqual(counter1, 1);
47
assert.strictEqual(counter2, 0);
48
result.promise.then(undefined, () => assert(true));
49
result.dispose();
50
assert.strictEqual(counter1, 1);
51
assert.strictEqual(counter2, 0);
52
53
result = cache.get();
54
assert.strictEqual(counter1, 2);
55
assert.strictEqual(counter2, 0);
56
57
return result.promise
58
.then(c => {
59
assert.strictEqual(counter1, 2);
60
assert.strictEqual(counter2, 1);
61
})
62
.then(() => cache.get().promise)
63
.then(c => {
64
assert.strictEqual(counter1, 2);
65
assert.strictEqual(counter2, 1);
66
});
67
});
68
});
69
70