-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcache.test.js
83 lines (71 loc) · 2.71 KB
/
cache.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
import * as t from './testing.js'
import * as cache from './cache.js'
import * as promise from './promise.js'
/**
* @param {t.TestCase} tc
*/
export const testCache = async tc => {
/**
* @type {cache.Cache<string, string>}
*/
const c = cache.create(50)
cache.set(c, 'a', '1')
t.assert(cache.get(c, 'a') === '1')
t.assert(await cache.getAsync(c, 'a') === '1')
const p = cache.setIfUndefined(c, 'b', () => promise.resolveWith('2'))
const q = cache.setIfUndefined(c, 'b', () => promise.resolveWith('3'))
t.assert(p === q)
t.assert(cache.get(c, 'b') == null)
t.assert(cache.getAsync(c, 'b') === p)
t.assert(await p === '2')
t.assert(cache.get(c, 'b') === '2')
t.assert(cache.getAsync(c, 'b') === '2')
await promise.wait(5) // keys shouldn't be timed out yet
t.assert(cache.get(c, 'a') === '1')
t.assert(cache.get(c, 'b') === '2')
/**
* @type {any}
*/
const m = c._map
const aTimestamp1 = m.get('a').created
const bTimestamp1 = m.get('b').created
// write new values and check later if the creation-timestamp was updated
cache.set(c, 'a', '11')
cache.set(c, 'b', '22')
await promise.wait(5) // keys should be updated and not timed out. Hence the creation time should be updated
t.assert(cache.get(c, 'a') === '11')
t.assert(cache.get(c, 'b') === '22')
cache.set(c, 'a', '11')
cache.set(c, 'b', '22')
// timestamps should be updated
t.assert(aTimestamp1 !== m.get('a').created)
t.assert(bTimestamp1 !== m.get('b').created)
await promise.wait(60) // now the keys should be timed-out
t.assert(cache.get(c, 'a') == null)
t.assert(cache.getAsync(c, 'b') == null)
t.assert(c._map.size === 0)
t.assert(c._q.start === null && c._q.end === null)
// test edge case of setIfUndefined
const xp = cache.setIfUndefined(c, 'a', () => promise.resolve('x'))
cache.set(c, 'a', 'y')
await xp
// we override the Entry.val property in cache when p resolves. However, we must prevent that when the value is overriden before p is resolved.
t.assert(cache.get(c, 'a') === 'y')
// test that we can remove properties
cache.remove(c, 'a')
cache.remove(c, 'does not exist') // remove a non-existent property to achieve full test-coverage
t.assert(cache.get(c, 'a') === undefined)
// test that the optional property in setifUndefined works
const yp = cache.setIfUndefined(c, 'a', () => promise.resolveWith(null), true)
t.assert(await yp === null)
t.assert(cache.get(c, 'a') === undefined)
// check manual updating of timeout
cache.set(c, 'a', '3')
const ts1 = m.get('a').created
await promise.wait(30)
cache.refreshTimeout(c, 'a')
const ts2 = m.get('a').created
t.assert(ts1 !== ts2)
cache.refreshTimeout(c, 'x') // for full test coverage
t.assert(m.get('x') == null)
}