cache-manager 3.4.0 → 3.4.4

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.
@@ -1,1587 +0,0 @@
1
- // TODO: These are really a mix of unit and integration tests.
2
-
3
- var assert = require('assert');
4
- var async = require('async');
5
- var sinon = require('sinon');
6
- var support = require('./support');
7
- var checkErr = support.checkErr;
8
- var caching = require('../index').caching;
9
- var memoryStore = require('../lib/stores/memory');
10
-
11
- var Promise = require('es6-promise').Promise;
12
-
13
- var methods = {
14
- getWidget: function(name, cb) {
15
- process.nextTick(function() {
16
- cb(null, {name: name});
17
- });
18
- },
19
- getMultiWidget: function(names, cb) {
20
- process.nextTick(function() {
21
- cb(null, names.map(function(name) { return {name: name}; }));
22
- });
23
- }
24
- };
25
-
26
- describe("caching", function() {
27
- var cache;
28
- var key;
29
- var defaultTtl = 1;
30
- var name;
31
- var value;
32
-
33
- describe("get() and set()", function() {
34
- ['memory'].forEach(function(store) {
35
- context("using " + store + " store", function() {
36
- beforeEach(function() {
37
- cache = caching({store: store, promiseDependency: Promise});
38
- key = support.random.string(20);
39
- value = support.random.string();
40
- });
41
-
42
- it("lets us set and get data in cache", function(done) {
43
- cache.set(key, value, {ttl: defaultTtl}, function(err) {
44
- checkErr(err);
45
-
46
- cache.get(key, function(err, result) {
47
- checkErr(err);
48
- assert.equal(result, value);
49
- done();
50
- });
51
- });
52
- });
53
-
54
- it("lets us set and get data without a callback", function(done) {
55
- cache = caching({store: memoryStore.create({noPromises: true})});
56
- cache.set(key, value, {ttl: defaultTtl});
57
-
58
- setTimeout(function() {
59
- var result = cache.get(key);
60
- assert.equal(result, value);
61
- done();
62
- }, 20);
63
- });
64
-
65
- it("lets us set and get data without a callback, returning a promise", function(done) {
66
- cache.set(key, value, {ttl: defaultTtl});
67
- setTimeout(function() {
68
- cache.get(key)
69
- .then(function(result) {
70
- assert.equal(result, value);
71
- done();
72
- });
73
- }, 20);
74
- });
75
-
76
- it("lets us set and get data without options object or callback", function(done) {
77
- cache = caching({store: memoryStore.create({noPromises: true})});
78
- cache.set(key, value);
79
-
80
- setTimeout(function() {
81
- var result = cache.get(key);
82
- assert.equal(result, value);
83
- done();
84
- }, 20);
85
- });
86
- });
87
- });
88
- });
89
-
90
- describe("mget() and mset()", function() {
91
- var key2;
92
- var value2;
93
- var store = 'memory';
94
-
95
- beforeEach(function() {
96
- key = support.random.string(20);
97
- value = support.random.string();
98
- key2 = support.random.string(20);
99
- value2 = support.random.string();
100
-
101
- cache = caching({store: store, ttl: defaultTtl, ignoreCacheErrors: false});
102
- });
103
-
104
- it("lets us set and get several keys and data in cache", function(done) {
105
- cache.mset(key, value, key2, value2, {ttl: defaultTtl}, function(err) {
106
- checkErr(err);
107
-
108
- cache.mget(key, key2, function(err, result) {
109
- checkErr(err);
110
- assert.equal(result[0], value);
111
- assert.equal(result[1], value2);
112
- done();
113
- });
114
- });
115
- });
116
-
117
- it("lets us set and get data without a callback", function(done) {
118
- cache = caching({store: memoryStore.create({noPromises: true})});
119
- cache.mset(key, value, key2, value2, {ttl: defaultTtl});
120
-
121
- setTimeout(function() {
122
- var result = cache.mget(key, key2);
123
- assert.equal(result[0], value);
124
- assert.equal(result[1], value2);
125
- done();
126
- }, 20);
127
- });
128
-
129
- it("lets us set and get data without a callback, returning a promise", function(done) {
130
- cache.mset(key, value, key2, value2, {ttl: defaultTtl});
131
- setTimeout(function() {
132
- cache.mget(key, key2)
133
- .then(function(result) {
134
- assert.equal(result[0], value);
135
- assert.equal(result[1], value2);
136
- done();
137
- });
138
- }, 20);
139
- });
140
-
141
- it("lets us set and get data without options object or callback", function(done) {
142
- cache = caching({store: memoryStore.create({noPromises: true})});
143
- cache.mset(key, value, key2, value2);
144
-
145
- setTimeout(function() {
146
- var result = cache.mget(key, key2);
147
- assert.equal(result[0], value);
148
- assert.equal(result[1], value2);
149
- done();
150
- }, 20);
151
- });
152
-
153
- it("lets us pass an 'options' object", function(done) {
154
- cache = caching({store: memoryStore.create({noPromises: true})});
155
- cache.mset(key, value, key2, value2);
156
-
157
- setTimeout(function() {
158
- var result = cache.mget(key, key2, {someConfig: true});
159
- assert.equal(result[0], value);
160
- assert.equal(result[1], value2);
161
- done();
162
- }, 20);
163
- });
164
- });
165
-
166
- describe("del()", function() {
167
- ['memory'].forEach(function(store) {
168
- context("using " + store + " store", function() {
169
- beforeEach(function(done) {
170
- cache = caching({store: store});
171
- key = support.random.string(20);
172
- value = support.random.string();
173
- cache.set(key, value, {ttl: defaultTtl}, function(err) {
174
- checkErr(err);
175
- done();
176
- });
177
- });
178
-
179
- it("deletes data from cache", function(done) {
180
- cache.get(key, function(err, result) {
181
- assert.equal(result, value);
182
-
183
- cache.del(key, function(err) {
184
- checkErr(err);
185
-
186
- cache.get(key, function(err, result) {
187
- assert.ok(!result);
188
- done();
189
- });
190
- });
191
- });
192
- });
193
-
194
- it("lets us delete data without a callback", function(done) {
195
- cache.get(key, function(err, result) {
196
- assert.equal(result, value);
197
-
198
- cache.del(key);
199
-
200
- setTimeout(function() {
201
- cache.get(key, function(err, result) {
202
- assert.ok(!result);
203
- done();
204
- });
205
- }, 20);
206
- });
207
- });
208
-
209
- it("lets us delete data using promises", function(done) {
210
- key = support.random.string();
211
- cache.set(key, value)
212
- .then(function() {
213
- return cache.get(key);
214
- }).then(function(val) {
215
- assert.equal(val, value);
216
- }).then(function() {
217
- return cache.del(key);
218
- }).then(function() {
219
- return cache.get(key);
220
- }).then(function(val) {
221
- assert.strictEqual(val, undefined);
222
- }).then(done)
223
- .catch(function(err) {
224
- done(err);
225
- });
226
- });
227
-
228
- describe('with multiple keys', function() {
229
- var key2;
230
- var value2;
231
-
232
- beforeEach(function(done) {
233
- cache = caching({store: store});
234
- key2 = support.random.string(20);
235
- value2 = support.random.string();
236
- cache.mset(key, value, key2, value2, {ttl: defaultTtl}, function(err) {
237
- checkErr(err);
238
- done();
239
- });
240
- });
241
-
242
- it('deletes an unlimited number of key arguments', function(done) {
243
- cache.mget(key, key2, function(err, result) {
244
- assert.equal(result[0], value);
245
- assert.equal(result[1], value2);
246
-
247
- cache.del(key, key2, function(err) {
248
- checkErr(err);
249
-
250
- cache.mget(key, key2, function(err, result) {
251
- assert.ok(!result[0]);
252
- assert.ok(!result[1]);
253
- done();
254
- });
255
- });
256
- });
257
- });
258
-
259
- it('deletes an array of keys', function(done) {
260
- cache.mget(key, key2, function(err, result) {
261
- assert.equal(result[0], value);
262
- assert.equal(result[1], value2);
263
-
264
- cache.del([key, key2], function(err) {
265
- checkErr(err);
266
-
267
- cache.mget(key, key2, function(err, result) {
268
- assert.ok(!result[0]);
269
- assert.ok(!result[1]);
270
- done();
271
- });
272
- });
273
- });
274
- });
275
- });
276
- });
277
- });
278
- });
279
-
280
- describe("reset()", function() {
281
- var key2;
282
- var value2;
283
-
284
- beforeEach(function(done) {
285
- cache = caching({store: 'memory'});
286
- key = support.random.string(20);
287
- value = support.random.string();
288
- cache.set(key, value, function(err) {
289
- checkErr(err);
290
-
291
- key2 = support.random.string(20);
292
- value2 = support.random.string();
293
-
294
- cache.set(key2, value2, done);
295
- });
296
- });
297
-
298
- it("clears the cache", function(done) {
299
- cache.reset(function(err) {
300
- checkErr(err);
301
-
302
- cache.get(key, function(err, result) {
303
- assert.ok(!result);
304
-
305
- cache.get(key2, function(err, result) {
306
- assert.ok(!result);
307
- done();
308
- });
309
- });
310
- });
311
- });
312
-
313
- it("lets us clear the cache without a callback (memory store only)", function(done) {
314
- cache.reset();
315
- setTimeout(function() {
316
- cache.get(key, function(err, result) {
317
- assert.ok(!result);
318
-
319
- cache.get(key2, function(err, result) {
320
- assert.ok(!result);
321
- done();
322
- });
323
- });
324
- }, 10);
325
- });
326
-
327
- context("when store has no del(), mget() or mset() method", function() {
328
- var fakeStore;
329
-
330
- beforeEach(function() {
331
- fakeStore = {
332
- get: function() {},
333
- set: function() {}
334
- };
335
- });
336
-
337
- it("it doesn't throw an error", function() {
338
- assert.doesNotThrow(function() {
339
- caching({store: fakeStore});
340
- });
341
- });
342
- });
343
- });
344
-
345
- describe("setex()", function() {
346
- var fakeStore;
347
-
348
- beforeEach(function() {
349
- fakeStore = {
350
- get: function() {},
351
- set: function() {},
352
- del: function() {},
353
- setex: function() {}
354
- };
355
-
356
- sinon.stub(fakeStore, 'setex');
357
-
358
- cache = caching({store: fakeStore});
359
- });
360
-
361
- it("passes the params to the underlying store's setex() method", function() {
362
- cache.setex('foo', 'bar', 'blah');
363
- assert.ok(fakeStore.setex.calledWith('foo', 'bar', 'blah'));
364
- });
365
- });
366
-
367
- describe("ttl()", function() {
368
- var fakeStore;
369
-
370
- beforeEach(function() {
371
- fakeStore = {
372
- get: function() {},
373
- set: function() {},
374
- del: function() {},
375
- ttl: function() {}
376
- };
377
-
378
- sinon.stub(fakeStore, 'ttl');
379
-
380
- cache = caching({store: fakeStore});
381
- });
382
-
383
- it("passes the params to the underlying store's ttl() method", function() {
384
- cache.ttl('foo');
385
- assert.ok(fakeStore.ttl.calledWith('foo'));
386
- });
387
- });
388
-
389
- describe("keys()", function() {
390
- var keyCount;
391
- var savedKeys;
392
-
393
- beforeEach(function(done) {
394
- keyCount = 10;
395
- savedKeys = [];
396
- var processed = 0;
397
-
398
- cache = caching({store: 'memory'});
399
-
400
- function isDone(cb) {
401
- cb(null, processed === keyCount);
402
- }
403
-
404
- async.until(isDone, function(cb) {
405
- processed += 1;
406
- key = support.random.string(20);
407
- savedKeys.push(key);
408
- value = support.random.string();
409
- cache.set(key, value, cb);
410
- }, done);
411
- });
412
-
413
- it("calls back with all keys in cache", function(done) {
414
- cache.keys(function(err, keys) {
415
- checkErr(err);
416
- assert.deepEqual(keys.sort(), savedKeys.sort());
417
- done();
418
- });
419
- });
420
-
421
- it("lets us set and get data without a callback, returning a promise", function(done) {
422
- cache.keys()
423
- .then(function(keys) {
424
- assert.deepEqual(keys.sort(), savedKeys.sort());
425
- done();
426
- })
427
- .catch(function(err) {
428
- done(err);
429
- });
430
- });
431
-
432
- context("when not using promises", function() {
433
- beforeEach(function(done) {
434
- savedKeys = [];
435
- keyCount = 10;
436
- var processed = 0;
437
-
438
- cache = caching({store: memoryStore.create({noPromises: true})});
439
-
440
- function isDone(cb) {
441
- cb(null, processed === keyCount);
442
- }
443
-
444
- async.until(isDone, function(cb) {
445
- processed += 1;
446
- key = support.random.string(20);
447
- savedKeys.push(key);
448
- value = support.random.string();
449
- cache.set(key, value, cb);
450
- }, done);
451
- });
452
-
453
- it("lets us get the keys without a callback (memory store only)", function() {
454
- var keys = cache.keys();
455
- assert.deepEqual(keys.sort(), savedKeys.sort());
456
- });
457
- });
458
- });
459
-
460
- describe("wrap()", function() {
461
- describe("using memory (lru-cache) store", function() {
462
- var memoryStoreStub;
463
- var opts;
464
-
465
- beforeEach(function() {
466
- opts = {ttl: 0.1};
467
- memoryStoreStub = memoryStore.create(opts);
468
-
469
- sinon.stub(memoryStore, 'create').returns(memoryStoreStub);
470
-
471
- cache = caching({store: 'memory', ttl: opts.ttl, ignoreCacheErrors: false});
472
- key = support.random.string(20);
473
- name = support.random.string();
474
- });
475
-
476
- afterEach(function() {
477
- memoryStore.create.restore();
478
- });
479
-
480
- context("calls back with the result of the wrapped function", function() {
481
- beforeEach(function() {
482
- sinon.spy(memoryStoreStub, 'set');
483
- });
484
-
485
- afterEach(function() {
486
- memoryStoreStub.set.restore();
487
- });
488
-
489
- it("when a ttl is passed in", function(done) {
490
- cache.wrap(key, function(cb) {
491
- methods.getWidget(name, cb);
492
- }, opts, function(err, widget) {
493
- checkErr(err);
494
- assert.deepEqual(widget, {name: name});
495
- sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, opts);
496
- done();
497
- });
498
- });
499
-
500
- it("when a ttl is not passed in", function(done) {
501
- cache.wrap(key, function(cb) {
502
- methods.getWidget(name, cb);
503
- }, function(err, widget) {
504
- checkErr(err);
505
- assert.deepEqual(widget, {name: name});
506
- sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, {});
507
- done();
508
- });
509
- });
510
- });
511
-
512
- context("ttl function", function() {
513
- beforeEach(function() {
514
- sinon.spy(memoryStoreStub, 'set');
515
- });
516
-
517
- afterEach(function() {
518
- memoryStoreStub.set.restore();
519
- });
520
-
521
- it("when a ttl function is passed in", function(done) {
522
- opts = {
523
- ttl: function(widget) {
524
- assert.deepEqual(widget, {name: name});
525
- return 0.2;
526
- }
527
- };
528
- cache.wrap(key, function(cb) {
529
- methods.getWidget(name, cb);
530
- }, opts, function(err, widget) {
531
- checkErr(err);
532
- assert.deepEqual(widget, {name: name});
533
- sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, {ttl: 0.2});
534
- done();
535
- });
536
- });
537
- });
538
-
539
- context("when result is already cached", function() {
540
- function getCachedWidget(name, cb) {
541
- cache.wrap(key, function(cacheCb) {
542
- methods.getWidget(name, cacheCb);
543
- }, opts, cb);
544
- }
545
-
546
- beforeEach(function(done) {
547
- getCachedWidget(name, function(err, widget) {
548
- checkErr(err);
549
- assert.ok(widget);
550
-
551
- memoryStoreStub.get(key, function(err, result) {
552
- checkErr(err);
553
- assert.ok(result);
554
-
555
- sinon.spy(memoryStoreStub, 'get');
556
-
557
- done();
558
- });
559
- });
560
- });
561
-
562
- afterEach(function() {
563
- memoryStoreStub.get.restore();
564
- });
565
-
566
- it("retrieves data from cache", function(done) {
567
- var funcCalled = false;
568
-
569
- cache.wrap(key, function(cb) {
570
- methods.getWidget(name, function(err, result) {
571
- funcCalled = true;
572
- cb(err, result);
573
- });
574
- }, function(err, widget) {
575
- checkErr(err);
576
- assert.deepEqual(widget, {name: name});
577
- assert.ok(memoryStoreStub.get.calledWith(key));
578
- assert.ok(!funcCalled);
579
- done();
580
- });
581
- });
582
- });
583
-
584
- var falseyValues = [false, null, 0];
585
-
586
- falseyValues.forEach(function(falseyValue) {
587
- context("when cached value is `" + falseyValue + "`", function() {
588
- function getFalseyValue(cb) {
589
- process.nextTick(function() {
590
- cb(null, falseyValue);
591
- });
592
- }
593
-
594
- function getCachedFalseyValue(cb) {
595
- cache.wrap(key, function(cacheCb) {
596
- getFalseyValue(cacheCb);
597
- }, cb);
598
- }
599
-
600
- beforeEach(function(done) {
601
- getCachedFalseyValue(function(err, result) {
602
- checkErr(err);
603
- assert.strictEqual(result, falseyValue);
604
-
605
- memoryStoreStub.get(key, function(err, result) {
606
- checkErr(err);
607
- assert.strictEqual(result, falseyValue);
608
-
609
- sinon.spy(memoryStoreStub, 'get');
610
-
611
- done();
612
- });
613
- });
614
- });
615
-
616
- afterEach(function() {
617
- memoryStoreStub.get.restore();
618
- });
619
-
620
- it("retrieves data from cache", function(done) {
621
- getCachedFalseyValue(function(err, value) {
622
- checkErr(err);
623
- assert.strictEqual(value, falseyValue);
624
- assert.ok(memoryStoreStub.get.calledWith(key));
625
- done();
626
- });
627
- });
628
- });
629
- });
630
-
631
- context("when we pass in an isCacheableValue function to the caching constructor", function() {
632
- var testCallbacks = {
633
- isCacheableValue: function(value) {
634
- return value !== 'do_not_store_this' && value !== undefined;
635
- }
636
- };
637
-
638
- function getValue(name, cb) {
639
- process.nextTick(function() {
640
- if (name === 'foo') {
641
- cb(null, 'store_this');
642
- } else {
643
- cb(null, 'do_not_store_this');
644
- }
645
- });
646
- }
647
-
648
- function getCachedValue(name, cb) {
649
- cache.wrap(key, function(cacheCb) {
650
- getValue(name, function(err, result) {
651
- cacheCb(err, result);
652
- });
653
- }, {ttl: defaultTtl}, cb);
654
- }
655
-
656
- beforeEach(function() {
657
- sinon.spy(testCallbacks, 'isCacheableValue');
658
- cache = caching({store: 'memory', isCacheableValue: testCallbacks.isCacheableValue});
659
- sinon.spy(memoryStoreStub, 'set');
660
- });
661
-
662
- afterEach(function() {
663
- memoryStoreStub.set.restore();
664
- testCallbacks.isCacheableValue.restore();
665
- });
666
-
667
- it("stores allowed values", function(done) {
668
- var name = 'foo';
669
-
670
- getCachedValue(name, function(err) {
671
- checkErr(err);
672
- assert.ok(memoryStoreStub.set.called);
673
- assert.ok(testCallbacks.isCacheableValue.called);
674
-
675
- getCachedValue(name, function(err) {
676
- checkErr(err);
677
- done();
678
- });
679
- });
680
- });
681
-
682
- it("does not store non-allowed values", function(done) {
683
- var name = 'bar';
684
-
685
- getCachedValue(name, function(err) {
686
- checkErr(err);
687
- assert.ok(memoryStoreStub.set.notCalled);
688
- assert.ok(testCallbacks.isCacheableValue.called);
689
- done();
690
- });
691
- });
692
- });
693
-
694
- context("when callback called twice by client", function() {
695
- it("it does not throw an error", function(done) {
696
- cache.wrap(key, function(cb) {
697
- methods.getWidget(name, cb);
698
- methods.getWidget(name, cb);
699
- }, opts, function(err, widget) {
700
- checkErr(err);
701
- assert.deepEqual(widget, {name: name});
702
- setTimeout(done, 10);
703
- });
704
- });
705
- });
706
-
707
- it("lets us make nested calls", function(done) {
708
- function getCachedWidget(name, cb) {
709
- cache.wrap(key, function(cacheCb) {
710
- methods.getWidget(name, cacheCb);
711
- }, cb);
712
- }
713
-
714
- getCachedWidget(name, function(err, widget) {
715
- checkErr(err);
716
- assert.equal(widget.name, name);
717
-
718
- getCachedWidget(name, function(err, widget) {
719
- checkErr(err);
720
- assert.equal(widget.name, name);
721
-
722
- getCachedWidget(name, function(err, widget) {
723
- checkErr(err);
724
- assert.equal(widget.name, name);
725
- done();
726
- });
727
- });
728
- });
729
- });
730
-
731
- it("expires cached result after ttl seconds", function(done) {
732
- var ttl = 0.1;
733
-
734
- cache.wrap(key, function(cb) {
735
- methods.getWidget(name, cb);
736
- }, {ttl: ttl}, function(err, widget) {
737
- checkErr(err);
738
- assert.deepEqual(widget, {name: name});
739
-
740
- memoryStoreStub.get(key, function(err, result) {
741
- checkErr(err);
742
- assert.ok(result);
743
-
744
- var funcCalled = false;
745
-
746
- setTimeout(function() {
747
- cache.wrap(key, function(cb) {
748
- methods.getWidget(name, function(err, result) {
749
- funcCalled = true;
750
- cb(err, result);
751
- });
752
- }, function(err, widget) {
753
- checkErr(err);
754
- assert.ok(funcCalled);
755
- assert.deepEqual(widget, {name: name});
756
- done();
757
- });
758
- }, (ttl * 1000 + 10));
759
- });
760
- });
761
- });
762
-
763
- context("when an error is thrown in the work function", function() {
764
- var fakeError;
765
-
766
- beforeEach(function() {
767
- fakeError = new Error(support.random.string());
768
- });
769
-
770
- it("does not catch the error", function(done) {
771
- var originalExceptionHandler = process.listeners('uncaughtException').pop();
772
- process.removeListener('uncaughtException', originalExceptionHandler);
773
-
774
- process.once('uncaughtException', function(err) {
775
- process.on('uncaughtException', originalExceptionHandler);
776
- assert.ok(err);
777
- done();
778
- });
779
-
780
- cache.wrap(key, function() {
781
- throw fakeError;
782
- }, function() {
783
- done(new Error('Should not have caught error'));
784
- });
785
- });
786
- });
787
-
788
- context("when store.get() calls back with an error", function() {
789
- context("and ignoreCacheErrors is not set (default is false)", function() {
790
- it("bubbles up that error", function(done) {
791
- var fakeError = new Error(support.random.string());
792
-
793
- sinon.stub(memoryStoreStub, 'get').callsFake(function(key, options, cb) {
794
- cb(fakeError);
795
- });
796
-
797
- cache.wrap(key, function(cb) {
798
- methods.getWidget(name, cb);
799
- }, function(err) {
800
- assert.equal(err, fakeError);
801
- memoryStoreStub.get.restore();
802
- done();
803
- });
804
- });
805
- });
806
-
807
- context("and ignoreCacheErrors is set to true", function() {
808
- it("does not bubble up that error", function(done) {
809
- cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: true});
810
-
811
- var fakeError = new Error(support.random.string());
812
-
813
- sinon.stub(memoryStoreStub, 'get').callsFake(function(key, options, cb) {
814
- cb(fakeError);
815
- });
816
-
817
- cache.wrap(key, function(cb) {
818
- methods.getWidget(name, cb);
819
- }, function(err) {
820
- assert.equal(err, null);
821
- memoryStoreStub.get.restore();
822
- done();
823
- });
824
- });
825
- });
826
- });
827
-
828
- context("when store.set() calls back with an error", function() {
829
- context("and ignoreCacheErrors is not set", function() {
830
- it("bubbles up that error", function(done) {
831
- var fakeError = new Error(support.random.string());
832
-
833
- sinon.stub(memoryStoreStub, 'set').callsFake(function(key, val, ttl, cb) {
834
- cb(fakeError);
835
- });
836
-
837
- cache.wrap(key, function(cb) {
838
- methods.getWidget(name, cb);
839
- }, function(err) {
840
- assert.equal(err, fakeError);
841
- memoryStoreStub.set.restore();
842
- done();
843
- });
844
- });
845
- });
846
-
847
- context("and ignoreCacheErrors is set to true", function() {
848
- it("does not bubbles up that error", function(done) {
849
- cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: true});
850
- var fakeError = new Error(support.random.string());
851
-
852
- sinon.stub(memoryStoreStub, 'set').yields(fakeError);
853
-
854
- cache.wrap(key, function(cb) {
855
- methods.getWidget(name, cb);
856
- }, function(err) {
857
- assert.equal(err, null);
858
- memoryStoreStub.set.restore();
859
- done();
860
- });
861
- });
862
- });
863
- });
864
-
865
- context("when wrapped function calls back with an error", function() {
866
- it("calls back with that error", function(done) {
867
- var fakeError = new Error(support.random.string());
868
- sinon.stub(methods, 'getWidget').callsFake(function(name, cb) {
869
- cb(fakeError, {name: name});
870
- });
871
-
872
- cache.wrap(key, function(cb) {
873
- methods.getWidget(name, cb);
874
- }, function(err, widget) {
875
- methods.getWidget.restore();
876
- assert.equal(err, fakeError);
877
- assert.ok(!widget);
878
- done();
879
- });
880
- });
881
- });
882
-
883
- context("when wrapped function returns a non cacheable value once", function() {
884
- it("second call to 'wrap' triggers the callback", function(done) {
885
- var key = support.random.string();
886
-
887
- // 1.
888
- cache.wrap(key, function(cb) {
889
- cb(null, undefined);
890
- }, function(err, result) {
891
- assert.equal(result, undefined);
892
- });
893
-
894
- // 2.
895
- cache.wrap(key, function(cb) {
896
- cb(null, undefined);
897
- }, function(err, result) {
898
- assert.equal(result, undefined);
899
- done();
900
- });
901
- });
902
- });
903
-
904
- context("when passing multiple keys", function() {
905
- var key2;
906
- var name2;
907
-
908
- beforeEach(function() {
909
- key2 = '2-' + support.random.string(20);
910
- name2 = '2-' + support.random.string();
911
- sinon.spy(memoryStoreStub, 'mset');
912
- });
913
-
914
- afterEach(function() {
915
- memoryStoreStub.mset.restore();
916
- });
917
-
918
- context("when no keys are already cached", function() {
919
- it("retrieves data from data store and caches results via mset", function(done) {
920
- var funcCalled = false;
921
- sinon.stub(memoryStoreStub, 'mget').yields();
922
-
923
- cache.wrap(key, key2, function(cb) {
924
- funcCalled = true;
925
- cb(null, [{name: name}, {name: name2}]);
926
- }, function(err) {
927
- checkErr(err);
928
- assert.ok(memoryStoreStub.mget.calledWith(key, key2));
929
- assert.ok(funcCalled);
930
- assert.ok(memoryStoreStub.mset.called);
931
- var callArgs = memoryStoreStub.mset.args[0];
932
- assert.equal(callArgs[0], key);
933
- assert.deepEqual(callArgs[1], {name: name});
934
- assert.equal(callArgs[2], key2);
935
- assert.deepEqual(callArgs[3], {name: name2});
936
- memoryStoreStub.mget.restore();
937
- done();
938
- });
939
- });
940
- });
941
-
942
- context("when result is already cached", function() {
943
- it("retrieves data from cache", function(done) {
944
- var funcCalled = false;
945
- sinon.stub(memoryStoreStub, 'mget').callsFake(function() {
946
- var args = Array.prototype.slice.apply(arguments);
947
- var cb = args.pop();
948
- cb(null, [{name: name}, {name: name2}]);
949
- });
950
-
951
- cache.wrap(key, key2, function(cb) {
952
- funcCalled = true;
953
- cb();
954
- }, function(err, widgets) {
955
- checkErr(err);
956
- assert.deepEqual(widgets[0], {name: name});
957
- assert.deepEqual(widgets[1], {name: name2});
958
- assert.ok(memoryStoreStub.mget.calledWith(key, key2));
959
- assert.ok(!funcCalled);
960
- memoryStoreStub.mget.restore();
961
- done();
962
- });
963
- });
964
- });
965
-
966
- context("when some keys are already cached but others are not", function() {
967
- // This means the user must manually call the data store to get any
968
- // values that are not in cache, which is not ideal.
969
- it("responds with only the data that exists in the cache", function(done) {
970
- var key3 = support.random.string();
971
- var name3 = support.random.string();
972
- var funcCalled = false;
973
-
974
- sinon.stub(memoryStoreStub, 'mget').yields(null, [{name: name}, {name: name3}]);
975
-
976
- cache.wrap(key, key2, key3, function(cb) {
977
- funcCalled = true;
978
- methods.getMultiWidget([name2], cb);
979
- }, function(err, widgets) {
980
- checkErr(err);
981
- assert.deepEqual(widgets[0], {name: name});
982
- assert.deepEqual(widgets[1], {name: name3});
983
- assert.ok(memoryStoreStub.mget.calledWith(key, key2));
984
- assert.ok(!funcCalled);
985
- memoryStoreStub.mget.restore();
986
- done();
987
- });
988
- });
989
- });
990
-
991
- it("when a ttl is passed in", function(done) {
992
- cache.wrap(key, key2, function(cb) {
993
- methods.getMultiWidget([name, name2], cb);
994
- }, opts, function(err, widgets) {
995
- checkErr(err);
996
- assert.deepEqual(widgets[0], {name: name});
997
- assert.deepEqual(widgets[1], {name: name2});
998
- sinon.assert.calledWith(memoryStoreStub.mset, key, {name: name}, key2, {name: name2}, opts);
999
- done();
1000
- });
1001
- });
1002
-
1003
- it("when a ttl is passed in (function)", function(done) {
1004
- var ttlFunc = function() { return 1234; };
1005
- opts = {ttl: ttlFunc};
1006
-
1007
- cache.wrap(key, key2, function(cb) {
1008
- methods.getMultiWidget([name, name2], cb);
1009
- }, opts, function(err) {
1010
- checkErr(err);
1011
- sinon.assert.calledWith(
1012
- memoryStoreStub.mset, key, {name: name}, key2, {name: name2}, {ttl: 1234}
1013
- );
1014
- done();
1015
- });
1016
- });
1017
-
1018
- it("when a ttl is not passed in", function(done) {
1019
- cache.wrap(key, key2, function(cb) {
1020
- methods.getMultiWidget([name, name2], cb);
1021
- }, function(err, widgets) {
1022
- checkErr(err);
1023
- assert.deepEqual(widgets[0], {name: name});
1024
- assert.deepEqual(widgets[1], {name: name2});
1025
- sinon.assert.calledWith(memoryStoreStub.mset, key, {name: name}, key2, {name: name2}, {});
1026
- done();
1027
- });
1028
- });
1029
-
1030
- it("does not store non-allowed values", function(done) {
1031
- var name = 'bar';
1032
-
1033
- cache = caching({store: 'memory', isCacheableValue: function() { return false; }});
1034
- cache.wrap(key, key2, function(cb) {
1035
- methods.getMultiWidget([name, name2], cb);
1036
- }, function(err) {
1037
- checkErr(err);
1038
- assert.ok(memoryStoreStub.mset.notCalled);
1039
- done();
1040
- });
1041
- });
1042
-
1043
- context("when store.mget() calls back with an error", function() {
1044
- context("and ignoreCacheErrors is not set (default is false)", function() {
1045
- it("bubbles up that error", function(done) {
1046
- var fakeError = new Error(support.random.string());
1047
-
1048
- sinon.stub(memoryStoreStub, 'mget').callsFake(function() {
1049
- var args = Array.prototype.slice.apply(arguments);
1050
- var cb = args.pop();
1051
- cb(fakeError);
1052
- });
1053
-
1054
- cache.wrap(key, key2, function(cb) {
1055
- methods.getMultiWidget([name, name2], cb);
1056
- }, function(err) {
1057
- assert.equal(err, fakeError);
1058
- memoryStoreStub.mget.restore();
1059
- done();
1060
- });
1061
- });
1062
- });
1063
-
1064
- context("and ignoreCacheErrors is set to true", function() {
1065
- it("does not bubble up that error", function(done) {
1066
- cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: true});
1067
-
1068
- var fakeError = new Error(support.random.string());
1069
-
1070
- sinon.stub(memoryStoreStub, 'mget').callsFake(function() {
1071
- var args = Array.prototype.slice.apply(arguments);
1072
- var cb = args.pop();
1073
- cb(fakeError);
1074
- });
1075
-
1076
- cache.wrap(key, key2, function(cb) {
1077
- methods.getMultiWidget([name, name2], cb);
1078
- }, function(err) {
1079
- assert.equal(err, null);
1080
- memoryStoreStub.mget.restore();
1081
- done();
1082
- });
1083
- });
1084
- });
1085
- });
1086
-
1087
- context("when an error is thrown in the work function", function() {
1088
- var fakeError;
1089
-
1090
- beforeEach(function() {
1091
- fakeError = new Error(support.random.string());
1092
- });
1093
-
1094
- it("does catch the error ", function(done) {
1095
- cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: false});
1096
-
1097
- cache.wrap(key, key2, function(cb) {
1098
- return cb(fakeError);
1099
- }, function(err) {
1100
- assert.equal(err, fakeError);
1101
- done();
1102
- });
1103
- });
1104
- });
1105
- });
1106
- });
1107
-
1108
- describe("using memory (lru-cache) store with a refreshThreshold", function() {
1109
- var memoryStoreStub;
1110
- var opts;
1111
-
1112
- beforeEach(function() {
1113
- opts = {ttl: 10};
1114
- memoryStoreStub = memoryStore.create(opts);
1115
-
1116
- sinon.stub(memoryStore, 'create').returns(memoryStoreStub);
1117
-
1118
- cache = caching({store: 'memory', ttl: opts.ttl, refreshThreshold: 2, ignoreCacheErrors: false});
1119
- key = support.random.string(20);
1120
- name = support.random.string();
1121
- });
1122
-
1123
- afterEach(function() {
1124
- memoryStore.create.restore();
1125
- });
1126
-
1127
- function getCachedWidget(name, cb) {
1128
- cache.wrap(key, function(cacheCb) {
1129
- methods.getWidget(name, cacheCb);
1130
- }, opts, cb);
1131
- }
1132
-
1133
- context("using standard memorystore", function() {
1134
- beforeEach(function(done) {
1135
- getCachedWidget(name, function(err, widget) {
1136
- checkErr(err);
1137
- assert.ok(widget);
1138
-
1139
- memoryStoreStub.get(key, function(err, result) {
1140
- checkErr(err);
1141
- assert.ok(result);
1142
-
1143
- sinon.spy(cache, 'checkRefreshThreshold');
1144
- done();
1145
- });
1146
- });
1147
- });
1148
-
1149
- afterEach(function() {
1150
- cache.checkRefreshThreshold.restore();
1151
- });
1152
-
1153
- it("retrieves data from cache and stops background if failing while checking ttl", function(done) {
1154
- // NOTE: TTL function is not defined
1155
- var funcCalled = false;
1156
- cache.wrap(key, function(cb) {
1157
- funcCalled = true;
1158
- methods.getWidget(name, function(err, result) {
1159
- cb(err, result);
1160
- });
1161
- }, function(err, widget) {
1162
- checkErr(err);
1163
- assert.deepEqual(widget, {name: name});
1164
- assert.equal(cache.checkRefreshThreshold.callCount, 1);
1165
- assert.equal(funcCalled, false);
1166
-
1167
- done();
1168
- });
1169
- });
1170
- });
1171
-
1172
- context("using tweaked memory store, when result is already cached", function() {
1173
- beforeEach(function(done) {
1174
- memoryStoreStub.ttl = function(key, cb) {
1175
- return cb(null, 5);
1176
- };
1177
- sinon.spy(memoryStoreStub, 'ttl');
1178
- getCachedWidget(name, function(err, widget) {
1179
- checkErr(err);
1180
- assert.ok(widget);
1181
-
1182
- memoryStoreStub.get(key, function(err, result) {
1183
- checkErr(err);
1184
- assert.ok(result);
1185
-
1186
- sinon.spy(memoryStoreStub, 'get');
1187
- sinon.spy(memoryStoreStub, 'set');
1188
-
1189
- done();
1190
- });
1191
- });
1192
- });
1193
-
1194
- afterEach(function() {
1195
- memoryStoreStub.get.restore();
1196
- memoryStoreStub.set.restore();
1197
- memoryStoreStub.ttl.restore();
1198
- });
1199
-
1200
- it("retrieves data from cache and checks ttl without refreshing", function(done) {
1201
- var funcCalled = false;
1202
- cache.wrap(key, function(cb) {
1203
- funcCalled = true;
1204
- methods.getWidget(name, function(err, result) {
1205
- cb(err, result);
1206
- });
1207
- }, function(err, widget) {
1208
- checkErr(err);
1209
- assert.deepEqual(widget, {name: name});
1210
- assert.ok(memoryStoreStub.get.calledWith(key));
1211
- assert.ok(memoryStoreStub.ttl.calledWith(key));
1212
- assert.equal(funcCalled, false);
1213
- assert.equal(memoryStoreStub.set.callCount, 0);
1214
- done();
1215
- });
1216
- });
1217
-
1218
- it("retrieves data from cache and checks twice ttl without refreshing", function(done) {
1219
- var funcCalled = 0;
1220
- var values = [];
1221
-
1222
- for (var i = 0; i < 2; i++) {
1223
- values.push(i);
1224
- }
1225
-
1226
- async.eachSeries(values, function(val, next) {
1227
- cache.wrap(key, function(cb) {
1228
- funcCalled += 1;
1229
- methods.getWidget(name, function(err, result) {
1230
- cb(err, result);
1231
- });
1232
- }, function(err, widget) {
1233
- checkErr(err);
1234
- assert.deepEqual(widget, {name: name});
1235
- next(err);
1236
- });
1237
- }, function(err) {
1238
- // Wait for just a bit, to be sure that the callback is called.
1239
- setTimeout(function() {
1240
- checkErr(err);
1241
- assert.equal(memoryStoreStub.get.callCount, 2);
1242
- assert.equal(memoryStoreStub.ttl.callCount, 2);
1243
- assert.equal(funcCalled, 0);
1244
- assert.equal(memoryStoreStub.set.callCount, 0);
1245
- done();
1246
- }, 500);
1247
- });
1248
- });
1249
- });
1250
-
1251
- context("using tweaked memorystore, when result is already cached but expiring", function() {
1252
- beforeEach(function(done) {
1253
- memoryStoreStub.ttl = function(key, cb) {
1254
- return cb(null, 1);
1255
- };
1256
- sinon.spy(memoryStoreStub, 'ttl');
1257
-
1258
- getCachedWidget(name, function(err, widget) {
1259
- checkErr(err);
1260
- assert.ok(widget);
1261
-
1262
- memoryStoreStub.get(key, function(err, result) {
1263
- checkErr(err);
1264
- assert.ok(result);
1265
-
1266
- sinon.spy(memoryStoreStub, 'get');
1267
- sinon.spy(memoryStoreStub, 'set');
1268
-
1269
- done();
1270
- });
1271
- });
1272
- });
1273
-
1274
- afterEach(function() {
1275
- memoryStoreStub.get.restore();
1276
- memoryStoreStub.set.restore();
1277
- memoryStoreStub.ttl.restore();
1278
- });
1279
-
1280
- it("returns value and invokes worker in background", function(done) {
1281
- var funcCalled = false;
1282
- cache.wrap(key, function(cb) {
1283
- funcCalled = true;
1284
- methods.getWidget(name, function(err, result) {
1285
- cb(err, result);
1286
- });
1287
- }, function(err, widget) {
1288
- // Wait for just a bit, to be sure that the callback is called.
1289
- setTimeout(function() {
1290
- checkErr(err);
1291
- assert.deepEqual(widget, {name: name});
1292
- assert.ok(memoryStoreStub.get.calledWith(key));
1293
- assert.ok(memoryStoreStub.ttl.calledWith(key));
1294
- assert.equal(funcCalled, true);
1295
- assert.equal(memoryStoreStub.set.callCount, 1);
1296
- done();
1297
- }, 500);
1298
- });
1299
- });
1300
-
1301
- it("returns value and invokes worker in background using the provided ttl function", function(done) {
1302
- var funcCalled = false;
1303
- var ttlFuncCalled = false;
1304
- var ttlFunc = function() {
1305
- ttlFuncCalled = true;
1306
- return 4;
1307
- };
1308
-
1309
- cache.wrap(key, function(cb) {
1310
- funcCalled = true;
1311
- methods.getWidget(name, function(err, result) {
1312
- cb(err, result);
1313
- });
1314
- }, {ttl: ttlFunc}, function(err, widget) {
1315
- // Wait for just a bit, to be sure that the callback is called.
1316
- setTimeout(function() {
1317
- checkErr(err);
1318
- assert.deepEqual(widget, {name: name});
1319
- assert.ok(memoryStoreStub.get.calledWith(key));
1320
- assert.ok(memoryStoreStub.ttl.calledWith(key));
1321
- assert.equal(funcCalled, true);
1322
- assert.equal(ttlFuncCalled, true);
1323
- assert.equal(memoryStoreStub.set.callCount, 1);
1324
- done();
1325
- }, 500);
1326
- });
1327
- });
1328
-
1329
- it("returns value and invokes worker in background once when called multiple times", function(done) {
1330
- var funcCalled = 0;
1331
- var values = [];
1332
- for (var i = 0; i < 2; i++) {
1333
- values.push(i);
1334
- }
1335
-
1336
- async.each(values, function(val, next) {
1337
- cache.wrap(key, function(cb) {
1338
- funcCalled += 1;
1339
- var timeout = support.random.number(100);
1340
- setTimeout(function() {
1341
- methods.getWidget(name, function(err, result) {
1342
- cb(err, result);
1343
- });
1344
- }, timeout);
1345
- }, function(err, widget) {
1346
- checkErr(err);
1347
- assert.deepEqual(widget, {name: name});
1348
- next(err);
1349
- });
1350
- }, function(err) {
1351
- // Wait for just a bit, to be sure that the callback is called.
1352
- setTimeout(function() {
1353
- checkErr(err);
1354
- assert.ok(memoryStoreStub.get.calledWith(key));
1355
- assert.equal(memoryStoreStub.ttl.callCount, 1);
1356
- assert.equal(funcCalled, 1);
1357
- assert.equal(memoryStoreStub.set.callCount, 1);
1358
- done();
1359
- }, 500);
1360
- });
1361
- });
1362
-
1363
- it("returns value and invokes worker in background discarding result if error", function(done) {
1364
- var funcCalled = false;
1365
- var fakeError = new Error(support.random.string());
1366
- cache.wrap(key, function(cb) {
1367
- funcCalled = true;
1368
- cb(fakeError);
1369
- }, function(err, widget) {
1370
- // Wait for just a bit, to be sure that the callback is called.
1371
- setTimeout(function() {
1372
- checkErr(err);
1373
- assert.deepEqual(widget, {name: name});
1374
- assert.ok(memoryStoreStub.get.calledWith(key));
1375
- assert.ok(memoryStoreStub.ttl.calledWith(key));
1376
- assert.equal(funcCalled, true);
1377
- assert.equal(memoryStoreStub.set.callCount, 0);
1378
- done();
1379
- }, 500);
1380
- });
1381
- });
1382
- });
1383
- });
1384
-
1385
- describe("when called multiple times in parallel with same key", function() {
1386
- var construct;
1387
- var constructMultiple;
1388
-
1389
- beforeEach(function() {
1390
- cache = caching({
1391
- store: 'memory',
1392
- max: 50,
1393
- ttl: 5 * 60
1394
- });
1395
-
1396
- construct = sinon.spy(function(val, cb) {
1397
- var timeout = support.random.number(100);
1398
- setTimeout(function() {
1399
- cb(null, 'value');
1400
- }, timeout);
1401
- });
1402
-
1403
- constructMultiple = sinon.spy(function(val, cb) {
1404
- var timeout = support.random.number(100);
1405
- setTimeout(function() {
1406
- cb(null, ['value', 'value2']);
1407
- }, timeout);
1408
- });
1409
- });
1410
-
1411
- it("calls the wrapped function once", function(done) {
1412
- var values = [];
1413
- for (var i = 0; i < 2; i++) {
1414
- values.push(i);
1415
- }
1416
-
1417
- async.each(values, function(val, next) {
1418
- cache.wrap('key', function(cb) {
1419
- construct(val, cb);
1420
- }, function(err, result) {
1421
- assert.equal(result, 'value');
1422
- next(err);
1423
- });
1424
- }, function(err) {
1425
- checkErr(err);
1426
- assert.equal(construct.callCount, 1);
1427
- done();
1428
- });
1429
- });
1430
-
1431
- it("calls the multiWrapped function once", function(done) {
1432
- var values = [];
1433
- for (var i = 0; i < 2; i++) {
1434
- values.push(i);
1435
- }
1436
-
1437
- async.each(values, function(val, next) {
1438
- cache.wrap('key', 'key2', function(cb) {
1439
- constructMultiple(val, cb);
1440
- }, function(err, results) {
1441
- assert.equal(results[0], 'value');
1442
- assert.equal(results[1], 'value2');
1443
- next(err);
1444
- });
1445
- }, function(err) {
1446
- checkErr(err);
1447
- assert.equal(constructMultiple.callCount, 1);
1448
- done();
1449
- });
1450
- });
1451
- });
1452
-
1453
- describe("using native promises", function() {
1454
- beforeEach(function() {
1455
- cache = caching({
1456
- store: 'memory',
1457
- max: 50,
1458
- ttl: 5 * 60
1459
- });
1460
- });
1461
-
1462
- it("should be able to chain with simple promise", function(done) {
1463
- cache.wrap('key', function() {
1464
- return 'OK';
1465
- }).then(function(res) {
1466
- assert.equal(res, 'OK');
1467
- done();
1468
- });
1469
- });
1470
-
1471
- it("should be able to chain with cache function as a promise", function(done) {
1472
- cache.wrap('key', function() {
1473
- return new Promise(function(resolve) {
1474
- resolve('OK');
1475
- });
1476
- }).then(function(res) {
1477
- assert.equal(res, 'OK');
1478
- done();
1479
- });
1480
- });
1481
-
1482
- it("should be able use a ttl while chaining cache function as a promise", function(done) {
1483
- cache.wrap('key', function() {
1484
- return new Promise(function(resolve) {
1485
- resolve('OK');
1486
- });
1487
- }, {ttl: 10}).then(function(res) {
1488
- assert.equal(res, 'OK');
1489
- done();
1490
- });
1491
- });
1492
-
1493
- it("should be able to catch errors in cache function as a promise", function(done) {
1494
- cache.wrap('key', function() {
1495
- return new Promise(function(resolve, reject) {
1496
- reject('NOK');
1497
- });
1498
- }).then(function() {
1499
- done(new Error('It should not call "then" since there is an error in the cache function!'));
1500
- }).catch(function() {
1501
- done();
1502
- });
1503
- });
1504
-
1505
- it("should be able to chain with non-cacheable value", function(done) {
1506
- cache.wrap('key', function() {
1507
- return;
1508
- }).then(function(res) {
1509
- assert.equal(res, undefined);
1510
- done();
1511
- });
1512
- });
1513
- });
1514
- });
1515
-
1516
- describe("instantiating with no store passed in", function() {
1517
- it("defaults to 'memory' store", function() {
1518
- var cache = caching();
1519
- assert.equal(cache.store.name, 'memory');
1520
- });
1521
- });
1522
-
1523
- describe("instantiating with custom store", function() {
1524
- it("allows us to pass in our own store object", function(done) {
1525
- var store = memoryStore.create({ttl: defaultTtl});
1526
- cache = caching({store: store});
1527
- cache.set(key, value, function(err) {
1528
- checkErr(err);
1529
- cache.get(key, function(err, result) {
1530
- assert.equal(result, value);
1531
- done();
1532
- });
1533
- });
1534
- });
1535
-
1536
- it("allows us to pass in a module (uninstantiated)", function(done) {
1537
- var store = memoryStore;
1538
- cache = caching({store: store});
1539
- cache.set(key, value, {ttl: defaultTtl}, function(err) {
1540
- checkErr(err);
1541
- cache.get(key, function(err, result) {
1542
- assert.equal(result, value);
1543
- done();
1544
- });
1545
- });
1546
- });
1547
- });
1548
-
1549
- describe("overloading with custom store", function() {
1550
- it("allows us to override isCacheableValue", function(done) {
1551
- var store = memoryStore.create({ttl: defaultTtl});
1552
- var onlyOne = true;
1553
- store.isCacheableValue = function(result) {
1554
- if (onlyOne) {
1555
- onlyOne = false;
1556
- done();
1557
- }
1558
- return result !== undefined;
1559
- };
1560
- cache = caching({store: store});
1561
- cache.wrap(key, function(cb) {
1562
- methods.getWidget(name, cb);
1563
- }, function(err, widget) {
1564
- checkErr(err);
1565
- assert.deepEqual(widget, {name: name});
1566
- });
1567
- });
1568
- });
1569
-
1570
- describe("when using store's isCacheableValue method", function() {
1571
- it("should not break its' context", function() {
1572
- var store = {
1573
- isCacheableValue: function() {
1574
- if (this !== store) {
1575
- throw new Error("Broken store context");
1576
- }
1577
- },
1578
- get: function() {},
1579
- set: function() {}
1580
- };
1581
-
1582
- cache = caching({store: store});
1583
-
1584
- assert.doesNotThrow(cache._isCacheableValue);
1585
- });
1586
- });
1587
- });