forked from jaredwray/cacheable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-caching.test.ts
164 lines (140 loc) · 4.58 KB
/
multi-caching.test.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { describe, expect, it, beforeEach } from 'vitest';
import { faker } from '@faker-js/faker';
import { sleep } from './utils';
import {
Cache,
caching,
MemoryCache,
MultiCache,
multiCaching,
Store,
} from '../src';
describe('multiCaching', () => {
let memoryCache: MemoryCache;
let memoryCache2: MemoryCache;
let memoryCache3: MemoryCache;
let multiCache: MultiCache;
let ttl: number;
let defaultTtl: number;
let key: string;
beforeEach(async () => {
ttl = 100;
defaultTtl = 5000;
memoryCache = await caching('memory', {
ttl,
});
memoryCache2 = await caching('memory', {
ttl,
});
memoryCache3 = await caching('memory', {
ttl,
});
key = faker.datatype.string(20);
});
describe('get(), set(), del(), reset(), mget(), mset()', () => {
let value: string;
beforeEach(() => {
multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]);
key = faker.datatype.string(20);
value = faker.datatype.string();
});
describe('set()', () => {
it('lets us set data in all caches', async () => {
await multiCache.set(key, value, defaultTtl);
await expect(memoryCache.get(key)).resolves.toEqual(value);
await expect(memoryCache2.get(key)).resolves.toEqual(value);
await expect(memoryCache3.get(key)).resolves.toEqual(value);
});
});
describe('get()', () => {
it('lets us get data', async () => {
await multiCache.set(key, value, defaultTtl);
await expect(multiCache.get(key)).resolves.toEqual(value);
});
});
describe('wrap()', () => {
it('should get data', async () => {
await multiCache.wrap(key, async () => value);
await expect(memoryCache.get(key)).resolves.toEqual(value);
await expect(memoryCache2.get(key)).resolves.toEqual(value);
await expect(memoryCache3.get(key)).resolves.toEqual(value);
await expect(multiCache.wrap(key, async () => 'foo')).resolves.toEqual(
value,
);
});
});
describe('del()', () => {
it('should delete data', async () => {
await multiCache.set(key, value);
await multiCache.del(key);
await expect(multiCache.get(key)).resolves.toBeUndefined();
});
});
describe('reset()', () => {
it('should reset cache', async () => {
await multiCache.set(key, value);
await multiCache.reset();
await expect(multiCache.get(key)).resolves.toBeUndefined();
});
});
describe('when cache fails', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const empty = (async () => {}) as never;
const cache: Cache = {
get: async () => {
throw new Error();
},
set: empty,
del: empty,
reset: empty,
wrap: empty,
store: {} as Store,
};
const cacheEmpty: Cache = {
get: empty,
set: empty,
del: empty,
reset: empty,
wrap: empty,
store: {} as Store,
};
it('should get error', async () => {
multiCache = multiCaching([cache, memoryCache]);
await multiCache.set(key, value);
await expect(multiCache.get(key)).resolves.toEqual(value);
});
it('should get all error', async () => {
multiCache = multiCaching([cache]);
await multiCache.set(key, value);
await expect(multiCache.get(key)).resolves.toBeUndefined();
});
it('should get empty', async () => {
multiCache = multiCaching([cacheEmpty, memoryCache]);
await multiCache.set(key, value);
await expect(multiCache.get(key)).resolves.toEqual(value);
});
it('should get all empty', async () => {
multiCache = multiCaching([cacheEmpty, cacheEmpty]);
await multiCache.set(key, value);
await expect(multiCache.get(key)).resolves.toBeUndefined();
});
});
});
describe('issues', () => {
it('#253', async () => {
const cache0 = await caching('memory', { ttl: 500 });
const cache1 = await caching('memory', { ttl: 1000 });
const multi = multiCaching([cache0, cache1]);
const key = 'bar';
const value = 'foo';
const fn = async () => value;
await multi.wrap(key, fn);
await sleep(600);
await expect(cache0.get(key)).resolves.toBeUndefined();
await expect(cache1.get(key)).resolves.toEqual(value);
await multi.wrap(key, fn);
await expect(cache0.get(key)).resolves.toEqual(value);
await expect(cache1.get(key)).resolves.toEqual(value);
});
});
});