UNPKG

19.7 kBJavaScriptView Raw
1// Load modules
2
3var Lab = require('lab');
4var Catbox = require('catbox');
5var Memory = require('..');
6
7
8// Declare internals
9
10var internals = {};
11
12
13// Test shortcuts
14
15var expect = Lab.expect;
16var before = Lab.before;
17var after = Lab.after;
18var describe = Lab.experiment;
19var it = Lab.test;
20
21
22describe('Memory', function () {
23
24 it('throws an error if not created with new', function (done) {
25
26 var fn = function () {
27
28 Memory();
29 };
30
31 expect(fn).to.throw(Error);
32 done();
33 });
34
35 it('creates a new connection', function (done) {
36
37 var client = new Catbox.Client(Memory);
38 client.start(function (err) {
39
40 expect(client.isReady()).to.equal(true);
41 done();
42 });
43 });
44
45 it('closes the connection', function (done) {
46
47 var client = new Catbox.Client(Memory);
48 client.start(function (err) {
49
50 expect(client.isReady()).to.equal(true);
51 client.stop();
52 expect(client.isReady()).to.equal(false);
53 done();
54 });
55 });
56
57 it('gets an item after setting it', function (done) {
58
59 var client = new Catbox.Client(Memory);
60 client.start(function (err) {
61
62 var key = { id: 'x', segment: 'test' };
63 client.set(key, '123', 500, function (err) {
64
65 expect(err).to.not.exist;
66 client.get(key, function (err, result) {
67
68 expect(err).to.equal(null);
69 expect(result.item).to.equal('123');
70 done();
71 });
72 });
73 });
74 });
75
76 it('gets an item after setting it (no memory limit)', function (done) {
77
78 var client = new Catbox.Client(new Memory({ maxByteSize: 0 }));
79 client.start(function (err) {
80
81 var key = { id: 'x', segment: 'test' };
82 client.set(key, '123', 500, function (err) {
83
84 expect(err).to.not.exist;
85 client.get(key, function (err, result) {
86
87 expect(err).to.equal(null);
88 expect(result.item).to.equal('123');
89
90 client.set(key, '345', 500, function (err) {
91
92 expect(err).to.not.exist;
93 client.get(key, function (err, result) {
94
95 expect(err).to.equal(null);
96 expect(result.item).to.equal('345');
97 done();
98 });
99 });
100 });
101 });
102 });
103 });
104
105 it('fails setting an item circular references', function (done) {
106
107 var client = new Catbox.Client(Memory);
108 client.start(function (err) {
109
110 var key = { id: 'x', segment: 'test' };
111 var value = { a: 1 };
112 value.b = value;
113 client.set(key, value, 10, function (err) {
114
115 expect(err.message).to.equal('Converting circular structure to JSON');
116 done();
117 });
118 });
119 });
120
121 it('fails setting an item with very long ttl', function (done) {
122
123 var client = new Catbox.Client(Memory);
124 client.start(function (err) {
125
126 var key = { id: 'x', segment: 'test' };
127 client.set(key, '123', Math.pow(2, 31), function (err) {
128
129 expect(err.message).to.equal('Invalid ttl (greater than 2147483647)');
130 done();
131 });
132 });
133 });
134
135 it('ignored starting a connection twice on same event', function (done) {
136
137 var client = new Catbox.Client(Memory);
138 var x = 2;
139 var start = function () {
140
141 client.start(function (err) {
142
143 expect(client.isReady()).to.equal(true);
144 --x;
145 if (!x) {
146 done();
147 }
148 });
149 };
150
151 start();
152 start();
153 });
154
155 it('ignored starting a connection twice chained', function (done) {
156
157 var client = new Catbox.Client(Memory);
158 client.start(function (err) {
159
160 expect(err).to.not.exist;
161 expect(client.isReady()).to.equal(true);
162
163 client.start(function (err) {
164
165 expect(err).to.not.exist;
166 expect(client.isReady()).to.equal(true);
167 done();
168 });
169 });
170 });
171
172 it('returns not found on get when using null key', function (done) {
173
174 var client = new Catbox.Client(Memory);
175 client.start(function (err) {
176
177 client.get(null, function (err, result) {
178
179 expect(err).to.equal(null);
180 expect(result).to.equal(null);
181 done();
182 });
183 });
184 });
185
186 it('returns not found on get when item expired', function (done) {
187
188 var client = new Catbox.Client(Memory);
189 client.start(function (err) {
190
191 var key = { id: 'x', segment: 'test' };
192 client.set(key, 'x', 1, function (err) {
193
194 expect(err).to.not.exist;
195 setTimeout(function () {
196
197 client.get(key, function (err, result) {
198
199 expect(err).to.equal(null);
200 expect(result).to.equal(null);
201 done();
202 });
203 }, 2);
204 });
205 });
206 });
207
208 it('errors on set when using null key', function (done) {
209
210 var client = new Catbox.Client(Memory);
211 client.start(function (err) {
212
213 client.set(null, {}, 1000, function (err) {
214
215 expect(err instanceof Error).to.equal(true);
216 done();
217 });
218 });
219 });
220
221 it('errors on get when using invalid key', function (done) {
222
223 var client = new Catbox.Client(Memory);
224 client.start(function (err) {
225
226 client.get({}, function (err) {
227
228 expect(err instanceof Error).to.equal(true);
229 done();
230 });
231 });
232 });
233
234 it('errors on set when using invalid key', function (done) {
235
236 var client = new Catbox.Client(Memory);
237 client.start(function (err) {
238
239 client.set({}, {}, 1000, function (err) {
240
241 expect(err instanceof Error).to.equal(true);
242 done();
243 });
244 });
245 });
246
247 it('ignores set when using non-positive ttl value', function (done) {
248
249 var client = new Catbox.Client(Memory);
250 client.start(function (err) {
251
252 var key = { id: 'x', segment: 'test' };
253 client.set(key, 'y', 0, function (err) {
254
255 expect(err).to.not.exist;
256 done();
257 });
258 });
259 });
260
261 it('errors on get when stopped', function (done) {
262
263 var client = new Catbox.Client(Memory);
264 client.stop();
265 var key = { id: 'x', segment: 'test' };
266 client.connection.get(key, function (err, result) {
267
268 expect(err).to.exist;
269 expect(result).to.not.exist;
270 done();
271 });
272 });
273
274 it('errors on set when stopped', function (done) {
275
276 var client = new Catbox.Client(Memory);
277 client.stop();
278 var key = { id: 'x', segment: 'test' };
279 client.connection.set(key, 'y', 1, function (err) {
280
281 expect(err).to.exist;
282 done();
283 });
284 });
285
286 it('errors on missing segment name', function (done) {
287
288 var config = {
289 expiresIn: 50000
290 };
291 var fn = function () {
292
293 var client = new Catbox.Client(Memory);
294 var cache = new Catbox.Policy(config, client, '');
295 };
296 expect(fn).to.throw(Error);
297 done();
298 });
299
300 it('errors on bad segment name', function (done) {
301
302 var config = {
303 expiresIn: 50000
304 };
305 var fn = function () {
306
307 var client = new Catbox.Client(Memory);
308 var cache = new Catbox.Policy(config, client, 'a\0b');
309 };
310 expect(fn).to.throw(Error);
311 done();
312 });
313
314 describe('#start', function () {
315
316 it('creates an empty cache object', function (done) {
317
318 var memory = new Memory();
319 expect(memory.cache).to.not.exist;
320 memory.start(function () {
321
322 expect(memory.cache).to.exist;
323 done();
324 });
325 });
326 });
327
328 describe('#stop', function () {
329
330 it('sets the cache object to null', function (done) {
331
332 var memory = new Memory();
333 expect(memory.cache).to.not.exist;
334 memory.start(function () {
335
336 expect(memory.cache).to.exist;
337 memory.stop();
338 expect(memory.cache).to.not.exist;
339 done();
340 });
341 });
342 });
343
344 describe('#get', function () {
345
346 it('errors on invalid json in cache', function (done) {
347
348 var key = {
349 segment: 'test',
350 id: 'test'
351 };
352
353 var memory = new Memory();
354 expect(memory.cache).to.not.exist;
355
356 memory.start(function () {
357
358 expect(memory.cache).to.exist;
359 memory.set(key, 'myvalue', 10, function () {
360
361 expect(memory.cache[key.segment][key.id].item).to.equal('"myvalue"');
362 memory.cache[key.segment][key.id].item = '"myvalue';
363 memory.get(key, function (err, result) {
364
365 expect(err.message).to.equal('Bad value content');
366 done();
367 });
368 });
369 });
370 });
371
372 it('returns not found on missing segment', function (done) {
373
374 var key = {
375 segment: 'test',
376 id: 'test'
377 };
378
379 var memory = new Memory();
380 expect(memory.cache).to.not.exist;
381
382 memory.start(function () {
383
384 expect(memory.cache).to.exist;
385 memory.get(key, function (err, result) {
386
387 expect(err).to.not.exist;
388 expect(result).to.not.exist;
389 done();
390 });
391 });
392 });
393 });
394
395 describe('#set', function () {
396
397 it('adds an item to the cache object', function (done) {
398
399 var key = {
400 segment: 'test',
401 id: 'test'
402 };
403
404 var memory = new Memory();
405 expect(memory.cache).to.not.exist;
406
407 memory.start(function () {
408
409 expect(memory.cache).to.exist;
410 memory.set(key, 'myvalue', 10, function () {
411
412 expect(memory.cache[key.segment][key.id].item).to.equal('"myvalue"');
413 done();
414 });
415 });
416 });
417
418 it('removes an item from the cache object when it expires', function (done) {
419
420 var key = {
421 segment: 'test',
422 id: 'test'
423 };
424
425 var memory = new Memory();
426 expect(memory.cache).to.not.exist;
427
428 memory.start(function () {
429
430 expect(memory.cache).to.exist;
431 memory.set(key, 'myvalue', 10, function () {
432
433 expect(memory.cache[key.segment][key.id].item).to.equal('"myvalue"');
434 setTimeout(function () {
435
436 expect(memory.cache[key.segment][key.id]).to.not.exist;
437 done();
438 }, 15);
439 });
440 });
441 });
442
443 it('errors when the maxByteSize has been reached', function (done) {
444
445 var key = {
446 segment: 'test',
447 id: 'test'
448 };
449
450 var memory = new Memory({ maxByteSize: 4 });
451 expect(memory.cache).to.not.exist;
452
453 memory.start(function () {
454
455 expect(memory.cache).to.exist;
456 memory.set(key, 'myvalue', 10, function (err) {
457
458 expect(err).to.exist;
459 expect(err).to.be.instanceOf(Error);
460 done();
461 });
462 });
463 });
464
465 it('increments the byte size when an item is inserted and errors when the limit is reached', function (done) {
466
467 var key1 = {
468 segment: 'test',
469 id: 'test'
470 };
471
472 var key2 = {
473 segment: 'test',
474 id: 'test2'
475 };
476
477 var memory = new Memory({ maxByteSize: 70 });
478 expect(memory.cache).to.not.exist;
479
480 memory.start(function () {
481
482 expect(memory.cache).to.exist;
483 memory.set(key1, 'my', 10, function () {
484
485 expect(memory.cache[key1.segment][key1.id].item).to.equal('"my"');
486
487 memory.set(key2, 'myvalue', 10, function (err) {
488
489 expect(err).to.exist;
490 done();
491 });
492 });
493 });
494 });
495
496 it('increments the byte size when an object is inserted', function (done) {
497
498 var key1 = {
499 segment: 'test',
500 id: 'test'
501 };
502 var itemToStore = {
503 my: {
504 array: [1, 2, 3],
505 bool: true,
506 string: 'test'
507 }
508 };
509
510 var memory = new Memory({ maxByteSize: 2000 });
511 expect(memory.cache).to.not.exist;
512
513 memory.start(function () {
514
515 expect(memory.cache).to.exist;
516 memory.set(key1, itemToStore, 10, function () {
517
518 expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(113);
519 expect(memory.cache[key1.segment][key1.id].item).to.exist;
520 done();
521 });
522 });
523 });
524
525 it('leaves the byte size unchanged when an object overrides existing key with same size', function (done) {
526
527 var key1 = {
528 segment: 'test',
529 id: 'test'
530 };
531 var itemToStore = {
532 my: {
533 array: [1, 2, 3],
534 bool: true,
535 string: 'test',
536 undefined: undefined
537 }
538 };
539
540 var memory = new Memory({ maxByteSize: 2000 });
541 expect(memory.cache).to.not.exist;
542
543 memory.start(function () {
544
545 expect(memory.cache).to.exist;
546 memory.set(key1, itemToStore, 10, function () {
547
548 expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(113);
549 expect(memory.cache[key1.segment][key1.id].item).to.exist;
550 memory.set(key1, itemToStore, 10, function () {
551
552 expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(113);
553 expect(memory.cache[key1.segment][key1.id].item).to.exist;
554 done();
555 });
556 });
557 });
558 });
559 });
560
561 describe('#drop', function () {
562
563 it('drops an existing item', function (done) {
564
565 var client = new Catbox.Client(Memory);
566 client.start(function (err) {
567
568 var key = { id: 'x', segment: 'test' };
569 client.set(key, '123', 500, function (err) {
570
571 expect(err).to.not.exist;
572 client.get(key, function (err, result) {
573
574 expect(err).to.equal(null);
575 expect(result.item).to.equal('123');
576 client.drop(key, function (err) {
577
578 expect(err).to.not.exist;
579 done();
580 });
581 });
582 });
583 });
584 });
585
586 it('drops an item from a missing segment', function (done) {
587
588 var client = new Catbox.Client(Memory);
589 client.start(function (err) {
590
591 var key = { id: 'x', segment: 'test' };
592 client.drop(key, function (err) {
593
594 expect(err).to.not.exist;
595 done();
596 });
597 });
598 });
599
600 it('drops a missing item', function (done) {
601
602 var client = new Catbox.Client(Memory);
603 client.start(function (err) {
604
605 var key = { id: 'x', segment: 'test' };
606 client.set(key, '123', 500, function (err) {
607
608 expect(err).to.not.exist;
609 client.get(key, function (err, result) {
610
611 expect(err).to.equal(null);
612 expect(result.item).to.equal('123');
613 client.drop({ id: 'y', segment: 'test' }, function (err) {
614
615 expect(err).to.not.exist;
616 done();
617 });
618 });
619 });
620 });
621 });
622
623 it('errors on drop when using invalid key', function (done) {
624
625 var client = new Catbox.Client(Memory);
626 client.start(function (err) {
627
628 client.drop({}, function (err) {
629
630 expect(err instanceof Error).to.equal(true);
631 done();
632 });
633 });
634 });
635
636 it('errors on drop when using null key', function (done) {
637
638 var client = new Catbox.Client(Memory);
639 client.start(function (err) {
640
641 client.drop(null, function (err) {
642
643 expect(err instanceof Error).to.equal(true);
644 done();
645 });
646 });
647 });
648
649 it('errors on drop when stopped', function (done) {
650
651 var client = new Catbox.Client(Memory);
652 client.stop();
653 var key = { id: 'x', segment: 'test' };
654 client.connection.drop(key, function (err) {
655
656 expect(err).to.exist;
657 done();
658 });
659 });
660
661 it('errors when cache item dropped while stopped', function (done) {
662
663 var client = new Catbox.Client(Memory);
664 client.stop();
665 client.drop('a', function (err) {
666
667 expect(err).to.exist;
668 done();
669 });
670 });
671 });
672
673 describe('#validateSegmentName', function () {
674
675 it('errors when the name is empty', function (done) {
676
677 var memory = new Memory();
678 var result = memory.validateSegmentName('');
679
680 expect(result).to.be.instanceOf(Error);
681 expect(result.message).to.equal('Empty string');
682 done();
683 });
684
685 it('errors when the name has a null character', function (done) {
686
687 var memory = new Memory();
688 var result = memory.validateSegmentName('\0test');
689
690 expect(result).to.be.instanceOf(Error);
691 done();
692 });
693
694 it('returns null when there are no errors', function (done) {
695
696 var memory = new Memory();
697 var result = memory.validateSegmentName('valid');
698
699 expect(result).to.not.be.instanceOf(Error);
700 expect(result).to.equal(null);
701 done();
702 });
703 });
704});