cache-manager 3.3.0 → 3.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/History.md +12 -0
- package/lib/multi_caching.js +5 -1
- package/package.json +16 -11
- package/.coveralls.yml +0 -1
- package/.eslintrc +0 -61
- package/.nycrc.json +0 -20
- package/.travis.yml +0 -10
- package/Makefile +0 -32
- package/test/caching.unit.js +0 -1587
- package/test/mocha.opts +0 -5
- package/test/multi_caching.unit.js +0 -2195
- package/test/run.js +0 -73
- package/test/stores/memory.unit.js +0 -276
- package/test/stores/none.unit.js +0 -148
- package/test/support.js +0 -115
- package/test/utils.unit.js +0 -24
|
@@ -1,2195 +0,0 @@
|
|
|
1
|
-
var assert = require('assert');
|
|
2
|
-
var async = require('async');
|
|
3
|
-
var sinon = require('sinon');
|
|
4
|
-
var support = require('./support');
|
|
5
|
-
var checkErr = support.checkErr;
|
|
6
|
-
var caching = require('../index').caching;
|
|
7
|
-
var multiCaching = require('../index').multiCaching;
|
|
8
|
-
var memoryStore = require('../lib/stores/memory');
|
|
9
|
-
|
|
10
|
-
var Promise = require('es6-promise').Promise;
|
|
11
|
-
|
|
12
|
-
var methods = {
|
|
13
|
-
getWidget: function(name, cb) {
|
|
14
|
-
process.nextTick(function() {
|
|
15
|
-
cb(null, {name: name});
|
|
16
|
-
});
|
|
17
|
-
},
|
|
18
|
-
getMultiWidget: function(names, cb) {
|
|
19
|
-
process.nextTick(function() {
|
|
20
|
-
cb(null, names.map(function(name) { return {name: name}; }));
|
|
21
|
-
});
|
|
22
|
-
},
|
|
23
|
-
getTTL: function(data, store) {
|
|
24
|
-
if (store === 'memory') {
|
|
25
|
-
return 444;
|
|
26
|
-
}
|
|
27
|
-
return 777;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
describe("multiCaching", function() {
|
|
32
|
-
var memoryCache;
|
|
33
|
-
var memoryCache2;
|
|
34
|
-
var memoryCache3;
|
|
35
|
-
var multiCache;
|
|
36
|
-
var key;
|
|
37
|
-
var key2;
|
|
38
|
-
var memoryTtl;
|
|
39
|
-
var name;
|
|
40
|
-
var name2;
|
|
41
|
-
var defaultTtl;
|
|
42
|
-
|
|
43
|
-
beforeEach(function() {
|
|
44
|
-
memoryTtl = 0.1;
|
|
45
|
-
defaultTtl = 5;
|
|
46
|
-
|
|
47
|
-
memoryCache = caching({store: 'memory', ttl: memoryTtl, promiseDependency: Promise});
|
|
48
|
-
memoryCache2 = caching({store: 'memory', ttl: memoryTtl, promiseDependency: Promise});
|
|
49
|
-
memoryCache3 = caching({store: 'memory', ttl: memoryTtl, promiseDependency: Promise});
|
|
50
|
-
|
|
51
|
-
key = support.random.string(20);
|
|
52
|
-
key2 = support.random.string(20);
|
|
53
|
-
name = support.random.string();
|
|
54
|
-
name2 = support.random.string();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe("get(), set(), del(), reset(), mget(), mset()", function() {
|
|
58
|
-
var value;
|
|
59
|
-
var value2;
|
|
60
|
-
|
|
61
|
-
beforeEach(function() {
|
|
62
|
-
multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]);
|
|
63
|
-
key = support.random.string(20);
|
|
64
|
-
key2 = support.random.string(20);
|
|
65
|
-
value = support.random.string();
|
|
66
|
-
value2 = support.random.string();
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
describe("set()", function() {
|
|
70
|
-
it("lets us set data in all caches", function(done) {
|
|
71
|
-
multiCache.set(key, value, {ttl: defaultTtl}, function(err) {
|
|
72
|
-
checkErr(err);
|
|
73
|
-
|
|
74
|
-
memoryCache.get(key, function(err, result) {
|
|
75
|
-
checkErr(err);
|
|
76
|
-
assert.equal(result, value);
|
|
77
|
-
|
|
78
|
-
memoryCache2.get(key, function(err, result) {
|
|
79
|
-
checkErr(err);
|
|
80
|
-
assert.equal(result, value);
|
|
81
|
-
|
|
82
|
-
memoryCache3.get(key, function(err, result) {
|
|
83
|
-
checkErr(err);
|
|
84
|
-
assert.equal(result, value);
|
|
85
|
-
done();
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it("lets us set data ('Object' type) in all caches", function(done) {
|
|
93
|
-
value = {name: support.random.string()};
|
|
94
|
-
multiCache.set(key, value, function(err) {
|
|
95
|
-
checkErr(err);
|
|
96
|
-
|
|
97
|
-
memoryCache.get(key, function(err, result) {
|
|
98
|
-
checkErr(err);
|
|
99
|
-
assert.deepEqual(result, value);
|
|
100
|
-
|
|
101
|
-
memoryCache2.get(key, function(err, result) {
|
|
102
|
-
checkErr(err);
|
|
103
|
-
assert.deepEqual(result, value);
|
|
104
|
-
|
|
105
|
-
memoryCache3.get(key, function(err, result) {
|
|
106
|
-
checkErr(err);
|
|
107
|
-
assert.deepEqual(result, value);
|
|
108
|
-
done();
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("lets us set data without a callback", function(done) {
|
|
116
|
-
multiCache.set(key, value, {ttl: defaultTtl});
|
|
117
|
-
setTimeout(function() {
|
|
118
|
-
multiCache.get(key, function(err, result) {
|
|
119
|
-
checkErr(err);
|
|
120
|
-
assert.equal(result, value);
|
|
121
|
-
|
|
122
|
-
memoryCache.get(key, function(err, result) {
|
|
123
|
-
checkErr(err);
|
|
124
|
-
assert.equal(result, value);
|
|
125
|
-
|
|
126
|
-
memoryCache2.get(key, function(err, result) {
|
|
127
|
-
checkErr(err);
|
|
128
|
-
assert.equal(result, value);
|
|
129
|
-
|
|
130
|
-
memoryCache3.get(key, function(err, result) {
|
|
131
|
-
checkErr(err);
|
|
132
|
-
assert.equal(result, value);
|
|
133
|
-
done();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
}, 20);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it("lets us set data without an options param", function(done) {
|
|
142
|
-
multiCache.set(key, value, function(err) {
|
|
143
|
-
checkErr(err);
|
|
144
|
-
|
|
145
|
-
multiCache.get(key, function(err, result) {
|
|
146
|
-
checkErr(err);
|
|
147
|
-
assert.equal(result, value);
|
|
148
|
-
|
|
149
|
-
memoryCache.get(key, function(err, result) {
|
|
150
|
-
checkErr(err);
|
|
151
|
-
assert.equal(result, value);
|
|
152
|
-
|
|
153
|
-
memoryCache2.get(key, function(err, result) {
|
|
154
|
-
checkErr(err);
|
|
155
|
-
assert.equal(result, value);
|
|
156
|
-
|
|
157
|
-
memoryCache3.get(key, function(err, result) {
|
|
158
|
-
checkErr(err);
|
|
159
|
-
assert.equal(result, value);
|
|
160
|
-
done();
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it("lets us set data without options or callback", function(done) {
|
|
169
|
-
multiCache.set(key, value);
|
|
170
|
-
setTimeout(function() {
|
|
171
|
-
multiCache.get(key, function(err, result) {
|
|
172
|
-
checkErr(err);
|
|
173
|
-
assert.equal(result, value);
|
|
174
|
-
|
|
175
|
-
memoryCache.get(key, function(err, result) {
|
|
176
|
-
checkErr(err);
|
|
177
|
-
assert.equal(result, value);
|
|
178
|
-
|
|
179
|
-
memoryCache2.get(key, function(err, result) {
|
|
180
|
-
checkErr(err);
|
|
181
|
-
assert.equal(result, value);
|
|
182
|
-
|
|
183
|
-
memoryCache3.get(key, function(err, result) {
|
|
184
|
-
checkErr(err);
|
|
185
|
-
assert.equal(result, value);
|
|
186
|
-
done();
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
}, 20);
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
describe("mset()", function() {
|
|
196
|
-
it("lets us set data in all caches", function(done) {
|
|
197
|
-
multiCache.mset(key, value, key2, value2, {ttl: defaultTtl}, function(err) {
|
|
198
|
-
checkErr(err);
|
|
199
|
-
|
|
200
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
201
|
-
checkErr(err);
|
|
202
|
-
assert.equal(result[0], value);
|
|
203
|
-
assert.equal(result[1], value2);
|
|
204
|
-
|
|
205
|
-
memoryCache2.mget(key, key2, function(err, result) {
|
|
206
|
-
checkErr(err);
|
|
207
|
-
assert.equal(result[0], value);
|
|
208
|
-
assert.equal(result[1], value2);
|
|
209
|
-
|
|
210
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
211
|
-
checkErr(err);
|
|
212
|
-
assert.equal(result[0], value);
|
|
213
|
-
assert.equal(result[1], value2);
|
|
214
|
-
done();
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it("lets us set data ('Object' type) in all caches", function(done) {
|
|
222
|
-
value2 = {name: support.random.string()};
|
|
223
|
-
multiCache.mset(key, value, key2, value2, function(err) {
|
|
224
|
-
checkErr(err);
|
|
225
|
-
|
|
226
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
227
|
-
checkErr(err);
|
|
228
|
-
assert.equal(result[0], value);
|
|
229
|
-
assert.equal(result[1], value2);
|
|
230
|
-
|
|
231
|
-
memoryCache2.mget(key, key2, function(err, result) {
|
|
232
|
-
checkErr(err);
|
|
233
|
-
assert.equal(result[0], value);
|
|
234
|
-
assert.equal(result[1], value2);
|
|
235
|
-
|
|
236
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
237
|
-
checkErr(err);
|
|
238
|
-
assert.equal(result[0], value);
|
|
239
|
-
assert.equal(result[1], value2);
|
|
240
|
-
done();
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it("lets us set data without a callback", function(done) {
|
|
248
|
-
multiCache.mset(key, value, key2, value2, {ttl: defaultTtl});
|
|
249
|
-
setTimeout(function() {
|
|
250
|
-
multiCache.mget(key, key2, function(err, result) {
|
|
251
|
-
checkErr(err);
|
|
252
|
-
assert.equal(result[0], value);
|
|
253
|
-
assert.equal(result[1], value2);
|
|
254
|
-
|
|
255
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
256
|
-
checkErr(err);
|
|
257
|
-
assert.equal(result[0], value);
|
|
258
|
-
assert.equal(result[1], value2);
|
|
259
|
-
|
|
260
|
-
memoryCache2.mget(key, key2, function(err, result) {
|
|
261
|
-
checkErr(err);
|
|
262
|
-
assert.equal(result[0], value);
|
|
263
|
-
assert.equal(result[1], value2);
|
|
264
|
-
|
|
265
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
266
|
-
checkErr(err);
|
|
267
|
-
assert.equal(result[0], value);
|
|
268
|
-
assert.equal(result[1], value2);
|
|
269
|
-
done();
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
}, 20);
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
it("lets us set data without an options param", function(done) {
|
|
278
|
-
multiCache.mset(key, value, key2, value2, function(err) {
|
|
279
|
-
checkErr(err);
|
|
280
|
-
|
|
281
|
-
multiCache.mget(key, key2, function(err, result) {
|
|
282
|
-
checkErr(err);
|
|
283
|
-
assert.equal(result[0], value);
|
|
284
|
-
assert.equal(result[1], value2);
|
|
285
|
-
|
|
286
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
287
|
-
checkErr(err);
|
|
288
|
-
assert.equal(result[0], value);
|
|
289
|
-
assert.equal(result[1], value2);
|
|
290
|
-
|
|
291
|
-
memoryCache2.mget(key, key2, function(err, result) {
|
|
292
|
-
checkErr(err);
|
|
293
|
-
assert.equal(result[0], value);
|
|
294
|
-
assert.equal(result[1], value2);
|
|
295
|
-
|
|
296
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
297
|
-
checkErr(err);
|
|
298
|
-
assert.equal(result[0], value);
|
|
299
|
-
assert.equal(result[1], value2);
|
|
300
|
-
done();
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
it("lets us set data without options or callback", function(done) {
|
|
309
|
-
multiCache.set(key, value);
|
|
310
|
-
setTimeout(function() {
|
|
311
|
-
multiCache.get(key, function(err, result) {
|
|
312
|
-
checkErr(err);
|
|
313
|
-
assert.equal(result, value);
|
|
314
|
-
|
|
315
|
-
memoryCache.get(key, function(err, result) {
|
|
316
|
-
checkErr(err);
|
|
317
|
-
assert.equal(result, value);
|
|
318
|
-
|
|
319
|
-
memoryCache2.get(key, function(err, result) {
|
|
320
|
-
checkErr(err);
|
|
321
|
-
assert.equal(result, value);
|
|
322
|
-
|
|
323
|
-
memoryCache3.get(key, function(err, result) {
|
|
324
|
-
checkErr(err);
|
|
325
|
-
assert.equal(result, value);
|
|
326
|
-
done();
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
});
|
|
331
|
-
}, 20);
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
describe("get()", function() {
|
|
336
|
-
it("gets data from first cache that has it", function(done) {
|
|
337
|
-
memoryCache3.set(key, value, function(err) {
|
|
338
|
-
checkErr(err);
|
|
339
|
-
|
|
340
|
-
multiCache.get(key, function(err, result) {
|
|
341
|
-
checkErr(err);
|
|
342
|
-
assert.equal(result, value);
|
|
343
|
-
done();
|
|
344
|
-
});
|
|
345
|
-
});
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
it("passes any options to underlying caches", function(done) {
|
|
349
|
-
multiCache.set(key, value, function(err) {
|
|
350
|
-
checkErr(err);
|
|
351
|
-
|
|
352
|
-
sinon.spy(memoryCache.store, 'get');
|
|
353
|
-
|
|
354
|
-
var opts = {foo: 'bar'};
|
|
355
|
-
|
|
356
|
-
multiCache.get(key, opts, function(err, result) {
|
|
357
|
-
checkErr(err);
|
|
358
|
-
|
|
359
|
-
assert.equal(result, value);
|
|
360
|
-
assert.ok(memoryCache.store.get.calledWith(key, opts));
|
|
361
|
-
|
|
362
|
-
memoryCache.store.get.restore();
|
|
363
|
-
done();
|
|
364
|
-
});
|
|
365
|
-
});
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
describe('using promises', function() {
|
|
369
|
-
it('gets data from first cache that has it', function(done) {
|
|
370
|
-
memoryCache3.set(key, value)
|
|
371
|
-
.then(function() {
|
|
372
|
-
return multiCache.get(key);
|
|
373
|
-
})
|
|
374
|
-
.then(function(result) {
|
|
375
|
-
assert.equal(result, value);
|
|
376
|
-
})
|
|
377
|
-
.then(done);
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
it("passes any options to underlying caches", function(done) {
|
|
381
|
-
var opts = {foo: 'bar'};
|
|
382
|
-
|
|
383
|
-
multiCache.set(key, value)
|
|
384
|
-
.then(function() {
|
|
385
|
-
sinon.spy(memoryCache.store, 'get');
|
|
386
|
-
return multiCache.get(key, opts);
|
|
387
|
-
})
|
|
388
|
-
.then(function(result) {
|
|
389
|
-
assert.equal(result, value);
|
|
390
|
-
assert.ok(memoryCache.store.get.calledWith(key, opts));
|
|
391
|
-
|
|
392
|
-
memoryCache.store.get.restore();
|
|
393
|
-
})
|
|
394
|
-
.then(done);
|
|
395
|
-
});
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
describe("mget()", function() {
|
|
400
|
-
it("gets data from first cache that has it (1)", function(done) {
|
|
401
|
-
memoryCache3.mset(key, value, key2, value2, function(err) {
|
|
402
|
-
checkErr(err);
|
|
403
|
-
|
|
404
|
-
multiCache.mget(key, key2, function(err, result) {
|
|
405
|
-
checkErr(err);
|
|
406
|
-
assert.equal(result[0], value);
|
|
407
|
-
assert.equal(result[1], value2);
|
|
408
|
-
done();
|
|
409
|
-
});
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
it("gets data from first cache that has it (2)", function(done) {
|
|
414
|
-
var key3 = support.random.string(20);
|
|
415
|
-
var value3 = support.random.string();
|
|
416
|
-
memoryCache.mset(key3, value3);
|
|
417
|
-
memoryCache2.mset(key, value);
|
|
418
|
-
memoryCache3.mset(key2, value2);
|
|
419
|
-
|
|
420
|
-
setTimeout(function() {
|
|
421
|
-
multiCache.mget(key, key2, key3, function(err, result) {
|
|
422
|
-
checkErr(err);
|
|
423
|
-
assert.equal(result[0], value);
|
|
424
|
-
assert.equal(result[1], value2);
|
|
425
|
-
assert.equal(result[2], value3);
|
|
426
|
-
done();
|
|
427
|
-
});
|
|
428
|
-
}, 20);
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
it("gets data from first cache that has it (3)", function(done) {
|
|
432
|
-
var key3 = support.random.string(20);
|
|
433
|
-
memoryCache2.mset(key, value);
|
|
434
|
-
|
|
435
|
-
setTimeout(function() {
|
|
436
|
-
multiCache.mget(key2, key, key3, function(err, result) {
|
|
437
|
-
checkErr(err);
|
|
438
|
-
assert.equal(result[0], null);
|
|
439
|
-
assert.equal(result[1], value);
|
|
440
|
-
assert.equal(result[2], null);
|
|
441
|
-
done();
|
|
442
|
-
});
|
|
443
|
-
}, 20);
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
it("passes any options to underlying caches", function(done) {
|
|
447
|
-
multiCache.mset(key, value, key2, value2, function(err) {
|
|
448
|
-
checkErr(err);
|
|
449
|
-
|
|
450
|
-
sinon.spy(memoryCache.store, 'mget');
|
|
451
|
-
|
|
452
|
-
var opts = {foo: 'bar'};
|
|
453
|
-
|
|
454
|
-
multiCache.mget(key, key2, opts, function(err, result) {
|
|
455
|
-
checkErr(err);
|
|
456
|
-
|
|
457
|
-
assert.equal(result[0], value);
|
|
458
|
-
assert.equal(result[1], value2);
|
|
459
|
-
assert.ok(memoryCache.store.mget.calledWith(key, key2, opts));
|
|
460
|
-
|
|
461
|
-
memoryCache.store.mget.restore();
|
|
462
|
-
done();
|
|
463
|
-
});
|
|
464
|
-
});
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
describe('using promises', function() {
|
|
468
|
-
it('gets data from first cache that has it', function(done) {
|
|
469
|
-
memoryCache3.mset(key, value, key2, value2)
|
|
470
|
-
.then(function() {
|
|
471
|
-
return multiCache.mget(key, key2);
|
|
472
|
-
})
|
|
473
|
-
.then(function(result) {
|
|
474
|
-
assert.equal(result[0], value);
|
|
475
|
-
assert.equal(result[1], value2);
|
|
476
|
-
})
|
|
477
|
-
.then(done);
|
|
478
|
-
});
|
|
479
|
-
|
|
480
|
-
it("passes any options to underlying caches", function(done) {
|
|
481
|
-
var opts = {foo: 'bar'};
|
|
482
|
-
|
|
483
|
-
multiCache.mset(key, value, key2, value2)
|
|
484
|
-
.then(function() {
|
|
485
|
-
sinon.spy(memoryCache.store, 'mget');
|
|
486
|
-
return multiCache.mget(key, key2, opts);
|
|
487
|
-
})
|
|
488
|
-
.then(function(result) {
|
|
489
|
-
assert.equal(result[0], value);
|
|
490
|
-
assert.equal(result[1], value2);
|
|
491
|
-
assert.ok(memoryCache.store.mget.calledWith(key, key2, opts));
|
|
492
|
-
|
|
493
|
-
memoryCache.store.mget.restore();
|
|
494
|
-
})
|
|
495
|
-
.then(done);
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
describe("del()", function() {
|
|
501
|
-
it("lets us delete data in all caches", function(done) {
|
|
502
|
-
multiCache.set(key, value, function(err) {
|
|
503
|
-
checkErr(err);
|
|
504
|
-
|
|
505
|
-
multiCache.del(key, function(err) {
|
|
506
|
-
checkErr(err);
|
|
507
|
-
|
|
508
|
-
memoryCache.get(key, function(err, result) {
|
|
509
|
-
assert.ok(!result);
|
|
510
|
-
|
|
511
|
-
memoryCache2.get(key, function(err, result) {
|
|
512
|
-
checkErr(err);
|
|
513
|
-
assert.ok(!result);
|
|
514
|
-
|
|
515
|
-
memoryCache3.get(key, function(err, result) {
|
|
516
|
-
checkErr(err);
|
|
517
|
-
assert.ok(!result);
|
|
518
|
-
done();
|
|
519
|
-
});
|
|
520
|
-
});
|
|
521
|
-
});
|
|
522
|
-
});
|
|
523
|
-
});
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
it("lets us delete multiple keys in all caches (Args)", function(done) {
|
|
527
|
-
multiCache.mset(key, value, key2, value2, function(err) {
|
|
528
|
-
checkErr(err);
|
|
529
|
-
|
|
530
|
-
multiCache.del(key, key2, function(err) {
|
|
531
|
-
checkErr(err);
|
|
532
|
-
|
|
533
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
534
|
-
assert.ok(!result[0]);
|
|
535
|
-
assert.ok(!result[1]);
|
|
536
|
-
|
|
537
|
-
memoryCache2.mget(key, key2, function(err, result) {
|
|
538
|
-
checkErr(err);
|
|
539
|
-
assert.ok(!result[0]);
|
|
540
|
-
assert.ok(!result[1]);
|
|
541
|
-
|
|
542
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
543
|
-
checkErr(err);
|
|
544
|
-
assert.ok(!result[0]);
|
|
545
|
-
assert.ok(!result[1]);
|
|
546
|
-
done();
|
|
547
|
-
});
|
|
548
|
-
});
|
|
549
|
-
});
|
|
550
|
-
});
|
|
551
|
-
});
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
it("lets us delete multiple keys in all caches (Array)", function(done) {
|
|
555
|
-
multiCache.mset(key, value, key2, value2, function(err) {
|
|
556
|
-
checkErr(err);
|
|
557
|
-
|
|
558
|
-
multiCache.del([key, key2], function(err) {
|
|
559
|
-
checkErr(err);
|
|
560
|
-
|
|
561
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
562
|
-
assert.ok(!result[0]);
|
|
563
|
-
assert.ok(!result[1]);
|
|
564
|
-
|
|
565
|
-
memoryCache2.mget(key, key2, function(err, result) {
|
|
566
|
-
checkErr(err);
|
|
567
|
-
assert.ok(!result[0]);
|
|
568
|
-
assert.ok(!result[1]);
|
|
569
|
-
|
|
570
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
571
|
-
checkErr(err);
|
|
572
|
-
assert.ok(!result[0]);
|
|
573
|
-
assert.ok(!result[1]);
|
|
574
|
-
done();
|
|
575
|
-
});
|
|
576
|
-
});
|
|
577
|
-
});
|
|
578
|
-
});
|
|
579
|
-
});
|
|
580
|
-
});
|
|
581
|
-
|
|
582
|
-
it("lets us delete data without a callback", function(done) {
|
|
583
|
-
multiCache.set(key, value, function(err) {
|
|
584
|
-
checkErr(err);
|
|
585
|
-
|
|
586
|
-
multiCache.del(key);
|
|
587
|
-
|
|
588
|
-
setTimeout(function() {
|
|
589
|
-
memoryCache.get(key, function(err, result) {
|
|
590
|
-
checkErr(err);
|
|
591
|
-
assert.ok(!result);
|
|
592
|
-
|
|
593
|
-
memoryCache2.get(key, function(err, result) {
|
|
594
|
-
checkErr(err);
|
|
595
|
-
assert.ok(!result);
|
|
596
|
-
|
|
597
|
-
memoryCache3.get(key, function(err, result) {
|
|
598
|
-
checkErr(err);
|
|
599
|
-
assert.ok(!result);
|
|
600
|
-
done();
|
|
601
|
-
});
|
|
602
|
-
});
|
|
603
|
-
});
|
|
604
|
-
}, 10);
|
|
605
|
-
});
|
|
606
|
-
});
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
describe("reset()", function() {
|
|
610
|
-
it("resets all caches", function(done) {
|
|
611
|
-
multiCache.set(key, value, function(err) {
|
|
612
|
-
checkErr(err);
|
|
613
|
-
|
|
614
|
-
multiCache.reset(function() {
|
|
615
|
-
memoryCache.get(key, function(err, result) {
|
|
616
|
-
checkErr(err);
|
|
617
|
-
assert.ok(!result);
|
|
618
|
-
|
|
619
|
-
memoryCache2.get(key, function(err, result) {
|
|
620
|
-
checkErr(err);
|
|
621
|
-
assert.ok(!result);
|
|
622
|
-
|
|
623
|
-
memoryCache3.get(key, function(err, result) {
|
|
624
|
-
checkErr(err);
|
|
625
|
-
assert.ok(!result);
|
|
626
|
-
done();
|
|
627
|
-
});
|
|
628
|
-
});
|
|
629
|
-
});
|
|
630
|
-
});
|
|
631
|
-
});
|
|
632
|
-
});
|
|
633
|
-
});
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
describe("getAndPassUp()", function() {
|
|
637
|
-
var value;
|
|
638
|
-
var key;
|
|
639
|
-
|
|
640
|
-
describe("using a single cache store", function() {
|
|
641
|
-
beforeEach(function() {
|
|
642
|
-
multiCache = multiCaching([memoryCache3]);
|
|
643
|
-
key = support.random.string(20);
|
|
644
|
-
value = support.random.string();
|
|
645
|
-
});
|
|
646
|
-
|
|
647
|
-
it("gets data from first cache that has it", function(done) {
|
|
648
|
-
memoryCache3.set(key, value, function(err) {
|
|
649
|
-
checkErr(err);
|
|
650
|
-
|
|
651
|
-
multiCache.getAndPassUp(key, function(err, result) {
|
|
652
|
-
checkErr(err);
|
|
653
|
-
assert.equal(result, value);
|
|
654
|
-
done();
|
|
655
|
-
});
|
|
656
|
-
});
|
|
657
|
-
});
|
|
658
|
-
|
|
659
|
-
it("gets data from first cache that has it using promises", function(done) {
|
|
660
|
-
memoryCache3.set(key, value)
|
|
661
|
-
.then(function() {
|
|
662
|
-
return multiCache.getAndPassUp(key);
|
|
663
|
-
})
|
|
664
|
-
.then(function(result) {
|
|
665
|
-
assert.equal(result, value);
|
|
666
|
-
done();
|
|
667
|
-
});
|
|
668
|
-
});
|
|
669
|
-
});
|
|
670
|
-
|
|
671
|
-
describe("when value is not found in any cache", function() {
|
|
672
|
-
var response;
|
|
673
|
-
|
|
674
|
-
beforeEach(function(done) {
|
|
675
|
-
key = support.random.string(10);
|
|
676
|
-
sinon.spy(memoryCache, 'set');
|
|
677
|
-
sinon.spy(memoryCache2, 'set');
|
|
678
|
-
sinon.spy(memoryCache3, 'set');
|
|
679
|
-
|
|
680
|
-
multiCache.getAndPassUp(key, function(err, result) {
|
|
681
|
-
checkErr(err);
|
|
682
|
-
response = result;
|
|
683
|
-
done();
|
|
684
|
-
});
|
|
685
|
-
});
|
|
686
|
-
|
|
687
|
-
afterEach(function() {
|
|
688
|
-
memoryCache.set.restore();
|
|
689
|
-
memoryCache2.set.restore();
|
|
690
|
-
memoryCache3.set.restore();
|
|
691
|
-
});
|
|
692
|
-
|
|
693
|
-
it("calls back with undefined", function() {
|
|
694
|
-
assert.strictEqual(response, undefined);
|
|
695
|
-
});
|
|
696
|
-
|
|
697
|
-
it("does not set anything in caches", function(done) {
|
|
698
|
-
process.nextTick(function() {
|
|
699
|
-
assert.ok(memoryCache.set.notCalled);
|
|
700
|
-
assert.ok(memoryCache2.set.notCalled);
|
|
701
|
-
assert.ok(memoryCache3.set.notCalled);
|
|
702
|
-
done();
|
|
703
|
-
});
|
|
704
|
-
});
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
describe("using multi cache store", function() {
|
|
708
|
-
beforeEach(function() {
|
|
709
|
-
multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]);
|
|
710
|
-
key = support.random.string(20);
|
|
711
|
-
value = support.random.string();
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
it("checks to see if higher levels have item", function(done) {
|
|
715
|
-
memoryCache3.set(key, value, function(err) {
|
|
716
|
-
checkErr(err);
|
|
717
|
-
|
|
718
|
-
multiCache.getAndPassUp(key, function(err, result) {
|
|
719
|
-
checkErr(err);
|
|
720
|
-
assert.equal(result, value);
|
|
721
|
-
|
|
722
|
-
process.nextTick(function() {
|
|
723
|
-
memoryCache.get(key, function(err, result) {
|
|
724
|
-
assert.equal(result, value);
|
|
725
|
-
checkErr(err);
|
|
726
|
-
done();
|
|
727
|
-
});
|
|
728
|
-
});
|
|
729
|
-
});
|
|
730
|
-
});
|
|
731
|
-
});
|
|
732
|
-
|
|
733
|
-
it("checks to see if higher levels have item using promises", function(done) {
|
|
734
|
-
memoryCache3.set(key, value)
|
|
735
|
-
.then(function() {
|
|
736
|
-
return multiCache.getAndPassUp(key);
|
|
737
|
-
})
|
|
738
|
-
.then(function(result) {
|
|
739
|
-
assert.equal(result, value);
|
|
740
|
-
})
|
|
741
|
-
.then(function() {
|
|
742
|
-
process.nextTick(function() {
|
|
743
|
-
memoryCache.get(key)
|
|
744
|
-
.then(function(fetchedValue) {
|
|
745
|
-
assert.equal(fetchedValue, value);
|
|
746
|
-
});
|
|
747
|
-
});
|
|
748
|
-
})
|
|
749
|
-
.then(done);
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
context("when a cache store calls back with an error", function() {
|
|
753
|
-
var fakeError;
|
|
754
|
-
var memoryStoreStub;
|
|
755
|
-
|
|
756
|
-
beforeEach(function() {
|
|
757
|
-
memoryStoreStub = memoryStore.create({ttl: defaultTtl});
|
|
758
|
-
sinon.stub(memoryStore, 'create').returns(memoryStoreStub);
|
|
759
|
-
memoryCache = caching({store: 'memory', ttl: defaultTtl});
|
|
760
|
-
multiCache = multiCaching([memoryCache]);
|
|
761
|
-
fakeError = new Error(support.random.string());
|
|
762
|
-
sinon.stub(memoryStoreStub, 'get').yields(fakeError);
|
|
763
|
-
});
|
|
764
|
-
|
|
765
|
-
afterEach(function() {
|
|
766
|
-
memoryStore.create.restore();
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
it("bubbles up errors from caches", function(done) {
|
|
770
|
-
multiCache.getAndPassUp(key, function(err) {
|
|
771
|
-
assert.ok(memoryStoreStub.get.called);
|
|
772
|
-
assert.equal(err, fakeError);
|
|
773
|
-
done();
|
|
774
|
-
});
|
|
775
|
-
});
|
|
776
|
-
|
|
777
|
-
it("bubbles up errors from caches and reject promise", function(done) {
|
|
778
|
-
multiCache.getAndPassUp(key)
|
|
779
|
-
.catch(function(err) {
|
|
780
|
-
assert.ok(memoryStoreStub.get.called);
|
|
781
|
-
assert.equal(err, fakeError);
|
|
782
|
-
done();
|
|
783
|
-
});
|
|
784
|
-
});
|
|
785
|
-
});
|
|
786
|
-
});
|
|
787
|
-
});
|
|
788
|
-
|
|
789
|
-
describe("wrap()", function() {
|
|
790
|
-
describe("using a single cache store", function() {
|
|
791
|
-
beforeEach(function() {
|
|
792
|
-
multiCache = multiCaching([memoryCache3]);
|
|
793
|
-
});
|
|
794
|
-
|
|
795
|
-
context("calls back with the result of a function", function() {
|
|
796
|
-
beforeEach(function() {
|
|
797
|
-
sinon.spy(memoryCache3.store, 'set');
|
|
798
|
-
sinon.spy(memoryCache3.store, 'mset');
|
|
799
|
-
});
|
|
800
|
-
|
|
801
|
-
afterEach(function() {
|
|
802
|
-
memoryCache3.store.set.restore();
|
|
803
|
-
memoryCache3.store.mset.restore();
|
|
804
|
-
});
|
|
805
|
-
|
|
806
|
-
/**
|
|
807
|
-
* Note: it's up to the underlying cache implementation to handle the ttl number.
|
|
808
|
-
* We're just testing that the ttl gets passed to the underlying store.
|
|
809
|
-
*/
|
|
810
|
-
it('when a ttl number is passed in', function(done) {
|
|
811
|
-
multiCache.wrap(key, function(cb) {
|
|
812
|
-
methods.getWidget(name, cb);
|
|
813
|
-
}, {ttl: defaultTtl}, function(err, widget) {
|
|
814
|
-
checkErr(err);
|
|
815
|
-
assert.deepEqual(widget, {name: name});
|
|
816
|
-
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, {ttl: defaultTtl});
|
|
817
|
-
done();
|
|
818
|
-
});
|
|
819
|
-
});
|
|
820
|
-
|
|
821
|
-
it('when a ttl option is passed in', function(done) {
|
|
822
|
-
multiCache.wrap(key, function(cb) {
|
|
823
|
-
methods.getWidget(name, cb);
|
|
824
|
-
}, {ttl: defaultTtl}, function(err, widget) {
|
|
825
|
-
checkErr(err);
|
|
826
|
-
assert.deepEqual(widget, {name: name});
|
|
827
|
-
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, {ttl: defaultTtl});
|
|
828
|
-
|
|
829
|
-
done();
|
|
830
|
-
});
|
|
831
|
-
});
|
|
832
|
-
|
|
833
|
-
it('when no options are passed in', function(done) {
|
|
834
|
-
multiCache.wrap(key, function(cb) {
|
|
835
|
-
methods.getWidget(name, cb);
|
|
836
|
-
}, function(err, widget) {
|
|
837
|
-
checkErr(err);
|
|
838
|
-
assert.deepEqual(widget, {name: name});
|
|
839
|
-
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name});
|
|
840
|
-
done();
|
|
841
|
-
});
|
|
842
|
-
});
|
|
843
|
-
|
|
844
|
-
it('when multiple keys are passed', function(done) {
|
|
845
|
-
multiCache.wrap(key, key2, function(cb) {
|
|
846
|
-
methods.getMultiWidget([name, name2], cb);
|
|
847
|
-
}, function(err, widgets) {
|
|
848
|
-
checkErr(err);
|
|
849
|
-
assert.deepEqual(widgets[0], {name: name});
|
|
850
|
-
assert.deepEqual(widgets[1], {name: name2});
|
|
851
|
-
sinon.assert.calledWith(memoryCache3.store.mset, key, {name: name}, key2, {name: name2});
|
|
852
|
-
done();
|
|
853
|
-
});
|
|
854
|
-
});
|
|
855
|
-
});
|
|
856
|
-
|
|
857
|
-
context("when wrapped function calls back with an error", function() {
|
|
858
|
-
it("calls back with that error", function(done) {
|
|
859
|
-
var fakeError = new Error(support.random.string());
|
|
860
|
-
sinon.stub(methods, 'getWidget').yields(fakeError, {name: name});
|
|
861
|
-
|
|
862
|
-
multiCache.wrap(key, function(cb) {
|
|
863
|
-
methods.getWidget(name, cb);
|
|
864
|
-
}, function(err, widget) {
|
|
865
|
-
methods.getWidget.restore();
|
|
866
|
-
assert.equal(err, fakeError);
|
|
867
|
-
assert.ok(!widget);
|
|
868
|
-
done();
|
|
869
|
-
});
|
|
870
|
-
});
|
|
871
|
-
});
|
|
872
|
-
|
|
873
|
-
var falseyValues = [false, null, 0];
|
|
874
|
-
|
|
875
|
-
falseyValues.forEach(function(falseyValue) {
|
|
876
|
-
context("when cached value is `" + falseyValue + "`", function() {
|
|
877
|
-
function getFalseyValue(cb) {
|
|
878
|
-
process.nextTick(function() {
|
|
879
|
-
cb(null, falseyValue);
|
|
880
|
-
});
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
function getCachedFalseyValue(cb) {
|
|
884
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
885
|
-
getFalseyValue(cacheCb);
|
|
886
|
-
}, cb);
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
beforeEach(function(done) {
|
|
890
|
-
multiCache = multiCaching([memoryCache3]);
|
|
891
|
-
sinon.spy(memoryCache3.store, 'set');
|
|
892
|
-
|
|
893
|
-
getCachedFalseyValue(function(err, result) {
|
|
894
|
-
checkErr(err);
|
|
895
|
-
assert.strictEqual(result, falseyValue);
|
|
896
|
-
|
|
897
|
-
memoryCache3.get(key, function(err, result) {
|
|
898
|
-
checkErr(err);
|
|
899
|
-
assert.strictEqual(result, falseyValue);
|
|
900
|
-
|
|
901
|
-
sinon.spy(memoryCache3.store, 'get');
|
|
902
|
-
|
|
903
|
-
done();
|
|
904
|
-
});
|
|
905
|
-
});
|
|
906
|
-
});
|
|
907
|
-
|
|
908
|
-
afterEach(function() {
|
|
909
|
-
memoryCache3.store.set.restore();
|
|
910
|
-
memoryCache3.store.get.restore();
|
|
911
|
-
});
|
|
912
|
-
|
|
913
|
-
it("sets data in and retrieves data from cache", function(done) {
|
|
914
|
-
getCachedFalseyValue(function(err, value) {
|
|
915
|
-
checkErr(err);
|
|
916
|
-
assert.strictEqual(value, falseyValue);
|
|
917
|
-
assert.ok(memoryCache3.store.set.calledWith(key));
|
|
918
|
-
assert.ok(memoryCache3.store.get.calledWith(key));
|
|
919
|
-
done();
|
|
920
|
-
});
|
|
921
|
-
});
|
|
922
|
-
});
|
|
923
|
-
});
|
|
924
|
-
|
|
925
|
-
context("when we pass in an isCacheableValue function to the caching constructor", function() {
|
|
926
|
-
var testCallbacks = {
|
|
927
|
-
isCacheableValue: function(value) {
|
|
928
|
-
return value !== 'do_not_store_this' && value !== undefined;
|
|
929
|
-
}
|
|
930
|
-
};
|
|
931
|
-
|
|
932
|
-
function getValue(name, cb) {
|
|
933
|
-
process.nextTick(function() {
|
|
934
|
-
if (name === 'foo') {
|
|
935
|
-
cb(null, 'store_this');
|
|
936
|
-
} else {
|
|
937
|
-
cb(null, 'do_not_store_this');
|
|
938
|
-
}
|
|
939
|
-
});
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
function getCachedValue(name, cb) {
|
|
943
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
944
|
-
getValue(name, function(err, result) {
|
|
945
|
-
cacheCb(err, result);
|
|
946
|
-
});
|
|
947
|
-
}, cb);
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
beforeEach(function() {
|
|
951
|
-
sinon.spy(testCallbacks, 'isCacheableValue');
|
|
952
|
-
multiCache = multiCaching([memoryCache3], {isCacheableValue: testCallbacks.isCacheableValue});
|
|
953
|
-
sinon.spy(memoryCache3.store, 'set');
|
|
954
|
-
});
|
|
955
|
-
|
|
956
|
-
afterEach(function() {
|
|
957
|
-
memoryCache3.store.set.restore();
|
|
958
|
-
testCallbacks.isCacheableValue.restore();
|
|
959
|
-
});
|
|
960
|
-
|
|
961
|
-
it("stores allowed values", function(done) {
|
|
962
|
-
var name = 'foo';
|
|
963
|
-
|
|
964
|
-
getCachedValue(name, function(err) {
|
|
965
|
-
checkErr(err);
|
|
966
|
-
assert.ok(memoryCache3.store.set.called);
|
|
967
|
-
|
|
968
|
-
assert.ok(testCallbacks.isCacheableValue.called);
|
|
969
|
-
|
|
970
|
-
getCachedValue(name, function(err) {
|
|
971
|
-
checkErr(err);
|
|
972
|
-
done();
|
|
973
|
-
});
|
|
974
|
-
});
|
|
975
|
-
});
|
|
976
|
-
|
|
977
|
-
it("does not store non-allowed values", function(done) {
|
|
978
|
-
var name = 'bar';
|
|
979
|
-
|
|
980
|
-
getCachedValue(name, function(err) {
|
|
981
|
-
checkErr(err);
|
|
982
|
-
assert.ok(memoryCache3.store.set.notCalled);
|
|
983
|
-
done();
|
|
984
|
-
});
|
|
985
|
-
});
|
|
986
|
-
});
|
|
987
|
-
|
|
988
|
-
context("when an underlying store has its own isCacheableValue function", function() {
|
|
989
|
-
var memoryCache4;
|
|
990
|
-
|
|
991
|
-
var testCallbacks = {
|
|
992
|
-
isCacheableValue: function(value) {
|
|
993
|
-
var x = value !== 'do_not_store_this' && value !== undefined;
|
|
994
|
-
return x;
|
|
995
|
-
}
|
|
996
|
-
};
|
|
997
|
-
|
|
998
|
-
function getValue(name, cb) {
|
|
999
|
-
process.nextTick(function() {
|
|
1000
|
-
if (name === 'foo') {
|
|
1001
|
-
cb(null, 'store_this');
|
|
1002
|
-
} else {
|
|
1003
|
-
cb(null, 'do_not_store_this');
|
|
1004
|
-
}
|
|
1005
|
-
});
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
function getCachedValue(name, cb) {
|
|
1009
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1010
|
-
getValue(name, function(err, result) {
|
|
1011
|
-
cacheCb(err, result);
|
|
1012
|
-
});
|
|
1013
|
-
}, cb);
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
beforeEach(function() {
|
|
1017
|
-
sinon.spy(testCallbacks, 'isCacheableValue');
|
|
1018
|
-
memoryCache4 = caching({
|
|
1019
|
-
store: 'memory',
|
|
1020
|
-
ttl: memoryTtl
|
|
1021
|
-
});
|
|
1022
|
-
|
|
1023
|
-
// This simulates how node-cache-manager-redis sets its
|
|
1024
|
-
// isCacheableValue function:
|
|
1025
|
-
memoryCache4.store.isCacheableValue = testCallbacks.isCacheableValue;
|
|
1026
|
-
|
|
1027
|
-
multiCache = multiCaching([memoryCache4]);
|
|
1028
|
-
sinon.spy(memoryCache4.store, 'set');
|
|
1029
|
-
});
|
|
1030
|
-
|
|
1031
|
-
afterEach(function() {
|
|
1032
|
-
memoryCache4.store.set.restore();
|
|
1033
|
-
testCallbacks.isCacheableValue.restore();
|
|
1034
|
-
});
|
|
1035
|
-
|
|
1036
|
-
it("stores allowed values", function(done) {
|
|
1037
|
-
var name = 'foo';
|
|
1038
|
-
|
|
1039
|
-
getCachedValue(name, function(err) {
|
|
1040
|
-
checkErr(err);
|
|
1041
|
-
assert.ok(memoryCache4.store.set.called);
|
|
1042
|
-
|
|
1043
|
-
assert.ok(testCallbacks.isCacheableValue.called);
|
|
1044
|
-
|
|
1045
|
-
getCachedValue(name, function(err) {
|
|
1046
|
-
checkErr(err);
|
|
1047
|
-
done();
|
|
1048
|
-
});
|
|
1049
|
-
});
|
|
1050
|
-
});
|
|
1051
|
-
|
|
1052
|
-
it("does not store non-allowed values", function(done) {
|
|
1053
|
-
var name = 'bar';
|
|
1054
|
-
|
|
1055
|
-
getCachedValue(name, function(err) {
|
|
1056
|
-
checkErr(err);
|
|
1057
|
-
assert.ok(memoryCache4.store.set.notCalled);
|
|
1058
|
-
done();
|
|
1059
|
-
});
|
|
1060
|
-
});
|
|
1061
|
-
|
|
1062
|
-
it("allows repeated calls for uncacheable value", function(done) {
|
|
1063
|
-
function getUndefined(name, cb) {
|
|
1064
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1065
|
-
process.nextTick(function() {
|
|
1066
|
-
cacheCb(null, undefined);
|
|
1067
|
-
});
|
|
1068
|
-
}, cb);
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
var name = 'oof';
|
|
1072
|
-
|
|
1073
|
-
getUndefined(name, function(err, val) {
|
|
1074
|
-
checkErr(err);
|
|
1075
|
-
assert.ok(val === undefined);
|
|
1076
|
-
getUndefined(name, function(err, val) {
|
|
1077
|
-
checkErr(err);
|
|
1078
|
-
assert.ok(val === undefined);
|
|
1079
|
-
done();
|
|
1080
|
-
});
|
|
1081
|
-
});
|
|
1082
|
-
});
|
|
1083
|
-
|
|
1084
|
-
it("allows simultaneous calls for uncacheable value", function(done) {
|
|
1085
|
-
var results = [true, undefined];
|
|
1086
|
-
|
|
1087
|
-
function getUndefined(name, cb) {
|
|
1088
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1089
|
-
process.nextTick(function() {
|
|
1090
|
-
cacheCb(null, results.pop());
|
|
1091
|
-
});
|
|
1092
|
-
}, cb);
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
var name = 'oof';
|
|
1096
|
-
|
|
1097
|
-
getUndefined(name, function(err, val) {
|
|
1098
|
-
checkErr(err);
|
|
1099
|
-
assert.ok(val === undefined);
|
|
1100
|
-
});
|
|
1101
|
-
getUndefined(name, function(err, val) {
|
|
1102
|
-
checkErr(err);
|
|
1103
|
-
assert.ok(val === undefined);
|
|
1104
|
-
|
|
1105
|
-
// ensure we didn't cache "undefined"
|
|
1106
|
-
getUndefined(name, function(err, val) {
|
|
1107
|
-
checkErr(err);
|
|
1108
|
-
assert.ok(val === true);
|
|
1109
|
-
done();
|
|
1110
|
-
});
|
|
1111
|
-
});
|
|
1112
|
-
});
|
|
1113
|
-
});
|
|
1114
|
-
|
|
1115
|
-
context("when wrapped function does not return results", function() {
|
|
1116
|
-
it("should not call 'setInMultipleCaches()'", function(done) {
|
|
1117
|
-
sinon.spy(memoryCache3.store, 'mset');
|
|
1118
|
-
|
|
1119
|
-
multiCache.wrap(key, key2, function(cb) {
|
|
1120
|
-
cb(null, [undefined, undefined]);
|
|
1121
|
-
}, function(err, widgets) {
|
|
1122
|
-
checkErr(err);
|
|
1123
|
-
assert.ok(widgets === undefined);
|
|
1124
|
-
sinon.assert.notCalled(memoryCache3.store.mset);
|
|
1125
|
-
memoryCache3.store.mset.restore();
|
|
1126
|
-
done();
|
|
1127
|
-
});
|
|
1128
|
-
});
|
|
1129
|
-
});
|
|
1130
|
-
});
|
|
1131
|
-
|
|
1132
|
-
describe("using a single cache store with refresh handling", function() {
|
|
1133
|
-
var memoryCache4 = caching({store: 'memory', ttl: 10, refreshThreshold: 2, promiseDependency: Promise});
|
|
1134
|
-
|
|
1135
|
-
context("using standard memory store when the result is valid", function() {
|
|
1136
|
-
beforeEach(function(done) {
|
|
1137
|
-
multiCache = multiCaching([memoryCache4]);
|
|
1138
|
-
|
|
1139
|
-
getCachedWidget(name, function(err, widget) {
|
|
1140
|
-
checkErr(err);
|
|
1141
|
-
assert.ok(widget);
|
|
1142
|
-
|
|
1143
|
-
multiCache.get(key, function(err, result) {
|
|
1144
|
-
checkErr(err);
|
|
1145
|
-
assert.ok(result);
|
|
1146
|
-
|
|
1147
|
-
sinon.spy(memoryCache4, 'checkRefreshThreshold');
|
|
1148
|
-
|
|
1149
|
-
done();
|
|
1150
|
-
});
|
|
1151
|
-
});
|
|
1152
|
-
});
|
|
1153
|
-
|
|
1154
|
-
afterEach(function(done) {
|
|
1155
|
-
memoryCache4.checkRefreshThreshold.restore();
|
|
1156
|
-
done();
|
|
1157
|
-
});
|
|
1158
|
-
|
|
1159
|
-
function getCachedWidget(name, cb) {
|
|
1160
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1161
|
-
methods.getWidget(name, cacheCb);
|
|
1162
|
-
}, cb);
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
it('returns the value without refreshing if failing during TTL check', function(done) {
|
|
1166
|
-
// NOTE: ttl function is not defined
|
|
1167
|
-
var funcCalled = false;
|
|
1168
|
-
multiCache.wrap(key, function(cb) {
|
|
1169
|
-
funcCalled = true;
|
|
1170
|
-
methods.getWidget(name, cb);
|
|
1171
|
-
}, function(err, widget) {
|
|
1172
|
-
setTimeout(function() {
|
|
1173
|
-
checkErr(err);
|
|
1174
|
-
assert.deepEqual(widget, {name: name});
|
|
1175
|
-
assert.equal(memoryCache4.checkRefreshThreshold.callCount, 1);
|
|
1176
|
-
assert.ok(!funcCalled);
|
|
1177
|
-
done();
|
|
1178
|
-
}, 500);
|
|
1179
|
-
});
|
|
1180
|
-
});
|
|
1181
|
-
});
|
|
1182
|
-
|
|
1183
|
-
context("using a tweaked memory store when the result is valid", function() {
|
|
1184
|
-
beforeEach(function(done) {
|
|
1185
|
-
multiCache = multiCaching([memoryCache4]);
|
|
1186
|
-
|
|
1187
|
-
memoryCache4.store.ttl = function(key, cb) {
|
|
1188
|
-
return cb(null, 5);
|
|
1189
|
-
};
|
|
1190
|
-
getCachedWidget(name, function(err, widget) {
|
|
1191
|
-
checkErr(err);
|
|
1192
|
-
assert.ok(widget);
|
|
1193
|
-
|
|
1194
|
-
multiCache.get(key, function(err, result) {
|
|
1195
|
-
checkErr(err);
|
|
1196
|
-
assert.ok(result);
|
|
1197
|
-
|
|
1198
|
-
sinon.spy(memoryCache4.store, 'set');
|
|
1199
|
-
sinon.spy(memoryCache4.store, 'ttl');
|
|
1200
|
-
sinon.spy(memoryCache4.store, 'get');
|
|
1201
|
-
|
|
1202
|
-
done();
|
|
1203
|
-
});
|
|
1204
|
-
});
|
|
1205
|
-
});
|
|
1206
|
-
|
|
1207
|
-
afterEach(function(done) {
|
|
1208
|
-
memoryCache4.store.get.restore();
|
|
1209
|
-
memoryCache4.store.set.restore();
|
|
1210
|
-
memoryCache4.store.ttl.restore();
|
|
1211
|
-
memoryCache4.store.ttl = undefined;
|
|
1212
|
-
done();
|
|
1213
|
-
});
|
|
1214
|
-
|
|
1215
|
-
function getCachedWidget(name, cb) {
|
|
1216
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1217
|
-
methods.getWidget(name, cacheCb);
|
|
1218
|
-
}, cb);
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
it('returns the value without refreshing', function(done) {
|
|
1222
|
-
var funcCalled = false;
|
|
1223
|
-
multiCache.wrap(key, function(cb) {
|
|
1224
|
-
funcCalled = true;
|
|
1225
|
-
methods.getWidget(name, cb);
|
|
1226
|
-
}, function(err, widget) {
|
|
1227
|
-
setTimeout(function() {
|
|
1228
|
-
checkErr(err);
|
|
1229
|
-
assert.deepEqual(widget, {name: name});
|
|
1230
|
-
assert.ok(memoryCache4.store.get.called);
|
|
1231
|
-
assert.ok(!memoryCache4.store.set.called);
|
|
1232
|
-
assert.ok(!funcCalled);
|
|
1233
|
-
done();
|
|
1234
|
-
}, 500);
|
|
1235
|
-
});
|
|
1236
|
-
});
|
|
1237
|
-
|
|
1238
|
-
it('returns the value without refreshing but checking twice', function(done) {
|
|
1239
|
-
var funcCalled = 0;
|
|
1240
|
-
var values = [];
|
|
1241
|
-
|
|
1242
|
-
for (var i = 0; i < 2; i++) {
|
|
1243
|
-
values.push(i);
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
async.eachSeries(values, function(val, next) {
|
|
1247
|
-
multiCache.wrap(key, function(cb) {
|
|
1248
|
-
funcCalled += 1;
|
|
1249
|
-
methods.getWidget(name, function(err, result) {
|
|
1250
|
-
cb(err, result);
|
|
1251
|
-
});
|
|
1252
|
-
}, function(err, widget) {
|
|
1253
|
-
checkErr(err);
|
|
1254
|
-
assert.deepEqual(widget, {name: name});
|
|
1255
|
-
next(err);
|
|
1256
|
-
});
|
|
1257
|
-
}, function(err) {
|
|
1258
|
-
setTimeout(function() {
|
|
1259
|
-
checkErr(err);
|
|
1260
|
-
assert.equal(memoryCache4.store.get.callCount, 2);
|
|
1261
|
-
assert.equal(memoryCache4.store.ttl.callCount, 2);
|
|
1262
|
-
assert.equal(memoryCache4.store.set.callCount, 0);
|
|
1263
|
-
assert.equal(funcCalled, 0);
|
|
1264
|
-
done();
|
|
1265
|
-
}, 500);
|
|
1266
|
-
});
|
|
1267
|
-
});
|
|
1268
|
-
});
|
|
1269
|
-
context("using a tweaked memory store when the result is valid but expiring", function() {
|
|
1270
|
-
beforeEach(function(done) {
|
|
1271
|
-
multiCache = multiCaching([memoryCache4]);
|
|
1272
|
-
|
|
1273
|
-
memoryCache4.store.ttl = function(key, cb) {
|
|
1274
|
-
return cb(null, 1);
|
|
1275
|
-
};
|
|
1276
|
-
getCachedWidget(name, function(err, widget) {
|
|
1277
|
-
checkErr(err);
|
|
1278
|
-
assert.ok(widget);
|
|
1279
|
-
|
|
1280
|
-
multiCache.get(key, function(err, result) {
|
|
1281
|
-
checkErr(err);
|
|
1282
|
-
assert.ok(result);
|
|
1283
|
-
|
|
1284
|
-
sinon.spy(memoryCache4.store, 'set');
|
|
1285
|
-
sinon.spy(memoryCache4.store, 'ttl');
|
|
1286
|
-
sinon.spy(memoryCache4.store, 'get');
|
|
1287
|
-
|
|
1288
|
-
done();
|
|
1289
|
-
});
|
|
1290
|
-
});
|
|
1291
|
-
});
|
|
1292
|
-
|
|
1293
|
-
afterEach(function(done) {
|
|
1294
|
-
memoryCache4.store.get.restore();
|
|
1295
|
-
memoryCache4.store.set.restore();
|
|
1296
|
-
memoryCache4.store.ttl.restore();
|
|
1297
|
-
done();
|
|
1298
|
-
});
|
|
1299
|
-
|
|
1300
|
-
function getCachedWidget(name, cb) {
|
|
1301
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1302
|
-
methods.getWidget(name, cacheCb);
|
|
1303
|
-
}, cb);
|
|
1304
|
-
}
|
|
1305
|
-
|
|
1306
|
-
it('returns value and invokes worker in background', function(done) {
|
|
1307
|
-
var funcCalled = false;
|
|
1308
|
-
multiCache.wrap(key, function(cb) {
|
|
1309
|
-
funcCalled = true;
|
|
1310
|
-
methods.getWidget(name, cb);
|
|
1311
|
-
}, function(err, widget) {
|
|
1312
|
-
setTimeout(function() {
|
|
1313
|
-
checkErr(err);
|
|
1314
|
-
assert.deepEqual(widget, {name: name});
|
|
1315
|
-
assert.ok(memoryCache4.store.get.called);
|
|
1316
|
-
|
|
1317
|
-
assert.ok(funcCalled);
|
|
1318
|
-
assert.ok(memoryCache4.store.set.called);
|
|
1319
|
-
done();
|
|
1320
|
-
}, 500);
|
|
1321
|
-
});
|
|
1322
|
-
});
|
|
1323
|
-
|
|
1324
|
-
it("returns value and invokes worker in background once when called multiple times", function(done) {
|
|
1325
|
-
var funcCalled = 0;
|
|
1326
|
-
var values = [];
|
|
1327
|
-
for (var i = 0; i < 2; i++) {
|
|
1328
|
-
values.push(i);
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
async.each(values, function(val, next) {
|
|
1332
|
-
multiCache.wrap(key, function(cb) {
|
|
1333
|
-
funcCalled += 1;
|
|
1334
|
-
var timeout = support.random.number(100);
|
|
1335
|
-
setTimeout(function() {
|
|
1336
|
-
methods.getWidget(name, function(err, result) {
|
|
1337
|
-
cb(err, result);
|
|
1338
|
-
});
|
|
1339
|
-
}, timeout);
|
|
1340
|
-
}, function(err, widget) {
|
|
1341
|
-
checkErr(err);
|
|
1342
|
-
assert.deepEqual(widget, {name: name});
|
|
1343
|
-
next(err);
|
|
1344
|
-
});
|
|
1345
|
-
}, function(err) {
|
|
1346
|
-
// Wait for just a bit, to be sure that the callback is called.
|
|
1347
|
-
setTimeout(function() {
|
|
1348
|
-
checkErr(err);
|
|
1349
|
-
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1350
|
-
assert.equal(memoryCache4.store.ttl.callCount, 1);
|
|
1351
|
-
assert.equal(funcCalled, 1);
|
|
1352
|
-
assert.equal(memoryCache4.store.set.callCount, 1);
|
|
1353
|
-
done();
|
|
1354
|
-
}, 500);
|
|
1355
|
-
});
|
|
1356
|
-
});
|
|
1357
|
-
|
|
1358
|
-
it("returns value and invokes worker in background discarding result if error", function(done) {
|
|
1359
|
-
var funcCalled = false;
|
|
1360
|
-
var fakeError = new Error(support.random.string());
|
|
1361
|
-
multiCache.wrap(key, function(cb) {
|
|
1362
|
-
funcCalled = true;
|
|
1363
|
-
cb(fakeError);
|
|
1364
|
-
}, function(err, widget) {
|
|
1365
|
-
// Wait for just a bit, to be sure that the callback is called.
|
|
1366
|
-
setTimeout(function() {
|
|
1367
|
-
checkErr(err);
|
|
1368
|
-
assert.deepEqual(widget, {name: name});
|
|
1369
|
-
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1370
|
-
assert.ok(memoryCache4.store.ttl.calledWith(key));
|
|
1371
|
-
assert.ok(funcCalled);
|
|
1372
|
-
|
|
1373
|
-
assert.ok(!memoryCache4.store.set.called);
|
|
1374
|
-
done();
|
|
1375
|
-
}, 500);
|
|
1376
|
-
});
|
|
1377
|
-
});
|
|
1378
|
-
|
|
1379
|
-
it('returns value and invokes worker in background using the provided TTL function', function(done) {
|
|
1380
|
-
var funcCalled = false;
|
|
1381
|
-
var ttlFuncCalled = false;
|
|
1382
|
-
var ttlFunc = function() {
|
|
1383
|
-
ttlFuncCalled = true;
|
|
1384
|
-
return 4;
|
|
1385
|
-
};
|
|
1386
|
-
multiCache.wrap(key, function(cb) {
|
|
1387
|
-
funcCalled = true;
|
|
1388
|
-
methods.getWidget(name, cb);
|
|
1389
|
-
}, {ttl: ttlFunc}, function(err, widget) {
|
|
1390
|
-
setTimeout(function() {
|
|
1391
|
-
checkErr(err);
|
|
1392
|
-
assert.deepEqual(widget, {name: name});
|
|
1393
|
-
assert.ok(memoryCache4.store.get.called);
|
|
1394
|
-
|
|
1395
|
-
assert.ok(funcCalled);
|
|
1396
|
-
assert.ok(ttlFuncCalled);
|
|
1397
|
-
assert.ok(memoryCache4.store.set.called);
|
|
1398
|
-
done();
|
|
1399
|
-
}, 500);
|
|
1400
|
-
});
|
|
1401
|
-
});
|
|
1402
|
-
});
|
|
1403
|
-
});
|
|
1404
|
-
|
|
1405
|
-
describe("using two cache stores with refresh handling on the latter", function() {
|
|
1406
|
-
var memoryCache4 = caching({store: 'memory', ttl: 10, refreshThreshold: 2, promiseDependency: Promise});
|
|
1407
|
-
|
|
1408
|
-
context("when the result is only in [2]", function() {
|
|
1409
|
-
beforeEach(function(done) {
|
|
1410
|
-
multiCache = multiCaching([memoryCache3, memoryCache4]);
|
|
1411
|
-
|
|
1412
|
-
memoryCache4.store.ttl = function(key, cb) {
|
|
1413
|
-
return cb(null, 5);
|
|
1414
|
-
};
|
|
1415
|
-
getCachedWidget(name, function(err, widget) {
|
|
1416
|
-
checkErr(err);
|
|
1417
|
-
assert.ok(widget);
|
|
1418
|
-
|
|
1419
|
-
multiCache.get(key, function(err, result) {
|
|
1420
|
-
checkErr(err);
|
|
1421
|
-
assert.ok(result);
|
|
1422
|
-
|
|
1423
|
-
memoryCache3.del(key);
|
|
1424
|
-
|
|
1425
|
-
sinon.spy(memoryCache3.store, 'set');
|
|
1426
|
-
|
|
1427
|
-
sinon.spy(memoryCache4.store, 'set');
|
|
1428
|
-
sinon.spy(memoryCache4.store, 'ttl');
|
|
1429
|
-
sinon.spy(memoryCache4.store, 'get');
|
|
1430
|
-
|
|
1431
|
-
done();
|
|
1432
|
-
});
|
|
1433
|
-
});
|
|
1434
|
-
});
|
|
1435
|
-
|
|
1436
|
-
afterEach(function(done) {
|
|
1437
|
-
memoryCache3.store.set.restore();
|
|
1438
|
-
|
|
1439
|
-
memoryCache4.store.get.restore();
|
|
1440
|
-
memoryCache4.store.set.restore();
|
|
1441
|
-
memoryCache4.store.ttl.restore();
|
|
1442
|
-
memoryCache4.store.ttl = undefined;
|
|
1443
|
-
done();
|
|
1444
|
-
});
|
|
1445
|
-
|
|
1446
|
-
function getCachedWidget(name, cb) {
|
|
1447
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1448
|
-
methods.getWidget(name, cacheCb);
|
|
1449
|
-
}, cb);
|
|
1450
|
-
}
|
|
1451
|
-
|
|
1452
|
-
it('returns the value without refreshing and setting on [1]', function(done) {
|
|
1453
|
-
var funcCalled = false;
|
|
1454
|
-
multiCache.wrap(key, function(cb) {
|
|
1455
|
-
funcCalled = true;
|
|
1456
|
-
methods.getWidget(name, cb);
|
|
1457
|
-
}, function(err, widget) {
|
|
1458
|
-
setTimeout(function() {
|
|
1459
|
-
checkErr(err);
|
|
1460
|
-
assert.deepEqual(widget, {name: name});
|
|
1461
|
-
assert.equal(memoryCache3.store.set.callCount, 1);
|
|
1462
|
-
assert.equal(memoryCache4.store.get.callCount, 1);
|
|
1463
|
-
assert.equal(memoryCache4.store.set.callCount, 0);
|
|
1464
|
-
assert.equal(funcCalled, false);
|
|
1465
|
-
done();
|
|
1466
|
-
}, 500);
|
|
1467
|
-
});
|
|
1468
|
-
});
|
|
1469
|
-
});
|
|
1470
|
-
context("when the result is only in [2] but expiring", function() {
|
|
1471
|
-
beforeEach(function(done) {
|
|
1472
|
-
multiCache = multiCaching([memoryCache3, memoryCache4]);
|
|
1473
|
-
|
|
1474
|
-
memoryCache4.store.ttl = function(key, cb) {
|
|
1475
|
-
return cb(null, 1);
|
|
1476
|
-
};
|
|
1477
|
-
getCachedWidget(name, function(err, widget) {
|
|
1478
|
-
checkErr(err);
|
|
1479
|
-
assert.ok(widget);
|
|
1480
|
-
|
|
1481
|
-
multiCache.get(key, function(err, result) {
|
|
1482
|
-
checkErr(err);
|
|
1483
|
-
assert.ok(result);
|
|
1484
|
-
|
|
1485
|
-
memoryCache3.del(key);
|
|
1486
|
-
|
|
1487
|
-
sinon.spy(memoryCache3.store, 'set');
|
|
1488
|
-
|
|
1489
|
-
sinon.spy(memoryCache4.store, 'set');
|
|
1490
|
-
sinon.spy(memoryCache4.store, 'ttl');
|
|
1491
|
-
sinon.spy(memoryCache4.store, 'get');
|
|
1492
|
-
|
|
1493
|
-
done();
|
|
1494
|
-
});
|
|
1495
|
-
});
|
|
1496
|
-
});
|
|
1497
|
-
|
|
1498
|
-
afterEach(function(done) {
|
|
1499
|
-
memoryCache3.store.set.restore();
|
|
1500
|
-
|
|
1501
|
-
memoryCache4.store.get.restore();
|
|
1502
|
-
memoryCache4.store.set.restore();
|
|
1503
|
-
memoryCache4.store.ttl.restore();
|
|
1504
|
-
done();
|
|
1505
|
-
});
|
|
1506
|
-
|
|
1507
|
-
function getCachedWidget(name, cb) {
|
|
1508
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1509
|
-
methods.getWidget(name, cacheCb);
|
|
1510
|
-
}, cb);
|
|
1511
|
-
}
|
|
1512
|
-
|
|
1513
|
-
it('returns value and invokes worker in background, resetting all caches', function(done) {
|
|
1514
|
-
var funcCalled = false;
|
|
1515
|
-
multiCache.wrap(key, function(cb) {
|
|
1516
|
-
funcCalled = true;
|
|
1517
|
-
methods.getWidget(name, cb);
|
|
1518
|
-
}, function(err, widget) {
|
|
1519
|
-
setTimeout(function() {
|
|
1520
|
-
checkErr(err);
|
|
1521
|
-
assert.deepEqual(widget, {name: name});
|
|
1522
|
-
assert.equal(memoryCache4.store.get.callCount, 1);
|
|
1523
|
-
|
|
1524
|
-
assert.equal(funcCalled, true);
|
|
1525
|
-
assert.equal(memoryCache3.store.set.callCount, 2); // one call for the partial set
|
|
1526
|
-
assert.equal(memoryCache4.store.set.callCount, 1);
|
|
1527
|
-
done();
|
|
1528
|
-
}, 500);
|
|
1529
|
-
});
|
|
1530
|
-
});
|
|
1531
|
-
|
|
1532
|
-
it("returns value and invokes worker in background once when called multiple times", function(done) {
|
|
1533
|
-
var funcCalled = 0;
|
|
1534
|
-
var values = [];
|
|
1535
|
-
for (var i = 0; i < 2; i++) {
|
|
1536
|
-
values.push(i);
|
|
1537
|
-
}
|
|
1538
|
-
|
|
1539
|
-
async.each(values, function(val, next) {
|
|
1540
|
-
multiCache.wrap(key, function(cb) {
|
|
1541
|
-
funcCalled += 1;
|
|
1542
|
-
var timeout = support.random.number(100);
|
|
1543
|
-
setTimeout(function() {
|
|
1544
|
-
methods.getWidget(name, function(err, result) {
|
|
1545
|
-
cb(err, result);
|
|
1546
|
-
});
|
|
1547
|
-
}, timeout);
|
|
1548
|
-
}, function(err, widget) {
|
|
1549
|
-
checkErr(err);
|
|
1550
|
-
assert.deepEqual(widget, {name: name});
|
|
1551
|
-
next(err);
|
|
1552
|
-
});
|
|
1553
|
-
}, function(err) {
|
|
1554
|
-
// Wait for just a bit, to be sure that the callback is called.
|
|
1555
|
-
setTimeout(function() {
|
|
1556
|
-
checkErr(err);
|
|
1557
|
-
assert.equal(memoryCache3.store.set.callCount, 2); // one call for the partial set
|
|
1558
|
-
|
|
1559
|
-
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1560
|
-
assert.equal(memoryCache4.store.ttl.callCount, 1);
|
|
1561
|
-
assert.equal(funcCalled, 1);
|
|
1562
|
-
assert.equal(memoryCache4.store.set.callCount, 1);
|
|
1563
|
-
done();
|
|
1564
|
-
}, 500);
|
|
1565
|
-
});
|
|
1566
|
-
});
|
|
1567
|
-
|
|
1568
|
-
it("returns value and invokes worker in background discarding result if error", function(done) {
|
|
1569
|
-
var funcCalled = false;
|
|
1570
|
-
var fakeError = new Error(support.random.string());
|
|
1571
|
-
multiCache.wrap(key, function(cb) {
|
|
1572
|
-
funcCalled = true;
|
|
1573
|
-
cb(fakeError);
|
|
1574
|
-
}, function(err, widget) {
|
|
1575
|
-
// Wait for just a bit, to be sure that the callback is called.
|
|
1576
|
-
setTimeout(function() {
|
|
1577
|
-
checkErr(err);
|
|
1578
|
-
assert.deepEqual(widget, {name: name});
|
|
1579
|
-
assert.equal(memoryCache3.store.set.callCount, 1);
|
|
1580
|
-
|
|
1581
|
-
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1582
|
-
assert.equal(memoryCache4.store.ttl.callCount, 1);
|
|
1583
|
-
assert.equal(funcCalled, true);
|
|
1584
|
-
|
|
1585
|
-
assert.equal(memoryCache4.store.set.callCount, 0);
|
|
1586
|
-
done();
|
|
1587
|
-
}, 500);
|
|
1588
|
-
});
|
|
1589
|
-
});
|
|
1590
|
-
});
|
|
1591
|
-
});
|
|
1592
|
-
describe("using two cache stores", function() {
|
|
1593
|
-
beforeEach(function() {
|
|
1594
|
-
multiCache = multiCaching([memoryCache, memoryCache3]);
|
|
1595
|
-
});
|
|
1596
|
-
|
|
1597
|
-
it("calls back with the result of a function", function(done) {
|
|
1598
|
-
multiCache.wrap(key, function(cb) {
|
|
1599
|
-
methods.getWidget(name, cb);
|
|
1600
|
-
}, function(err, widget) {
|
|
1601
|
-
checkErr(err);
|
|
1602
|
-
assert.deepEqual(widget, {name: name});
|
|
1603
|
-
done();
|
|
1604
|
-
});
|
|
1605
|
-
});
|
|
1606
|
-
|
|
1607
|
-
it("sets value in all caches", function(done) {
|
|
1608
|
-
multiCache.wrap(key, function(cb) {
|
|
1609
|
-
methods.getWidget(name, cb);
|
|
1610
|
-
}, function(err, widget) {
|
|
1611
|
-
checkErr(err);
|
|
1612
|
-
assert.deepEqual(widget, {name: name});
|
|
1613
|
-
|
|
1614
|
-
memoryCache.get(key, function(err, result) {
|
|
1615
|
-
checkErr(err);
|
|
1616
|
-
assert.deepEqual(result, {name: name});
|
|
1617
|
-
|
|
1618
|
-
memoryCache3.get(key, function(err, result) {
|
|
1619
|
-
checkErr(err);
|
|
1620
|
-
assert.deepEqual(result, {name: name});
|
|
1621
|
-
done();
|
|
1622
|
-
});
|
|
1623
|
-
});
|
|
1624
|
-
});
|
|
1625
|
-
});
|
|
1626
|
-
|
|
1627
|
-
it('sets the ttl value by context (store) with a function', function(done) {
|
|
1628
|
-
memoryCache3.store.name = 'redis';
|
|
1629
|
-
sinon.spy(memoryCache.store, 'set');
|
|
1630
|
-
sinon.spy(memoryCache3.store, 'set');
|
|
1631
|
-
sinon.spy(methods, 'getTTL');
|
|
1632
|
-
|
|
1633
|
-
multiCache.wrap(key, function(cb) {
|
|
1634
|
-
methods.getWidget(name, cb);
|
|
1635
|
-
}, {ttl: methods.getTTL}, function(err) {
|
|
1636
|
-
checkErr(err);
|
|
1637
|
-
sinon.assert.calledTwice(methods.getTTL);
|
|
1638
|
-
sinon.assert.calledWith(methods.getTTL, [key, {name: name}], 'memory');
|
|
1639
|
-
sinon.assert.calledWith(methods.getTTL, [key, {name: name}], 'redis');
|
|
1640
|
-
sinon.assert.calledWith(memoryCache.store.set, key, {name: name}, {ttl: 444});
|
|
1641
|
-
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, {ttl: 777});
|
|
1642
|
-
|
|
1643
|
-
methods.getTTL.restore();
|
|
1644
|
-
memoryCache.store.set.restore();
|
|
1645
|
-
memoryCache3.store.set.restore();
|
|
1646
|
-
memoryCache3.store.name = 'memory';
|
|
1647
|
-
done();
|
|
1648
|
-
});
|
|
1649
|
-
});
|
|
1650
|
-
|
|
1651
|
-
context("when value exists in first store but not second", function() {
|
|
1652
|
-
it("returns value from first store, does not set it in second", function(done) {
|
|
1653
|
-
memoryCache.set(key, {name: name}, function(err) {
|
|
1654
|
-
checkErr(err);
|
|
1655
|
-
|
|
1656
|
-
multiCache.wrap(key, function(cb) {
|
|
1657
|
-
methods.getWidget(name, cb);
|
|
1658
|
-
}, function(err, widget) {
|
|
1659
|
-
checkErr(err);
|
|
1660
|
-
assert.deepEqual(widget, {name: name});
|
|
1661
|
-
|
|
1662
|
-
memoryCache3.get(key, function(err, result) {
|
|
1663
|
-
checkErr(err);
|
|
1664
|
-
assert.equal(result, null);
|
|
1665
|
-
done();
|
|
1666
|
-
});
|
|
1667
|
-
});
|
|
1668
|
-
});
|
|
1669
|
-
});
|
|
1670
|
-
|
|
1671
|
-
it("returns value from first store, does not set it in second (multiple keys)", function(done) {
|
|
1672
|
-
memoryCache.mset(key, {name: name}, key2, {name: name2}, function(err) {
|
|
1673
|
-
checkErr(err);
|
|
1674
|
-
|
|
1675
|
-
multiCache.wrap(key, key2, function(cb) {
|
|
1676
|
-
methods.getMultiWidget([name, name2], cb);
|
|
1677
|
-
}, function(err, widgets) {
|
|
1678
|
-
checkErr(err);
|
|
1679
|
-
assert.deepEqual(widgets[0], {name: name});
|
|
1680
|
-
assert.deepEqual(widgets[1], {name: name2});
|
|
1681
|
-
|
|
1682
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
1683
|
-
checkErr(err);
|
|
1684
|
-
assert.equal(result[0], null);
|
|
1685
|
-
assert.equal(result[1], null);
|
|
1686
|
-
done();
|
|
1687
|
-
});
|
|
1688
|
-
});
|
|
1689
|
-
});
|
|
1690
|
-
});
|
|
1691
|
-
});
|
|
1692
|
-
|
|
1693
|
-
context("when value exists in second store but not first", function() {
|
|
1694
|
-
it("returns value from second store, sets it in first store", function(done) {
|
|
1695
|
-
memoryCache3.set(key, {name: name}, function(err) {
|
|
1696
|
-
checkErr(err);
|
|
1697
|
-
|
|
1698
|
-
multiCache.wrap(key, function(cb) {
|
|
1699
|
-
methods.getWidget(name, cb);
|
|
1700
|
-
}, function(err, widget) {
|
|
1701
|
-
checkErr(err);
|
|
1702
|
-
assert.deepEqual(widget, {name: name});
|
|
1703
|
-
|
|
1704
|
-
memoryCache.get(key, function(err, result) {
|
|
1705
|
-
checkErr(err);
|
|
1706
|
-
assert.deepEqual(result, {name: name});
|
|
1707
|
-
done();
|
|
1708
|
-
});
|
|
1709
|
-
});
|
|
1710
|
-
});
|
|
1711
|
-
});
|
|
1712
|
-
|
|
1713
|
-
it("returns value from second store, sets it in first store (multiple keys)", function(done) {
|
|
1714
|
-
memoryCache3.mset(key, {name: name}, key2, {name: name2}, function(err) {
|
|
1715
|
-
checkErr(err);
|
|
1716
|
-
|
|
1717
|
-
multiCache.wrap(key, key2, function(cb) {
|
|
1718
|
-
methods.getMultiWidget([name, name2], cb);
|
|
1719
|
-
}, function(err, widgets) {
|
|
1720
|
-
checkErr(err);
|
|
1721
|
-
assert.deepEqual(widgets[0], {name: name});
|
|
1722
|
-
assert.deepEqual(widgets[1], {name: name2});
|
|
1723
|
-
|
|
1724
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
1725
|
-
checkErr(err);
|
|
1726
|
-
assert.deepEqual(result[0], {name: name});
|
|
1727
|
-
assert.deepEqual(result[1], {name: name2});
|
|
1728
|
-
done();
|
|
1729
|
-
});
|
|
1730
|
-
});
|
|
1731
|
-
});
|
|
1732
|
-
});
|
|
1733
|
-
});
|
|
1734
|
-
});
|
|
1735
|
-
|
|
1736
|
-
describe("using three cache stores", function() {
|
|
1737
|
-
beforeEach(function() {
|
|
1738
|
-
multiCache = multiCaching([memoryCache, memoryCache3, memoryCache2]);
|
|
1739
|
-
});
|
|
1740
|
-
|
|
1741
|
-
it("calls back with the result of a function", function(done) {
|
|
1742
|
-
multiCache.wrap(key, function(cb) {
|
|
1743
|
-
methods.getWidget(name, cb);
|
|
1744
|
-
}, function(err, widget) {
|
|
1745
|
-
checkErr(err);
|
|
1746
|
-
assert.deepEqual(widget, {name: name});
|
|
1747
|
-
done();
|
|
1748
|
-
});
|
|
1749
|
-
});
|
|
1750
|
-
|
|
1751
|
-
it("sets value in all caches", function(done) {
|
|
1752
|
-
multiCache.wrap(key, function(cb) {
|
|
1753
|
-
methods.getWidget(name, cb);
|
|
1754
|
-
}, function(err, widget) {
|
|
1755
|
-
checkErr(err);
|
|
1756
|
-
assert.deepEqual(widget, {name: name});
|
|
1757
|
-
|
|
1758
|
-
memoryCache.get(key, function(err, result) {
|
|
1759
|
-
checkErr(err);
|
|
1760
|
-
assert.deepEqual(result, {name: name});
|
|
1761
|
-
|
|
1762
|
-
memoryCache2.get(key, function(err, result) {
|
|
1763
|
-
checkErr(err);
|
|
1764
|
-
assert.deepEqual(result, {name: name});
|
|
1765
|
-
|
|
1766
|
-
memoryCache3.get(key, function(err, result) {
|
|
1767
|
-
checkErr(err);
|
|
1768
|
-
assert.deepEqual(result, {name: name});
|
|
1769
|
-
done();
|
|
1770
|
-
});
|
|
1771
|
-
});
|
|
1772
|
-
});
|
|
1773
|
-
});
|
|
1774
|
-
});
|
|
1775
|
-
|
|
1776
|
-
context("when value exists in first store only", function() {
|
|
1777
|
-
it("returns value from first store, does not set it in second or third", function(done) {
|
|
1778
|
-
memoryCache.set(key, {name: name}, function(err) {
|
|
1779
|
-
checkErr(err);
|
|
1780
|
-
|
|
1781
|
-
multiCache.wrap(key, function(cb) {
|
|
1782
|
-
methods.getWidget(name, cb);
|
|
1783
|
-
}, function(err, widget) {
|
|
1784
|
-
checkErr(err);
|
|
1785
|
-
assert.deepEqual(widget, {name: name});
|
|
1786
|
-
|
|
1787
|
-
memoryCache2.get(key, function(err, result) {
|
|
1788
|
-
checkErr(err);
|
|
1789
|
-
assert.equal(result, null);
|
|
1790
|
-
|
|
1791
|
-
memoryCache3.get(key, function(err, result) {
|
|
1792
|
-
checkErr(err);
|
|
1793
|
-
assert.equal(result, null);
|
|
1794
|
-
done();
|
|
1795
|
-
});
|
|
1796
|
-
});
|
|
1797
|
-
});
|
|
1798
|
-
});
|
|
1799
|
-
});
|
|
1800
|
-
});
|
|
1801
|
-
|
|
1802
|
-
context("when value exists in second store only", function() {
|
|
1803
|
-
it("returns value from second store, sets it in first store, does not set third store", function(done) {
|
|
1804
|
-
memoryCache3.set(key, {name: name}, function(err) {
|
|
1805
|
-
checkErr(err);
|
|
1806
|
-
|
|
1807
|
-
multiCache.wrap(key, function(cb) {
|
|
1808
|
-
methods.getWidget(name, cb);
|
|
1809
|
-
}, function(err, widget) {
|
|
1810
|
-
checkErr(err);
|
|
1811
|
-
assert.deepEqual(widget, {name: name});
|
|
1812
|
-
|
|
1813
|
-
memoryCache.get(key, function(err, result) {
|
|
1814
|
-
checkErr(err);
|
|
1815
|
-
assert.deepEqual(result, {name: name});
|
|
1816
|
-
|
|
1817
|
-
memoryCache2.get(key, function(err, result) {
|
|
1818
|
-
checkErr(err);
|
|
1819
|
-
assert.equal(result, null);
|
|
1820
|
-
done();
|
|
1821
|
-
});
|
|
1822
|
-
});
|
|
1823
|
-
});
|
|
1824
|
-
});
|
|
1825
|
-
});
|
|
1826
|
-
});
|
|
1827
|
-
|
|
1828
|
-
context("when value exists in third store only", function() {
|
|
1829
|
-
it("returns value from third store, sets it in first and second stores", function(done) {
|
|
1830
|
-
memoryCache2.set(key, {name: name}, function(err) {
|
|
1831
|
-
checkErr(err);
|
|
1832
|
-
|
|
1833
|
-
multiCache.wrap(key, function(cb) {
|
|
1834
|
-
methods.getWidget(name, cb);
|
|
1835
|
-
}, function(err, widget) {
|
|
1836
|
-
checkErr(err);
|
|
1837
|
-
assert.deepEqual(widget, {name: name});
|
|
1838
|
-
|
|
1839
|
-
memoryCache3.get(key, function(err, result) {
|
|
1840
|
-
checkErr(err);
|
|
1841
|
-
assert.deepEqual(result, {name: name});
|
|
1842
|
-
|
|
1843
|
-
memoryCache.get(key, function(err, result) {
|
|
1844
|
-
checkErr(err);
|
|
1845
|
-
assert.deepEqual(result, {name: name});
|
|
1846
|
-
|
|
1847
|
-
done();
|
|
1848
|
-
});
|
|
1849
|
-
});
|
|
1850
|
-
});
|
|
1851
|
-
});
|
|
1852
|
-
});
|
|
1853
|
-
});
|
|
1854
|
-
|
|
1855
|
-
it("lets us make nested calls", function(done) {
|
|
1856
|
-
function getCachedWidget(name, cb) {
|
|
1857
|
-
multiCache.wrap(key, function(cacheCb) {
|
|
1858
|
-
methods.getWidget(name, cacheCb);
|
|
1859
|
-
}, cb);
|
|
1860
|
-
}
|
|
1861
|
-
|
|
1862
|
-
getCachedWidget(name, function(err, widget) {
|
|
1863
|
-
checkErr(err);
|
|
1864
|
-
assert.equal(widget.name, name);
|
|
1865
|
-
|
|
1866
|
-
getCachedWidget(name, function(err, widget) {
|
|
1867
|
-
checkErr(err);
|
|
1868
|
-
assert.equal(widget.name, name);
|
|
1869
|
-
|
|
1870
|
-
getCachedWidget(name, function(err, widget) {
|
|
1871
|
-
checkErr(err);
|
|
1872
|
-
assert.equal(widget.name, name);
|
|
1873
|
-
done();
|
|
1874
|
-
});
|
|
1875
|
-
});
|
|
1876
|
-
});
|
|
1877
|
-
});
|
|
1878
|
-
|
|
1879
|
-
describe("passing multiple keys", function() {
|
|
1880
|
-
it("sets value in all caches", function(done) {
|
|
1881
|
-
multiCache.wrap(key, key2, function(cb) {
|
|
1882
|
-
methods.getMultiWidget([name, name2], cb);
|
|
1883
|
-
}, function(err, widgets) {
|
|
1884
|
-
checkErr(err);
|
|
1885
|
-
assert.deepEqual(widgets[0], {name: name});
|
|
1886
|
-
assert.deepEqual(widgets[1], {name: name2});
|
|
1887
|
-
|
|
1888
|
-
memoryCache.mget(key, key2, function(err, result) {
|
|
1889
|
-
checkErr(err);
|
|
1890
|
-
assert.deepEqual(result[0], {name: name});
|
|
1891
|
-
assert.deepEqual(result[1], {name: name2});
|
|
1892
|
-
|
|
1893
|
-
memoryCache2.mget(key, key2, function(err, result) {
|
|
1894
|
-
checkErr(err);
|
|
1895
|
-
assert.deepEqual(result[0], {name: name});
|
|
1896
|
-
assert.deepEqual(result[1], {name: name2});
|
|
1897
|
-
|
|
1898
|
-
memoryCache3.mget(key, key2, function(err, result) {
|
|
1899
|
-
checkErr(err);
|
|
1900
|
-
assert.deepEqual(result[0], {name: name});
|
|
1901
|
-
assert.deepEqual(result[1], {name: name2});
|
|
1902
|
-
done();
|
|
1903
|
-
});
|
|
1904
|
-
});
|
|
1905
|
-
});
|
|
1906
|
-
});
|
|
1907
|
-
});
|
|
1908
|
-
|
|
1909
|
-
it("should not throw error if one of the store does not have `mset() or mget()`", function(done) {
|
|
1910
|
-
var basicStore = {
|
|
1911
|
-
set: function() {},
|
|
1912
|
-
get: function() {}
|
|
1913
|
-
};
|
|
1914
|
-
sinon.spy(basicStore, 'set');
|
|
1915
|
-
multiCache = multiCaching([memoryCache, {store: basicStore}]);
|
|
1916
|
-
|
|
1917
|
-
multiCache.wrap(key, key2, function(cb) {
|
|
1918
|
-
methods.getMultiWidget([name, name2], cb);
|
|
1919
|
-
}, done);
|
|
1920
|
-
});
|
|
1921
|
-
});
|
|
1922
|
-
});
|
|
1923
|
-
|
|
1924
|
-
context("error handling", function() {
|
|
1925
|
-
var memoryStoreStub;
|
|
1926
|
-
var ttl;
|
|
1927
|
-
|
|
1928
|
-
beforeEach(function() {
|
|
1929
|
-
ttl = 0.1;
|
|
1930
|
-
memoryStoreStub = memoryStore.create({ttl: ttl});
|
|
1931
|
-
sinon.stub(memoryStore, 'create').returns(memoryStoreStub);
|
|
1932
|
-
memoryCache = caching({store: 'memory', ttl: ttl});
|
|
1933
|
-
multiCache = multiCaching([memoryCache]);
|
|
1934
|
-
});
|
|
1935
|
-
|
|
1936
|
-
afterEach(function() {
|
|
1937
|
-
memoryStore.create.restore();
|
|
1938
|
-
});
|
|
1939
|
-
|
|
1940
|
-
context("when an error is thrown in the work function", function() {
|
|
1941
|
-
var fakeError;
|
|
1942
|
-
|
|
1943
|
-
beforeEach(function() {
|
|
1944
|
-
fakeError = new Error(support.random.string());
|
|
1945
|
-
});
|
|
1946
|
-
|
|
1947
|
-
it("does not catch the error", function(done) {
|
|
1948
|
-
var originalExceptionHandler = process.listeners('uncaughtException').pop();
|
|
1949
|
-
process.removeListener('uncaughtException', originalExceptionHandler);
|
|
1950
|
-
|
|
1951
|
-
process.once('uncaughtException', function(err) {
|
|
1952
|
-
process.on('uncaughtException', originalExceptionHandler);
|
|
1953
|
-
assert.ok(err);
|
|
1954
|
-
done();
|
|
1955
|
-
});
|
|
1956
|
-
|
|
1957
|
-
multiCache.wrap(key, function() {
|
|
1958
|
-
throw fakeError;
|
|
1959
|
-
}, function() {
|
|
1960
|
-
done(new Error('Should not have caught error'));
|
|
1961
|
-
});
|
|
1962
|
-
});
|
|
1963
|
-
});
|
|
1964
|
-
|
|
1965
|
-
context("when an error is thrown in the work function's callback", function() {
|
|
1966
|
-
var fakeError;
|
|
1967
|
-
|
|
1968
|
-
beforeEach(function() {
|
|
1969
|
-
fakeError = new Error(support.random.string());
|
|
1970
|
-
});
|
|
1971
|
-
|
|
1972
|
-
it("bubbles up that error", function(done) {
|
|
1973
|
-
multiCache.wrap(key, function(next) {
|
|
1974
|
-
next();
|
|
1975
|
-
}, {ttl: ttl}, function() {
|
|
1976
|
-
// This test as-is doesn't prove a fix for #28 (https://github.com/BryanDonovan/node-cache-manager/issues/28)
|
|
1977
|
-
// but if you remove the try/catch, it shows that the undefined `waiting` array issue
|
|
1978
|
-
// is no longer present (the domain doesn't try to process the error in the callbackFiller).
|
|
1979
|
-
try {
|
|
1980
|
-
throw new Error('foo');
|
|
1981
|
-
} catch (e) {
|
|
1982
|
-
assert.equal(e.message, 'foo');
|
|
1983
|
-
}
|
|
1984
|
-
|
|
1985
|
-
done();
|
|
1986
|
-
});
|
|
1987
|
-
});
|
|
1988
|
-
|
|
1989
|
-
it("bubbles up that error (multiple keys)", function(done) {
|
|
1990
|
-
multiCache.wrap(key, key2, function(next) {
|
|
1991
|
-
next(fakeError);
|
|
1992
|
-
}, {ttl: ttl}, function(err) {
|
|
1993
|
-
assert.equal(err, fakeError);
|
|
1994
|
-
done();
|
|
1995
|
-
});
|
|
1996
|
-
});
|
|
1997
|
-
});
|
|
1998
|
-
|
|
1999
|
-
context("when store.get() calls back with an error", function() {
|
|
2000
|
-
it("bubbles up that error", function(done) {
|
|
2001
|
-
var fakeError = new Error(support.random.string());
|
|
2002
|
-
|
|
2003
|
-
sinon.stub(memoryStoreStub, 'get').yields(fakeError);
|
|
2004
|
-
|
|
2005
|
-
multiCache.wrap(key, function(cb) {
|
|
2006
|
-
methods.getWidget(name, cb);
|
|
2007
|
-
}, function(err) {
|
|
2008
|
-
assert.equal(err, fakeError);
|
|
2009
|
-
memoryStoreStub.get.restore();
|
|
2010
|
-
done();
|
|
2011
|
-
});
|
|
2012
|
-
});
|
|
2013
|
-
});
|
|
2014
|
-
|
|
2015
|
-
context("when store.set() calls back with an error", function() {
|
|
2016
|
-
it("bubbles up that error", function(done) {
|
|
2017
|
-
var fakeError = new Error(support.random.string());
|
|
2018
|
-
|
|
2019
|
-
sinon.stub(memoryStoreStub, 'set').yields(fakeError);
|
|
2020
|
-
|
|
2021
|
-
multiCache.wrap(key, function(cb) {
|
|
2022
|
-
methods.getWidget(name, cb);
|
|
2023
|
-
}, function(err) {
|
|
2024
|
-
assert.equal(err, fakeError);
|
|
2025
|
-
memoryStoreStub.set.restore();
|
|
2026
|
-
done();
|
|
2027
|
-
});
|
|
2028
|
-
});
|
|
2029
|
-
});
|
|
2030
|
-
|
|
2031
|
-
context("when store.mget() calls back with an error", function() {
|
|
2032
|
-
it("bubbles up that error", function(done) {
|
|
2033
|
-
var fakeError = new Error(support.random.string());
|
|
2034
|
-
|
|
2035
|
-
sinon.stub(memoryStoreStub, 'mget').yields(fakeError);
|
|
2036
|
-
|
|
2037
|
-
multiCache.wrap(key, key2, function(cb) {
|
|
2038
|
-
methods.getMultiWidget([name, name2], cb);
|
|
2039
|
-
}, function(err) {
|
|
2040
|
-
assert.equal(err, fakeError);
|
|
2041
|
-
memoryStoreStub.mget.restore();
|
|
2042
|
-
done();
|
|
2043
|
-
});
|
|
2044
|
-
});
|
|
2045
|
-
});
|
|
2046
|
-
});
|
|
2047
|
-
|
|
2048
|
-
describe("when called multiple times in parallel with same key", function() {
|
|
2049
|
-
var construct;
|
|
2050
|
-
var constructMulti;
|
|
2051
|
-
|
|
2052
|
-
beforeEach(function() {
|
|
2053
|
-
multiCache = multiCaching([memoryCache, memoryCache3]);
|
|
2054
|
-
var firstTimeout = 110;
|
|
2055
|
-
var firstTimeoutUsed = false;
|
|
2056
|
-
|
|
2057
|
-
function getTimeout() {
|
|
2058
|
-
if (firstTimeoutUsed) {
|
|
2059
|
-
support.random.number(100);
|
|
2060
|
-
} else {
|
|
2061
|
-
firstTimeoutUsed = true;
|
|
2062
|
-
return firstTimeout;
|
|
2063
|
-
}
|
|
2064
|
-
}
|
|
2065
|
-
|
|
2066
|
-
construct = sinon.spy(function(val, cb) {
|
|
2067
|
-
var timeout = getTimeout();
|
|
2068
|
-
setTimeout(function() {
|
|
2069
|
-
cb(null, 'value');
|
|
2070
|
-
}, timeout);
|
|
2071
|
-
});
|
|
2072
|
-
|
|
2073
|
-
constructMulti = sinon.spy(function(val, cb) {
|
|
2074
|
-
var timeout = getTimeout();
|
|
2075
|
-
setTimeout(function() {
|
|
2076
|
-
cb(null, ['value', 'value2']);
|
|
2077
|
-
}, timeout);
|
|
2078
|
-
});
|
|
2079
|
-
});
|
|
2080
|
-
|
|
2081
|
-
it("calls the wrapped function once", function(done) {
|
|
2082
|
-
var values = [];
|
|
2083
|
-
for (var i = 0; i < 5; i++) {
|
|
2084
|
-
values.push(i);
|
|
2085
|
-
}
|
|
2086
|
-
|
|
2087
|
-
async.each(values, function(val, next) {
|
|
2088
|
-
multiCache.wrap('key', function(cb) {
|
|
2089
|
-
construct(val, cb);
|
|
2090
|
-
}, function(err, result) {
|
|
2091
|
-
assert.equal(result, 'value');
|
|
2092
|
-
next(err);
|
|
2093
|
-
});
|
|
2094
|
-
}, function(err) {
|
|
2095
|
-
checkErr(err);
|
|
2096
|
-
assert.equal(construct.callCount, 1);
|
|
2097
|
-
done();
|
|
2098
|
-
});
|
|
2099
|
-
});
|
|
2100
|
-
|
|
2101
|
-
it("calls the multi wrapped function once", function(done) {
|
|
2102
|
-
var values = [];
|
|
2103
|
-
for (var i = 0; i < 5; i++) {
|
|
2104
|
-
values.push(i);
|
|
2105
|
-
}
|
|
2106
|
-
|
|
2107
|
-
async.each(values, function(val, next) {
|
|
2108
|
-
multiCache.wrap('key', 'key2', function(cb) {
|
|
2109
|
-
constructMulti(val, cb);
|
|
2110
|
-
}, function(err, results) {
|
|
2111
|
-
assert.equal(results[0], 'value');
|
|
2112
|
-
assert.equal(results[1], 'value2');
|
|
2113
|
-
next(err);
|
|
2114
|
-
});
|
|
2115
|
-
}, function(err) {
|
|
2116
|
-
checkErr(err);
|
|
2117
|
-
assert.equal(constructMulti.callCount, 1);
|
|
2118
|
-
done();
|
|
2119
|
-
});
|
|
2120
|
-
});
|
|
2121
|
-
});
|
|
2122
|
-
|
|
2123
|
-
describe("using native promises", function() {
|
|
2124
|
-
beforeEach(function() {
|
|
2125
|
-
multiCache = multiCaching([memoryCache, memoryCache3]);
|
|
2126
|
-
});
|
|
2127
|
-
|
|
2128
|
-
it("should be able to chain with simple promise", function(done) {
|
|
2129
|
-
multiCache.wrap('key', function() {
|
|
2130
|
-
return 'OK';
|
|
2131
|
-
})
|
|
2132
|
-
.then(function(res) {
|
|
2133
|
-
assert.equal(res, 'OK');
|
|
2134
|
-
done();
|
|
2135
|
-
});
|
|
2136
|
-
});
|
|
2137
|
-
|
|
2138
|
-
it("should be able to chain with cache function as a promise", function(done) {
|
|
2139
|
-
multiCache.wrap('key', function() {
|
|
2140
|
-
return new Promise(function(resolve) {
|
|
2141
|
-
resolve('OK');
|
|
2142
|
-
});
|
|
2143
|
-
})
|
|
2144
|
-
.then(function(res) {
|
|
2145
|
-
assert.equal(res, 'OK');
|
|
2146
|
-
done();
|
|
2147
|
-
});
|
|
2148
|
-
});
|
|
2149
|
-
|
|
2150
|
-
it("should be able to catch errors in cache function as a promise", function(done) {
|
|
2151
|
-
multiCache.wrap('key', function() {
|
|
2152
|
-
return new Promise(function(resolve, reject) {
|
|
2153
|
-
reject('NOK');
|
|
2154
|
-
});
|
|
2155
|
-
})
|
|
2156
|
-
.then(function() {
|
|
2157
|
-
done(new Error('It should not call then since there is an error in the cache function!'));
|
|
2158
|
-
})
|
|
2159
|
-
.catch(function() {
|
|
2160
|
-
done();
|
|
2161
|
-
});
|
|
2162
|
-
});
|
|
2163
|
-
|
|
2164
|
-
it("should be able to catch a throw in cache function as a promise", function(done) {
|
|
2165
|
-
multiCache.wrap('key', function() {
|
|
2166
|
-
throw 'NOK';
|
|
2167
|
-
})
|
|
2168
|
-
.then(function() {
|
|
2169
|
-
done(new Error('It should not call then since there is an error in the cache function!'));
|
|
2170
|
-
})
|
|
2171
|
-
.catch(function() {
|
|
2172
|
-
done();
|
|
2173
|
-
});
|
|
2174
|
-
});
|
|
2175
|
-
|
|
2176
|
-
it("should be able to chain with non-cacheable value", function(done) {
|
|
2177
|
-
multiCache.wrap('key', function() {
|
|
2178
|
-
return;
|
|
2179
|
-
})
|
|
2180
|
-
.then(function(res) {
|
|
2181
|
-
assert.equal(res, undefined);
|
|
2182
|
-
done();
|
|
2183
|
-
});
|
|
2184
|
-
});
|
|
2185
|
-
});
|
|
2186
|
-
});
|
|
2187
|
-
|
|
2188
|
-
context("when instantiated with a non-Array 'caches' arg", function() {
|
|
2189
|
-
it("throws an error", function() {
|
|
2190
|
-
assert.throws(function() {
|
|
2191
|
-
multiCaching({foo: 'bar'});
|
|
2192
|
-
}, /multiCaching requires an array/);
|
|
2193
|
-
});
|
|
2194
|
-
});
|
|
2195
|
-
});
|