-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCacheTest.js
More file actions
430 lines (331 loc) · 16.5 KB
/
CacheTest.js
File metadata and controls
430 lines (331 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
const {describe, it, beforeEach, afterEach} = (exports.lab = require('lab').script());
const expect = require('code').expect;
const Sinon = require('sinon');
const Parser = require('../../src/Parser');
const Cache = require('../../src/Cache');
const header1 = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)';
const header2 =
'User-Agent: Mozilla/5.0 (Linux; Android 4.0.3; GT-P3113 Build/IML74K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Safari/535.19';
const comparision1 = {
browser: {name: 'Internet Explorer', version: '6.0', type: 'browser'},
engine: {name: 'Trident'},
os: {name: 'Windows', version: {value: '5.0', alias: '2000'}},
device: {type: 'desktop'},
};
const comparision2 = {
browser: {name: 'Chrome', version: '18', type: 'browser'},
engine: {name: 'Webkit', version: '535.19'},
os: {name: 'Android', version: '4.0.3'},
device: {type: 'tablet', manufacturer: 'Samsung', model: 'Galaxy Tab 2 7.0'},
};
let clock;
describe('Cache Class', () => {
beforeEach((done) => {
Cache.resetClassState();
clock = Sinon.useFakeTimers('setInterval', 'clearInterval', 'Date');
done();
});
afterEach((done) => {
clock.restore();
done();
});
describe('Two subsequent parse with same UA', () => {
it('should be cached', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
done();
});
});
describe('Two subsequent parse with different UA', () => {
it('should not be cached', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
result = new Parser(header2, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision2);
expect(result.cached).to.not.exist();
done();
});
});
describe('Create cache with negative expiry time', () => {
it('should let the Cache never expire', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: -1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// let pass 2 years
clock.tick(60 * 60 * 24 * 265 * 2 * 1000);
// it's still in cache
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
done();
});
});
describe('Create cache with an expiry time that would make the cache check interval be 0s (cacheExpires/5), ', () => {
it('should instead be 1s', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 4});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// let pass 3 seconds
clock.tick(3 * 1000);
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 4});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
// let pass 4 seconds + cache check inteval
clock.tick((4 + 1 + 1) * 1000);
// No more in cache
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 4});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
describe('Default cache with default expiry time and expiry check timer', () => {
it('should be empty after expiry time 900s + 1/5 * 900s + 1s', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
clock.tick((900 + 900 / 5 + 1) * 1000);
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
describe('Default cache with default expiry time but with 1s expiry check timer', () => {
it('should be empty after expiry time 900s + 1s + 1s', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
clock.tick((900 + 1 + 1) * 1000);
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
describe('Default cache with 100s expiry time and default expiry check timer', () => {
it('should be empty after expiry time 100s + 1/5 * 100s + 1s', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
clock.tick((100 + 100 / 5 + 1) * 1000);
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
describe('Default cache with 100s expiry time but with 2s expiry check timer', () => {
it('should be empty after expiry time 100s + 2s + 1s', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100, cacheCheckInterval: 2});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100, cacheCheckInterval: 2});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
clock.tick((100 + 2 + 1) * 1000);
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100, cacheCheckInterval: 2});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
describe('Default cache with default expiry time but with 1s expiry check timer, one item with cache refreshed and one not', () => {
it('the one refreshed should be in cache after the first expiry time (900 + 1 + 1), the other not', (done) => {
// First call to Parser, cache both
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
let result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.not.exist();
// Second call, check that are cached
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.be.true();
// Make the timer be 1 second before the cache expiry time
clock.tick(899 * 1000);
// Refresh only the record related to header 1
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
// Make the cache expiry and wait that the check interval has passed
clock.tick(3 * 1000);
// Call the parser with header1, it should be still in cache
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
// Call the parser with header2, it should NOT be in cache
result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.not.exist();
done();
});
});
describe('Change the expiry time while cache is populated', () => {
it('should affect only new items, and updated the expiry time after a new cache hit', (done) => {
// First call to Parser, still not cached, expiry time default to 900s
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// let pass 200s
clock.tick(200 * 1000);
// Put in cache a new item with 100s expiry time
let result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100});
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.not.exist();
// let expire the second item
clock.tick((100 + 100 / 5 + 1) * 1000);
result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100});
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.not.exist();
// First item is still in cache, parse it and update the expiry time
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
// Let expiry the first item.
clock.tick((100 + 100 / 5 + 1) * 1000);
// Item has expired also if the first 900s are not passed
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
describe('Parse UA with no expiry time', () => {
it('should never expire', (done) => {
// First call to Parser, still not cached, expiry time default to 900s
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 0});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// let pass 2 years
clock.tick(60 * 60 * 24 * 265 * 2 * 1000);
// it's still in cache
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
done();
});
});
describe('Disable cache expiry when cache has already some item inside,', () => {
it('also old item with expiry time should never expire', (done) => {
// First call to Parser, still not cached, expiry time default to 900s
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// let pass half expiration time
clock.tick(450 * 1000);
let result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE, cacheExpires: 0});
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.not.exist();
// let pass 2 years
clock.tick(60 * 60 * 24 * 265 * 2 * 1000);
// it's still in cache
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE});
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.be.true();
done();
});
});
describe('Enable cache expiry when cache has already some item inside with no expiry time,', () => {
it('Old items are still there when the new expiry time come', (done) => {
// First call to Parser, still not cached, expiry time default to 900s
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 0});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// let pass 2 years
clock.tick(60 * 60 * 24 * 265 * 2 * 1000);
// it's still in cache
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 0});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
let result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE});
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.not.exist();
// expiry the second record
clock.tick((900 + 900 / 5 + 1) * 1000);
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE});
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.not.exists();
done();
});
});
describe('Default cache with 100s expiry time but with 2s expiry check timer', () => {
it('should be empty after expiry time 100s + 2s + 1s', (done) => {
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100, cacheCheckInterval: 2});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100, cacheCheckInterval: 2});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
clock.tick((100 + 2 + 1) * 1000);
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE, cacheExpires: 100, cacheCheckInterval: 2});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
describe('Default cache with default expiry time and default check interval, after a second parse that change the check internval', () => {
it('should correctly update the cache check loop', (done) => {
// First call to Parser, cache both
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// Make the timer be 1 second before the cache expiry time
clock.tick(899 * 1000);
let result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE, cacheCheckInterval: 1});
// Let the cache expire for header 1 and let the cache check interval pass
clock.tick((1 + 1 + 1) * 1000);
// header 1 record should be gone, header 2 one shoul be there in cache
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
result2 = new Parser(header2, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
expect(result2.toObject()).to.be.equal(comparision2);
expect(result2.cached).to.be.true();
done();
});
});
describe('Default cache with default expiry time and default check interval: second parse is done without cache', () => {
it('the result returned should not come from cache', (done) => {
// First call to Parser, cache both
let result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
// let pass some time
clock.tick(100 * 1000);
// cache is populated with header1 record
result = new Parser(header1, {cache: Parser.SIMPLE_CACHE});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.be.true();
// parse the same header without cache property in options
result = new Parser(header1, {cacheCheckInterval: 1});
expect(result.toObject()).to.be.equal(comparision1);
expect(result.cached).to.not.exist();
done();
});
});
});