-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodecache.config.test.js
430 lines (373 loc) · 14.7 KB
/
nodecache.config.test.js
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 NodeCache = require("../index")
describe("NodeCache params for instance config", () => {
describe("NodeCache params - all invalid", () => {
let cache
afterEach(() => {
cache.close()
})
test("When all config values are null", () => {
cache = new NodeCache({
forceString: null,
stdTTL: null,
maxKeys: null,
valueOnly: null
})
cache.set(1, 12345)
//since forceString will be enabled by default and valueOnly will be true by default.
expect(cache.get(1)).toStrictEqual(expect.any(String))
expect(cache.get(1)).toStrictEqual("12345")
})
test("When all config values are null with valueOnly disabled", () => {
cache = new NodeCache({
forceString: null,
stdTTL: null,
maxKeys: null,
valueOnly: false
})
cache.set(1, 12345)
expect(cache.get(1)).toStrictEqual({
value: expect.any(String), //since forceString will be enabled by default
ttl: expect.any(Number)
})
expect(cache.get(1)?.ttl).toBeLessThanOrEqual(Date.now())
expect(cache.get(1)?.value).toStrictEqual("12345")
})
test("When all config values are negative", () => {
cache = new NodeCache({
forceString: -1,
stdTTL: -1,
maxKeys: -1,
valueOnly: false
})
cache.set(2, 678910)
expect(cache.get(2)).toStrictEqual({
value: expect.any(String), //since forceString will be enabled by default
ttl: expect.any(Number)
})
expect(cache.get(2)?.ttl).toBeLessThanOrEqual(Date.now())
expect(cache.get(2)?.value).toStrictEqual("678910")
expect(cache.setM([
{ key: "kx", value: 1 },
{ key: "ky", value: 2, ttl: 60 * 60 * 1000 },
{ key: 3, value: "3" }])).toStrictEqual([true, true, true])
})
})
describe("NodeCache stdTTL configurations", () => {
let cache
afterEach(() => {
cache.close()
})
test("When no stdTTL value is configured", () => {
try {
cache = new NodeCache({ valueOnly: false })
cache.set("no-std-ttl", "test-value")
expect(cache.get("no-std-ttl")).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
} catch (error) {
console.warn(error.message)
}
})
test("When valid stdTTL of 100ms is configured for all the NodeCache::set() calls", () => {
try {
cache = new NodeCache({
stdTTL: 100,
valueOnly: false
})
cache.set("std-100", "test-value")
expect(cache.get("std-100")).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
setTimeout(() => {
cache.get("std-100")
expect(cache.get("std-100")).toBeUndefined()
}, 150)
expect(cache.get("std-100")?.ttl).toStrictEqual(expect.any(Number))
} catch (error) {
console.warn(error.message)
}
})
test("When valid, very large stdTTL value configured", () => {
cache = new NodeCache({
stdTTL: 30 * 24 * 60 * 60 * 1000, // 30 days
valueOnly: false
})
cache.set("std-large", "test-value-largeStd")
expect(cache.get("std-large")).toStrictEqual({
value: expect.anything(),
ttl: expect.any(Number)
})
expect(cache.get("std-large").ttl).toStrictEqual(expect.any(Number))
expect(cache.get("std-large").value).toStrictEqual("test-value-largeStd")
})
test("When boolean stdTTL value is configured", () => {
cache = new NodeCache({
stdTTL: true,
valueOnly: false
})
// expect the stdTTL to be 0 => Infinite.
cache.set("std-boolean", "boolean-ttl-check")
expect(cache.get("std-boolean")).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
expect(cache.get("std-boolean")?.ttl).toBeLessThanOrEqual(Date.now())
})
test("When string stdTTL value is configured", () => {
cache = new NodeCache({
stdTTL: "15000", // 15seconds
valueOnly: false
})
// expect the stdTTL to be 0 => Infinite.
cache.set("std-boolean", "string-ttl-check")
expect(cache.get("std-boolean")).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
expect(cache.get("std-boolean")?.ttl).toBeLessThanOrEqual(Date.now())
})
test("When falsy (NaN) stdTTL value is configured", () => {
cache = new NodeCache({
valueOnly: false,
stdTTL: NaN // falsy values: 0, false, null, undefined, NaN, ''
})
// expect the stdTTL to be 0 => Infinite.
cache.set("std-falsy", "falsy-check")
expect(cache.get("std-falsy")).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
expect(cache.get("std-falsy")?.ttl).toBeLessThanOrEqual(Date.now())
})
test("When falsy (null) stdTTL value is configured", () => {
cache = new NodeCache({
forceString: false,
valueOnly: false,
stdTTL: null
})
// expect the stdTTL to be 0 => Infinite.
cache.set("std-falsy-2", "falsy-check-2")
expect(cache.get("std-falsy-2")).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
expect(cache.get("std-falsy-2")?.ttl).toBeLessThanOrEqual(Date.now())
expect(cache.get("std-falsy-2")?.value).toStrictEqual("falsy-check-2")
})
test("When falsy (undefined) stdTTL value is configured", () => {
cache = new NodeCache({
stdTTL: undefined,
valueOnly: false
})
// expect the stdTTL to be 0 => Infinite.
cache.set("std-falsy-2", "falsy-check-2")
expect(cache.get("std-falsy-2")).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
expect(cache.get("std-falsy-2")?.ttl).toBeLessThanOrEqual(Date.now())
expect(cache.get("std-falsy-2")?.value).toStrictEqual("falsy-check-2")
})
})
describe("NodeCache forceString configurations", () => {
let cache
afterEach(() => {
cache.close()
})
test("When forceString is not configured, defaults to true", () => {
cache = new NodeCache()
cache.set("key1", {
id: 6231,
data: "data for key1",
createAt: Date.now()
})
const value = cache.get("key1")
expect(value).not.toBeUndefined()
expect(value).toEqual(expect.any(String)) // not an object anymore
})
test("forceString is set to false to prevent automatic type coercion", () => {
cache = new NodeCache({
forceString: false
})
cache.set("key2", {
id: 7158,
data: "data for key2",
createAt: Date.now()
})
const value = cache.get("key2")
expect(value).not.toBeUndefined()
expect(value).toEqual(expect.any(Object))
})
})
describe("NodeCache maxKeys configurations", () => {
let cache
afterEach(() => {
cache.close()
})
test("When maxKeys is not configured, defaults to -1 (No Limit)", () => {
cache = new NodeCache()
let flag = true
for (let i = 1; i <= 6; i++) {
let success = cache.set(i, `value-${i}`)
flag = flag & success
}
expect(flag).toBeTruthy()
expect(cache.get(6)).not.toBeUndefined()
cache.close()
})
test("When maxKeys set to 5, limit imposed on cache", () => {
cache = new NodeCache({
maxKeys: 5
})
expect(() => {
for (let i = 1; i <= 6; i++) {
cache.set(i, `value-${i}`)
}
}).toThrow(Error)
expect(cache).toBeDefined()
const loggerConfigurations = cache.getLogConfig()
const configurations = cache.getCacheConfig()
expect(configurations).toBeDefined()
expect(configurations).toStrictEqual(expect.any(Object))
expect(loggerConfigurations).toBeDefined()
expect(loggerConfigurations).toStrictEqual(expect.any(Object))
expect(cache.get(6)).toBeUndefined()
cache.close()
})
})
describe("NodeCache valueOnly configurations: Default (true) case", () => {
let cache
beforeEach(() => {
cache = new NodeCache({
forceString: false
// valueOnly: true
// By default valueOnly is set to true -> backward compatibility with older npm versions.
})
})
afterEach(() => {
cache.close()
})
test("When not valueOnly flag with the instance", () => {
expect(cache.getCacheConfig().valueOnly).toEqual(true)
})
test("When using Get() and Set() calls - true case", () => {
cache.set(151123, { _id: "nC11Hque81", value: "test-data" })
expect(cache.get(151123)).toStrictEqual({ _id: "nC11Hque81", value: "test-data" })
cache.set(1511232, { _id: "nC11Hque82", value: "test-data-2" }, 60 * 1000)
expect(cache.get(1511232)).toStrictEqual({ _id: "nC11Hque82", value: "test-data-2" })
})
test("When using getM() and setM() calls - true case", () => {
const input = [{ key: 1511235, value: "test-data-5" }, { key: 1511236, value: "test-data-6", ttl: 2 * 60 * 1000 }]
cache.setM(input)
const responses = cache.getM([1511235, 1511236])
responses.forEach(response => {
expect(response).toStrictEqual(expect.any(String))
})
})
})
describe("NodeCache valueOnly configurations: New (false) case", () => {
let cache
beforeEach(() => {
cache = new NodeCache({
//forceString: true by default
forceString: false,
valueOnly: false
})
})
afterEach(() => {
cache.close()
})
test("When not valueOnly flag with the instance", () => {
expect(cache.getCacheConfig().valueOnly).toEqual(false)
})
test("When using get() and set() calls - false case", () => {
cache.set(1511233, { _id: "nC11Hque83", value: "test-data-3" })
expect(cache.get(1511233)).toStrictEqual({
value: expect.any(Object),
ttl: expect.any(Number)
})
cache.set(1511234, { _id: "nC11Hque84", value: "test-data-4" }, 60 * 1000)
expect(cache.get(1511234)).toStrictEqual({
value: expect.any(Object),
ttl: expect.any(Number)
})
})
test("When using getM() and setM() calls - false case", () => {
const input = [{ key: 1511235, value: "test-data-5" }, { key: 1511236, value: "test-data-6", ttl: 2 * 60 * 1000 }]
cache.setM(input)
const responses = cache.getM([1511235, 1511236])
responses.forEach(response => {
expect(response).toStrictEqual({
value: expect.any(String),
ttl: expect.any(Number)
})
})
})
})
describe("NodeCache Logger configurations for the instance (type: custom)", () => {
let cache
beforeAll(() => {
cache = new NodeCache({
mode: "none",
type: "Custom"
})
})
afterAll(() => {
cache.close()
})
test("NodeCache instance to be not null", () => {
expect(cache).not.toBe(null)
})
test("NodeCache instance with Logger::mode as none", () => {
expect(cache.getLogConfig().mode).toEqual("none")
})
test("NodeCache instance with Logger::type as Custom", () => {
expect(cache.getLogConfig().type).toEqual("Custom")
})
test("NodeCache instance with Logger::path as none", () => {
expect(cache.getLogConfig().path).toEqual(undefined)
})
})
describe("NodeCache for the instance (type: custom) on mode = std", () => {
afterAll(() => {
cache.close()
})
let cache = new NodeCache({
mode: "std",
type: "Custom"
})
test("NodeCache instance to be not null", () => {
expect(cache).not.toBe(null)
})
test("NodeCache instance with Logger::mode as std", () => {
expect(cache.getLogConfig().mode).toEqual("std")
})
test("NodeCache instance with Logger::type as Custom", () => {
expect(cache.getLogConfig().type).toEqual("Custom")
})
test("NodeCache instance with Logger::path as none", () => {
expect(cache.getLogConfig().path).toEqual(undefined)
})
})
describe("NodeCache Falsy path config for the instance", () => {
let cache
beforeAll(() => {
cache = new NodeCache({
mode: "std",
type: "xyz",
path: undefined
})
})
afterAll(() => {
cache.close()
})
test("When valid instance uses default params for logger", () => {
expect(cache).not.toBe(null)
expect(cache.getLogConfig().mode).toEqual("std")
expect(cache.getLogConfig().type).toEqual("xyz")
expect(cache.getLogConfig().path).toEqual(undefined)
})
})
})