1 | (function() {
|
2 | var VCache, _, ks, localCache, localCacheNoClone, localCacheTTL, randomString, vs,
|
3 | indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
4 |
|
5 | _ = require("lodash");
|
6 |
|
7 | VCache = require("../");
|
8 |
|
9 | localCache = new VCache({
|
10 | stdTTL: 0
|
11 | });
|
12 |
|
13 | localCacheNoClone = new VCache({
|
14 | stdTTL: 0,
|
15 | useClones: false,
|
16 | checkperiod: 0
|
17 | });
|
18 |
|
19 | localCacheTTL = new VCache({
|
20 | stdTTL: 0.3,
|
21 | checkperiod: 0
|
22 | });
|
23 |
|
24 | localCache._killCheckPeriod();
|
25 |
|
26 | randomString = function(length, withnumbers) {
|
27 | var chars, i, randomstring, rnum, string_length;
|
28 | if (withnumbers == null) {
|
29 | withnumbers = true;
|
30 | }
|
31 | chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
32 | if (withnumbers) {
|
33 | chars += "0123456789";
|
34 | }
|
35 | string_length = length || 5;
|
36 | randomstring = "";
|
37 | i = 0;
|
38 | while (i < string_length) {
|
39 | rnum = Math.floor(Math.random() * chars.length);
|
40 | randomstring += chars.substring(rnum, rnum + 1);
|
41 | i++;
|
42 | }
|
43 | return randomstring;
|
44 | };
|
45 |
|
46 | vs = [];
|
47 |
|
48 | ks = [];
|
49 |
|
50 | module.exports = {
|
51 | "general": function(beforeExit, assert) {
|
52 | var _err, key, n, p, q, start, value, value2;
|
53 | console.log("\nSTART GENERAL TEST: " + VCache.version);
|
54 | n = 0;
|
55 | start = _.clone(localCache.getStats());
|
56 | value = randomString(100);
|
57 | value2 = randomString(100);
|
58 | key = randomString(10);
|
59 | localCache.once("del", function(_key, _val) {
|
60 | assert.equal(_key, key);
|
61 | assert.equal(_val, value2);
|
62 | });
|
63 | localCache.set(key, value, 0, function(err, res) {
|
64 | assert.isNull(err, err);
|
65 | n++;
|
66 | assert.equal(1, localCache.getStats().keys - start.keys);
|
67 | localCache.get(key, function(err, res) {
|
68 | n++;
|
69 | assert.eql(value, res);
|
70 | });
|
71 | localCache.keys(function(err, res) {
|
72 | var pred;
|
73 | n++;
|
74 | pred = [key];
|
75 | assert.eql(pred, res);
|
76 | });
|
77 | localCache.get("xxx", function(err, res) {
|
78 | n++;
|
79 | assert.isNull(err, err);
|
80 | assert.isUndefined(res, res);
|
81 | });
|
82 | localCache.del("xxx", function(err, res) {
|
83 | n++;
|
84 | assert.isNull(err, err);
|
85 | assert.equal(0, res);
|
86 | });
|
87 | localCache.set(key, value2, 0, function(err, res) {
|
88 | n++;
|
89 | assert.isNull(err, err);
|
90 | assert.ok(res, err);
|
91 | localCache.get(key, function(err, res) {
|
92 | var pred;
|
93 | n++;
|
94 | pred = value2;
|
95 | assert.eql(pred, res);
|
96 | assert.equal(1, localCache.getStats().keys - start.keys);
|
97 | });
|
98 | });
|
99 | localCache.del(key, function(err, res) {
|
100 | localCache.removeAllListeners("del");
|
101 | n++;
|
102 | assert.isNull(err, err);
|
103 | assert.equal(1, res);
|
104 | assert.equal(0, localCache.getStats().keys - start.keys);
|
105 | localCache.get(key, function(err, res) {
|
106 | n++;
|
107 | assert.isNull(err, err);
|
108 | assert.isUndefined(res, res);
|
109 | });
|
110 | localCache.set("zero", 0, 0, function(err, res) {
|
111 | n++;
|
112 | assert.isNull(err, err);
|
113 | assert.ok(res, err);
|
114 | });
|
115 | localCache.get("zero", function(err, res) {
|
116 | n++;
|
117 | assert.isNull(err, err);
|
118 | assert.eql(0, res);
|
119 | });
|
120 | });
|
121 | });
|
122 | if (typeof Promise !== "undefined" && Promise !== null) {
|
123 | p = new Promise(function(fulfill, reject) {
|
124 | return fulfill('Some deferred value');
|
125 | });
|
126 | p.then(function(value) {
|
127 | assert.eql(value, 'Some deferred value');
|
128 | });
|
129 | localCacheNoClone.set("promise", p);
|
130 | q = localCacheNoClone.get("promise");
|
131 | try {
|
132 | q.then(function(value) {
|
133 | n++;
|
134 | });
|
135 | } catch (_error) {
|
136 | _err = _error;
|
137 | assert.ok(false, _err);
|
138 | return;
|
139 | }
|
140 | } else {
|
141 | console.log("No Promise test, because not availible in this node version");
|
142 | }
|
143 | beforeExit(function() {
|
144 | var _count;
|
145 | _count = 11;
|
146 | if (typeof Promise !== "undefined" && Promise !== null) {
|
147 | _count += 1;
|
148 | }
|
149 | assert.equal(_count, n, "not exited");
|
150 | });
|
151 | },
|
152 | "general sync": function(beforeExit, assert) {
|
153 | var key, pred, res, res2, start, tObj, value, value2;
|
154 | console.log("\nSTART GENERAL TEST SYNC");
|
155 | localCache.flushAll();
|
156 | start = _.clone(localCache.getStats());
|
157 | value = randomString(100);
|
158 | value2 = randomString(100);
|
159 | key = randomString(10);
|
160 | localCache.once("del", function(_key, _val) {
|
161 | assert.equal(_key, key);
|
162 | assert.equal(_val, value2);
|
163 | });
|
164 | assert.ok(localCache.set(key, value, 0));
|
165 | assert.equal(1, localCache.getStats().keys - start.keys);
|
166 | res = localCache.get(key);
|
167 | assert.eql(value, res);
|
168 | res = localCache.keys();
|
169 | pred = [key];
|
170 | assert.eql(pred, res);
|
171 | res = localCache.get("xxx");
|
172 | assert.isUndefined(res, res);
|
173 | res = localCache.del("xxx");
|
174 | assert.equal(0, res);
|
175 | res = localCache.set(key, value2, 0);
|
176 | assert.ok(res, res);
|
177 | res = localCache.get(key);
|
178 | assert.eql(value2, res);
|
179 | assert.equal(1, localCache.getStats().keys - start.keys);
|
180 | res = localCache.del(key);
|
181 | localCache.removeAllListeners("del");
|
182 | assert.equal(1, res);
|
183 | assert.equal(0, localCache.getStats().keys - start.keys);
|
184 | res = localCache.get(key);
|
185 | assert.isUndefined(res, res);
|
186 | res = localCache.set("mulitA", 23);
|
187 | assert.ok(res, res);
|
188 | res = localCache.set("mulitB", 23);
|
189 | assert.ok(res, res);
|
190 | res = localCache.set("mulitC", 23);
|
191 | assert.ok(res, res);
|
192 | res = localCache.get("mulitA");
|
193 | assert.equal(res, 23);
|
194 | res = localCache.get("mulitB");
|
195 | assert.equal(res, 23);
|
196 | res = localCache.get("mulitC");
|
197 | assert.equal(res, 23);
|
198 | res = localCache.del(["mulitA", "mulitB"]);
|
199 | assert.equal(2, res);
|
200 | res = localCache.get("mulitA");
|
201 | assert.isUndefined(res, res);
|
202 | res = localCache.get("mulitB");
|
203 | assert.isUndefined(res, res);
|
204 | res = localCache.get("mulitC");
|
205 | assert.equal(res, 23);
|
206 | res = localCache.del(["mulitC"]);
|
207 | assert.equal(1, res);
|
208 | res = localCache.get("mulitC");
|
209 | assert.isUndefined(res, res);
|
210 | res = localCache.del(["mulitA", "mulitB", "mulitC"]);
|
211 | assert.equal(0, res);
|
212 | res = localCache.set("zero", 0, 0);
|
213 | assert.ok(res, res);
|
214 | res = localCache.get("zero");
|
215 | assert.eql(0, res);
|
216 | tObj = {
|
217 | a: 1,
|
218 | b: {
|
219 | x: 2,
|
220 | y: 3
|
221 | }
|
222 | };
|
223 | res = localCache.set("clone", tObj, 0);
|
224 | assert.ok(res, res);
|
225 | tObj.b.x = 666;
|
226 | res = localCache.get("clone");
|
227 | assert.equal(2, res.b.x);
|
228 | res.b.y = 42;
|
229 | res2 = localCache.get("clone");
|
230 | assert.equal(3, res2.b.y);
|
231 | },
|
232 | "flush": function(beforeExit, assert) {
|
233 | var count, i, j, k, key, len, n, ref, startKeys, val;
|
234 | console.log("\nSTART FLUSH TEST");
|
235 | n = 0;
|
236 | count = 100;
|
237 | startKeys = localCache.getStats().keys;
|
238 | ks = [];
|
239 | val = randomString(20);
|
240 | for (i = j = 1, ref = count; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
|
241 | key = randomString(7);
|
242 | ks.push(key);
|
243 | }
|
244 | for (k = 0, len = ks.length; k < len; k++) {
|
245 | key = ks[k];
|
246 | localCache.set(key, val, 0, function(err, res) {
|
247 | n++;
|
248 | assert.isNull(err, err);
|
249 | });
|
250 | }
|
251 | assert.equal(localCache.getStats().keys, startKeys + count);
|
252 | localCache.flushAll(false);
|
253 | assert.equal(localCache.getStats().keys, 0);
|
254 | assert.eql(localCache.data, {});
|
255 | beforeExit(function() {
|
256 | assert.equal(n, count + 0);
|
257 | });
|
258 | },
|
259 | "many": function(beforeExit, assert) {
|
260 | var _dur, count, i, j, k, key, l, len, len1, n, ref, time, val;
|
261 | n = 0;
|
262 | count = 100000;
|
263 | console.log("\nSTART MANY TEST/BENCHMARK.\nSet, Get and check " + count + " elements");
|
264 | val = randomString(20);
|
265 | ks = [];
|
266 | for (i = j = 1, ref = count; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
|
267 | key = randomString(7);
|
268 | ks.push(key);
|
269 | }
|
270 | time = new Date().getTime();
|
271 | for (k = 0, len = ks.length; k < len; k++) {
|
272 | key = ks[k];
|
273 | assert.ok(localCache.set(key, val, 0));
|
274 | }
|
275 | _dur = new Date().getTime() - time;
|
276 | console.log("BENCHMARK for SET:", _dur + "ms", " ( " + (_dur / count) + "ms per item ) ");
|
277 | time = new Date().getTime();
|
278 | for (l = 0, len1 = ks.length; l < len1; l++) {
|
279 | key = ks[l];
|
280 | n++;
|
281 | assert.eql(val, localCache.get(key));
|
282 | }
|
283 | _dur = new Date().getTime() - time;
|
284 | console.log("BENCHMARK for GET:", _dur + "ms", " ( " + (_dur / count) + "ms per item ) ");
|
285 | console.log("BENCHMARK STATS:", localCache.getStats());
|
286 | beforeExit(function() {
|
287 | var _keys, _stats;
|
288 | _stats = localCache.getStats();
|
289 | _keys = localCache.keys();
|
290 | assert.eql(_stats.keys, _keys.length);
|
291 | console.log(_stats);
|
292 | assert.eql(count - 10000 + 100, _keys.length);
|
293 | assert.equal(n, count);
|
294 | });
|
295 | },
|
296 | "delete": function(beforeExit, assert) {
|
297 | var count, i, j, k, n, ref, ref1, ri, startKeys;
|
298 | console.log("\nSTART DELETE TEST");
|
299 | n = 0;
|
300 | count = 10000;
|
301 | startKeys = localCache.getStats().keys;
|
302 | for (i = j = 1, ref = count; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
|
303 | ri = Math.floor(Math.random() * vs.length);
|
304 | localCache.del(ks[i], function(err, count) {
|
305 | n++;
|
306 | assert.isNull(err, err);
|
307 | return assert.equal(1, count);
|
308 | });
|
309 | }
|
310 | for (i = k = 1, ref1 = count; 1 <= ref1 ? k <= ref1 : k >= ref1; i = 1 <= ref1 ? ++k : --k) {
|
311 | ri = Math.floor(Math.random() * vs.length);
|
312 | localCache.del(ks[i], function(err, count) {
|
313 | n++;
|
314 | assert.equal(0, count);
|
315 | return assert.isNull(err, err);
|
316 | });
|
317 | }
|
318 | assert.equal(localCache.getStats().keys, startKeys - count);
|
319 | beforeExit(function() {
|
320 | assert.equal(n, count * 2);
|
321 | });
|
322 | },
|
323 | "stats": function(beforeExit, assert) {
|
324 | var count, end, i, j, k, key, keys, l, n, ref, ref1, ref2, start, val, vals;
|
325 | console.log("\nSTART STATS TEST");
|
326 | n = 0;
|
327 | start = _.clone(localCache.getStats());
|
328 | count = 5;
|
329 | keys = [];
|
330 | vals = [];
|
331 | for (i = j = 1, ref = count * 2; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
|
332 | key = randomString(7);
|
333 | val = randomString(50);
|
334 | keys.push(key);
|
335 | vals.push(val);
|
336 | localCache.set(key, val, 0, function(err, success) {
|
337 | n++;
|
338 | assert.isNull(err, err);
|
339 | assert.ok(success);
|
340 | });
|
341 | }
|
342 | for (i = k = 1, ref1 = count; 1 <= ref1 ? k <= ref1 : k >= ref1; i = 1 <= ref1 ? ++k : --k) {
|
343 | localCache.get(keys[i], function(err, res) {
|
344 | n++;
|
345 | assert.eql(vals[i], res);
|
346 | assert.isNull(err, err);
|
347 | });
|
348 | localCache.del(keys[i], function(err, success) {
|
349 | n++;
|
350 | assert.isNull(err, err);
|
351 | assert.ok(success);
|
352 | });
|
353 | }
|
354 | for (i = l = 1, ref2 = count; 1 <= ref2 ? l <= ref2 : l >= ref2; i = 1 <= ref2 ? ++l : --l) {
|
355 | localCache.get("xxxx", function(err, res) {
|
356 | ++n;
|
357 | assert.isNull(err, err);
|
358 | assert.isUndefined(res, res);
|
359 | });
|
360 | }
|
361 | end = localCache.getStats();
|
362 | assert.equal(end.hits - start.hits, 5, "hits wrong");
|
363 | assert.equal(end.misses - start.misses, 5, "misses wrong");
|
364 | assert.equal(end.keys - start.keys, 5, "hits wrong");
|
365 | assert.equal(end.ksize - start.ksize, 5 * 7, "hits wrong");
|
366 | assert.equal(end.vsize - start.vsize, 5 * 50, "hits wrong");
|
367 | beforeExit(function() {
|
368 | assert.equal(n, count * 5);
|
369 | });
|
370 | },
|
371 | "multi": function(beforeExit, assert) {
|
372 | var count, getKeys, i, j, k, key, l, len, len1, n, pred, ref, startKeys, val;
|
373 | console.log("\nSTART MULTI TEST");
|
374 | n = 0;
|
375 | count = 100;
|
376 | startKeys = localCache.getStats().keys;
|
377 | ks = [];
|
378 | val = randomString(20);
|
379 | for (i = j = 1, ref = count; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
|
380 | key = randomString(7);
|
381 | ks.push(key);
|
382 | }
|
383 | for (k = 0, len = ks.length; k < len; k++) {
|
384 | key = ks[k];
|
385 | localCache.set(key, val, 0, function(err, res) {
|
386 | n++;
|
387 | assert.isNull(err, err);
|
388 | });
|
389 | }
|
390 | getKeys = ks.splice(50, 5);
|
391 | pred = {};
|
392 | for (l = 0, len1 = getKeys.length; l < len1; l++) {
|
393 | key = getKeys[l];
|
394 | pred[key] = val;
|
395 | }
|
396 | localCache.mget(getKeys[0], function(err, res) {
|
397 | n++;
|
398 | assert.isNotNull(err, err);
|
399 | assert.eql(err.constructor.name, "Error");
|
400 | assert.eql("EKEYSTYPE", err.name);
|
401 | assert.isUndefined(res, res);
|
402 | });
|
403 | localCache.mget(getKeys, function(err, res) {
|
404 | n++;
|
405 | assert.isNull(err, err);
|
406 | assert.eql(pred, res);
|
407 | });
|
408 | localCache.del(getKeys, function(err, res) {
|
409 | n++;
|
410 | assert.isNull(err, err);
|
411 | assert.equal(getKeys.length, res);
|
412 | });
|
413 | localCache.mget(getKeys, function(err, res) {
|
414 | n++;
|
415 | assert.isNull(err, err);
|
416 | assert.eql({}, res);
|
417 | });
|
418 | beforeExit(function() {
|
419 | assert.equal(n, count + 4);
|
420 | });
|
421 | },
|
422 | "ttl": function(beforeExit, assert) {
|
423 | var _keys, key, key2, key3, key4, key5, n, val;
|
424 | console.log("\nSTART TTL TEST");
|
425 | val = randomString(20);
|
426 | key = "k1_" + randomString(7);
|
427 | key2 = "k2_" + randomString(7);
|
428 | key3 = "k3_" + randomString(7);
|
429 | key4 = "k4_" + randomString(7);
|
430 | key5 = "k5_" + randomString(7);
|
431 | _keys = [key, key2, key3, key4, key5];
|
432 | n = 0;
|
433 | localCache.set(key, val, 0.5, function(err, res) {
|
434 | assert.isNull(err, err);
|
435 | assert.ok(res);
|
436 | localCache.get(key, function(err, res) {
|
437 | assert.isNull(err, err);
|
438 | assert.eql(val, res);
|
439 | });
|
440 | });
|
441 | localCache.set(key2, val, 0.3, function(err, res) {
|
442 | assert.isNull(err, err);
|
443 | assert.ok(res);
|
444 | localCache.get(key2, function(err, res) {
|
445 | assert.isNull(err, err);
|
446 | assert.eql(val, res);
|
447 | });
|
448 | });
|
449 | setTimeout(function() {
|
450 | ++n;
|
451 | localCache.get(key, function(err, res) {
|
452 | assert.isNull(err, err);
|
453 | assert.eql(val, res);
|
454 | });
|
455 | }, 400);
|
456 | setTimeout(function() {
|
457 | ++n;
|
458 | localCache.get(key, function(err, res) {
|
459 | assert.isNull(err, err);
|
460 | assert.isUndefined(res, res);
|
461 | });
|
462 | }, 600);
|
463 | setTimeout(function() {
|
464 | ++n;
|
465 | localCache.get(key2, function(err, res) {
|
466 | assert.isNull(err, err);
|
467 | assert.eql(val, res);
|
468 | });
|
469 | }, 250);
|
470 | setTimeout(function() {
|
471 | process.nextTick(function() {
|
472 | var _testExpired, _testSet, startKeys;
|
473 | startKeys = localCache.getStats().keys;
|
474 | key = "autotest";
|
475 | _testExpired = function(_key, _val) {
|
476 | if (indexOf.call(_keys, _key) < 0) {
|
477 | assert.equal(_key, key);
|
478 | assert.equal(_val, val);
|
479 | }
|
480 | };
|
481 | _testSet = function(_key) {
|
482 | assert.equal(_key, key);
|
483 | };
|
484 | localCache.once("set", _testSet);
|
485 | localCache.set(key, val, 0.5, function(err, res) {
|
486 | assert.isNull(err, err);
|
487 | assert.ok(res);
|
488 | assert.equal(startKeys + 1, localCache.getStats().keys);
|
489 | localCache.get(key, function(err, res) {
|
490 | assert.eql(val, res);
|
491 | localCache.on("expired", _testExpired);
|
492 | return setTimeout(function() {
|
493 | localCache._checkData(false);
|
494 | assert.isUndefined(localCache.data[key]);
|
495 | localCache.removeAllListeners("set");
|
496 | localCache.removeAllListeners("expired");
|
497 | }, 700);
|
498 | });
|
499 | });
|
500 | });
|
501 | }, 1000);
|
502 | localCache.set(key3, val, 100, function(err, res) {
|
503 | assert.isNull(err, err);
|
504 | assert.ok(res);
|
505 | localCache.get(key3, function(err, res) {
|
506 | assert.isNull(err, err);
|
507 | assert.eql(val, res);
|
508 | localCache.ttl(key3 + "false", 0.3, function(err, setted) {
|
509 | assert.isNull(err, err);
|
510 | assert.equal(false, setted);
|
511 | });
|
512 | localCache.ttl(key3, 0.3, function(err, setted) {
|
513 | assert.isNull(err, err);
|
514 | assert.ok(setted);
|
515 | });
|
516 | localCache.get(key3, function(err, res) {
|
517 | assert.eql(val, res);
|
518 | });
|
519 | setTimeout(function() {
|
520 | res = localCache.get(key3);
|
521 | assert.isUndefined(res, res);
|
522 | assert.isUndefined(localCache.data[key3]);
|
523 | }, 500);
|
524 | });
|
525 | });
|
526 | localCache.set(key4, val, 100, function(err, res) {
|
527 | assert.isNull(err, err);
|
528 | assert.ok(res);
|
529 | localCache.get(key4, function(err, res) {
|
530 | assert.isNull(err, err);
|
531 | assert.eql(val, res);
|
532 | localCache.ttl(key4 + "false", function(err, setted) {
|
533 | assert.isNull(err, err);
|
534 | return assert.equal(false, setted);
|
535 | });
|
536 | localCache.ttl(key4, function(err, setted) {
|
537 | assert.isNull(err, err);
|
538 | assert.ok(setted);
|
539 | assert.isUndefined(localCache.data[key4]);
|
540 | });
|
541 | });
|
542 | });
|
543 | localCacheTTL.set(key5, val, 100, function(err, res) {
|
544 | assert.isNull(err, err);
|
545 | assert.ok(res);
|
546 | localCacheTTL.get(key5, function(err, res) {
|
547 | assert.isNull(err, err);
|
548 | assert.eql(val, res);
|
549 | localCacheTTL.ttl(key5 + "false", function(err, setted) {
|
550 | assert.isNull(err, err);
|
551 | assert.equal(false, setted);
|
552 | });
|
553 | localCacheTTL.ttl(key5, function(err, setted) {
|
554 | assert.isNull(err, err);
|
555 | assert.ok(setted);
|
556 | });
|
557 | localCacheTTL.get(key5, function(err, res) {
|
558 | assert.eql(val, res);
|
559 | });
|
560 | setTimeout(function() {
|
561 | res = localCache.get(key5);
|
562 | assert.isUndefined(res, res);
|
563 | localCacheTTL._checkData(false);
|
564 | assert.isUndefined(localCacheTTL.data[key5]);
|
565 | }, 500);
|
566 | });
|
567 | });
|
568 | }
|
569 | };
|
570 |
|
571 | }).call(this);
|