extract Cache class

This commit is contained in:
Joao Moreno
2016-08-23 16:05:17 +02:00
parent 538f69b5c6
commit 361348287e
3 changed files with 109 additions and 4 deletions

View File

@@ -0,0 +1,66 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import Cache from 'vs/base/common/cache';
import { TPromise } from 'vs/base/common/winjs.base';
suite('Cache', () => {
test('simple value', () => {
let counter = 0;
const cache = new Cache(() => TPromise.as(counter++));
return cache.get()
.then(c => assert.equal(c, 0), () => assert.fail())
.then(() => cache.get())
.then(c => assert.equal(c, 0), () => assert.fail());
});
test('simple error', () => {
let counter = 0;
const cache = new Cache(() => TPromise.wrapError(counter++));
return cache.get()
.then(() => assert.fail(), err => assert.equal(err, 0))
.then(() => cache.get())
.then(() => assert.fail(), err => assert.equal(err, 0));
});
test('should retry cancellations', () => {
let counter1 = 0, counter2 = 0;
const cache = new Cache(() => {
counter1++;
return TPromise.timeout(1).then(() => counter2++);
});
assert.equal(counter1, 0);
assert.equal(counter2, 0);
let promise = cache.get();
assert.equal(counter1, 1);
assert.equal(counter2, 0);
promise.cancel();
assert.equal(counter1, 1);
assert.equal(counter2, 0);
promise = cache.get();
assert.equal(counter1, 2);
assert.equal(counter2, 0);
return promise
.then(c => {
assert.equal(counter1, 2);
assert.equal(counter2, 1);
})
.then(() => cache.get())
.then(c => {
assert.equal(counter1, 2);
assert.equal(counter2, 1);
});
});
});