/
multi-caching.test.ts
391 lines (340 loc) · 11.7 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* eslint-disable max-nested-callbacks */
import {
beforeEach, describe, expect, it, vi,
} from 'vitest';
import {faker} from '@faker-js/faker';
import EventEmitter from 'eventemitter3';
import {
type Cache, caching, type MemoryCache, type MultiCache, multiCaching, type Store,
} from '../src/index.js';
import {sleep} from './utils.js';
describe('multiCaching', () => {
let memoryCache: MemoryCache;
let memoryCache2: MemoryCache;
let memoryCache3: MemoryCache;
let multiCache: MultiCache;
let ttl: number;
let defaultTtl: number;
let key: string;
async function multiMset() {
const keys = [faker.string.sample(20), faker.string.sample(20)];
const values = [faker.string.sample(), faker.string.sample()];
await multiCache.mset(
[
[keys[0], values[0]],
[keys[1], values[1]],
],
defaultTtl,
);
return {keys, values};
}
beforeEach(async () => {
ttl = 100;
defaultTtl = 5000;
memoryCache = await caching('memory', {
ttl,
});
memoryCache2 = await caching('memory', {
ttl,
});
memoryCache3 = await caching('memory', {
ttl,
});
key = faker.string.sample(20);
});
describe('get(), set(), del(), reset(), mget(), mset()', () => {
let value: string;
beforeEach(() => {
multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]);
key = faker.string.sample(20);
value = faker.string.sample();
});
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,
);
});
it('lets us set the ttl to be milliseconds', async () => {
const ttl = 2 * 1000;
await multiCache.wrap(key, async () => value, ttl);
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,
);
await sleep(ttl + 1000);
await expect(memoryCache.get(key)).resolves.toBeUndefined();
await expect(memoryCache2.get(key)).resolves.toBeUndefined();
await expect(memoryCache3.get(key)).resolves.toBeUndefined();
await expect(multiCache.wrap(key, async () => 'foo')).resolves.toEqual(
'foo',
);
});
it('lets us set the ttl to be a function', async () => {
const sec = faker.number.int({min: 2, max: 4});
value = faker.string.sample(sec * 2);
const function_ = vi.fn(() => 1000);
await multiCache.wrap(key, async () => value, function_);
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,
);
expect(function_).toHaveBeenCalledTimes(1);
await sleep(3000);
await expect(memoryCache.get(key)).resolves.toBeUndefined();
await expect(memoryCache2.get(key)).resolves.toBeUndefined();
await expect(memoryCache3.get(key)).resolves.toBeUndefined();
await expect(multiCache.wrap(key, async () => 'foo')).resolves.toEqual(
'foo',
);
});
});
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('mset()', () => {
it('lets us set multiple keys in all caches', async () => {
const {keys, values} = await multiMset();
await expect(memoryCache.get(keys[0])).resolves.toEqual(values[0]);
await expect(memoryCache2.get(keys[0])).resolves.toEqual(values[0]);
await expect(memoryCache3.get(keys[0])).resolves.toEqual(values[0]);
await expect(memoryCache.get(keys[1])).resolves.toEqual(values[1]);
await expect(memoryCache2.get(keys[1])).resolves.toEqual(values[1]);
await expect(memoryCache3.get(keys[1])).resolves.toEqual(values[1]);
});
});
describe('mget()', () => {
it('lets us get multiple keys', async () => {
const {keys, values} = await multiMset();
await multiCache.set(keys[0], values[0], defaultTtl);
await memoryCache3.set(keys[1], values[1], defaultTtl);
await expect(multiCache.mget(...keys)).resolves.toStrictEqual(values);
});
it('lets us get multiple undefined', async () => {
const length = 4;
await multiMset();
const arguments_ = Array.from({length}).fill('').map(() => faker.string.sample());
await expect(multiCache.mget(...arguments_)).resolves.toStrictEqual(
Array.from({length}).fill(undefined),
);
});
});
describe('mdel()', () => {
it('lets us delete multiple keys', async () => {
const {keys} = await multiMset();
await multiCache.mdel(...keys);
await expect(memoryCache.get(keys[0])).resolves.toBeUndefined();
await expect(memoryCache.get(keys[1])).resolves.toBeUndefined();
});
});
describe('when cache fails', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const empty = (async () => {}) as never;
const getErrorCache: Cache = {
async get() {
throw new Error('this is an error');
},
set: empty,
del: empty,
reset: empty,
wrap: empty,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
store: {} as Store,
on: empty,
removeListener: empty,
};
const setErrorCache: Cache = {
get: empty,
async set() {
throw new Error('this is an error');
},
del: empty,
reset: empty,
wrap: empty,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
store: {} as Store,
on: empty,
removeListener: empty,
};
const emitter = new EventEmitter();
const getErrorOnEmitterCache: Cache = {
async get() {
emitter.emit('error', {error: new Error('error!'), key, operation: 'get'});
return undefined;
},
set: empty,
del: empty,
reset: empty,
wrap: empty,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
store: {} as Store,
on: (type, function_) => emitter.on('error', function_),
removeListener: empty,
};
const cacheEmpty: Cache = {
get: empty,
set: empty,
del: empty,
reset: empty,
wrap: empty,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
store: {} as Store,
on: empty,
removeListener: empty,
};
it('should get error', async () => {
multiCache = multiCaching([getErrorCache, memoryCache]);
await multiCache.set(key, value);
await expect(multiCache.get(key)).resolves.toEqual(value);
});
it('should get all error', async () => {
multiCache = multiCaching([getErrorCache]);
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();
});
it('emits an error event when store.get() fails', async () => {
multiCache = multiCaching([getErrorCache, memoryCache]);
let errorEvent;
multiCache.on('error', error => {
errorEvent = error;
});
await multiCache.get(key);
expect(errorEvent!.error).not.toBeUndefined();
expect(errorEvent!.operation).toBe('get');
expect(errorEvent!.key).toBe(key);
});
it('should receive error event on set failure', async () => {
multiCache = multiCaching([setErrorCache, memoryCache]);
let errorEvent;
multiCache.on('error', error => {
errorEvent = error;
});
await multiCache.set(key, value);
expect(errorEvent!.error).not.toBeUndefined();
expect(errorEvent!.key).toBe(key);
expect(errorEvent!.data).toBe(value);
expect(errorEvent!.operation).toBe('set');
});
it('should receive error event on mget failure', async () => {
multiCache = multiCaching([setErrorCache, memoryCache]);
let errorEvent;
multiCache.on('error', error => {
errorEvent = error;
});
const key2 = value;
await multiCache.mget(key, key2);
expect(errorEvent!.error).not.toBeUndefined();
expect(errorEvent!.keys).toStrictEqual([key, key2]);
expect(errorEvent!.operation).toBe('mget');
});
it('should receive error event on get failure during wrap', async () => {
multiCache = multiCaching([getErrorCache, memoryCache]);
const error = new Error('store.get() failed');
const function_ = vi.fn().mockResolvedValue(value);
let errorEvent;
multiCache.on('error', error => {
errorEvent = error;
});
await multiCache.wrap(key, function_);
expect(errorEvent!.error).not.toBeUndefined();
expect(errorEvent!.operation).toBe('wrap');
expect(errorEvent!.key).toBe(key);
expect(errorEvent!.data).toBeUndefined();
});
it('should receive error events from underlying caches', async () => {
multiCache = multiCaching([getErrorOnEmitterCache]);
let errorEvent;
multiCache.on('error', error => {
errorEvent = error;
});
await multiCache.get(key);
expect(errorEvent!.error).not.toBeUndefined();
expect(errorEvent!.operation).toBe('get');
expect(errorEvent!.key).toBe(key);
});
});
});
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 function_ = async () => value;
await multi.wrap(key, function_);
await sleep(600);
await expect(cache0.get(key)).resolves.toBeUndefined();
await expect(cache1.get(key)).resolves.toEqual(value);
await multi.wrap(key, function_);
await expect(cache0.get(key)).resolves.toEqual(value);
await expect(cache1.get(key)).resolves.toEqual(value);
});
it('#533', () => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
expect(
(async () => {
const cache0 = await caching('memory', {
ttl: 5 * 1000,
refreshThreshold: 4 * 1000,
});
const cache1 = await caching('memory', {
ttl: 10 * 1000,
refreshThreshold: 8 * 1000,
});
const multi = multiCaching([cache0, cache1]);
await multi.wrap('refreshThreshold', async () => 0);
await sleep(2 * 1000);
await multi.wrap('refreshThreshold', async () => 1);
await sleep(500);
await multi.wrap('refreshThreshold', async () => 2);
await sleep(500);
return multi.wrap('refreshThreshold', async () => 3);
})(),
).resolves.toEqual(1);
});
});
});