forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.test.ts
More file actions
65 lines (54 loc) · 2 KB
/
cache.test.ts
File metadata and controls
65 lines (54 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Cache } from 'vs/base/common/cache';
import { timeout } from 'vs/base/common/async';
suite('Cache', () => {
test('simple value', () => {
let counter = 0;
const cache = new Cache(_ => Promise.resolve(counter++));
return cache.get().promise
.then(c => assert.equal(c, 0), () => assert.fail('Unexpected assertion error'))
.then(() => cache.get().promise)
.then(c => assert.equal(c, 0), () => assert.fail('Unexpected assertion error'));
});
test('simple error', () => {
let counter = 0;
const cache = new Cache(_ => Promise.reject(new Error(String(counter++))));
return cache.get().promise
.then(() => assert.fail('Unexpected assertion error'), err => assert.equal(err.message, 0))
.then(() => cache.get().promise)
.then(() => assert.fail('Unexpected assertion error'), err => assert.equal(err.message, 0));
});
test('should retry cancellations', () => {
let counter1 = 0, counter2 = 0;
const cache = new Cache(token => {
counter1++;
return Promise.resolve(timeout(2, token).then(() => counter2++));
});
assert.equal(counter1, 0);
assert.equal(counter2, 0);
let result = cache.get();
assert.equal(counter1, 1);
assert.equal(counter2, 0);
result.promise.then(undefined, () => assert(true));
result.dispose();
assert.equal(counter1, 1);
assert.equal(counter2, 0);
result = cache.get();
assert.equal(counter1, 2);
assert.equal(counter2, 0);
return result.promise
.then(c => {
assert.equal(counter1, 2);
assert.equal(counter2, 1);
})
.then(() => cache.get().promise)
.then(c => {
assert.equal(counter1, 2);
assert.equal(counter2, 1);
});
});
});