-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonSerializingCache.spec.ts
More file actions
103 lines (81 loc) · 3.14 KB
/
NonSerializingCache.spec.ts
File metadata and controls
103 lines (81 loc) · 3.14 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import "mocha";
import * as chai from "chai";
import { MemoryCache } from './MemoryCache';
import { DelayedCache } from './test/DelayedCache';
import { NonSerializingCache } from './NonSerializingCache';
const expect = chai.expect;
/* tslint:disable:only-arrow-functions */
/* tslint:disable:max-classes-per-file */
describe("NonSerializingCache", function () {
describe('get', function () {
it ("should fill using callback", async function () {
const testValue = 'value';
const testKey = 'key';
const memoryCache = new MemoryCache();
const cache = new NonSerializingCache(memoryCache, async () => testValue);
const result1 = await cache.get(testKey);
expect(result1).to.equal(testValue);
const result2 = await memoryCache.get(testKey);
expect(result2).to.equal(testValue);
});
it("should only issue one read per request run", async function () {
class TestCache extends MemoryCache {
counter: number
constructor() {
super();
this.counter = 0;
}
get(key: string) {
this.counter++;
return super.get(key);
}
}
const testValue = 'value';
const testKey = 'key';
const rootCache = new DelayedCache(new TestCache(), { get: 10 });
const cache = new NonSerializingCache(rootCache, async () => testValue);
await Promise.all([
cache.get(testKey), cache.get(testKey), cache.get(testKey)
]);
expect((rootCache.cache as TestCache).counter).to.equal(3);
});
});
describe('set', function () {
it('should not be supported', async function () {
const memoryCache = new MemoryCache();
const cache = new NonSerializingCache(memoryCache, async () => "value");
const testKey = 'key';
let set = false;
try {
await cache.set(testKey, "dummy");
set = true;
} catch (err) {
/* */
}
expect(set).to.equal(false);
});
});
describe('del', function () {
it("should only issue one delete per request run", async function () {
class TestCache extends MemoryCache {
counter: number
constructor() {
super();
this.counter = 0;
}
del(key: string) {
this.counter++;
return super.del(key);
}
}
const testValue = 'value';
const testKey = 'key';
const rootCache = new DelayedCache(new TestCache(), { get: 10 });
const cache = new NonSerializingCache(rootCache, async () => testValue);
await Promise.all([
cache.del(testKey), cache.del(testKey), cache.del(testKey)
]);
expect((rootCache.cache as TestCache).counter).to.equal(3);
});
});
});