-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
222 lines (174 loc) · 5.99 KB
/
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
const test = require('brittle')
const Rache = require('.')
test('basic set and get', t => {
const cache = new Rache()
cache.set('key', 'value')
t.is(cache.get('key'), 'value', 'correct value')
})
test('set of existing value', t => {
const cache = new Rache()
cache.set('key', 'value')
t.is(cache.get('key'), 'value', 'sanity check')
cache.set('key', 'new')
t.is(cache.get('key'), 'new', 'updated value')
t.is(cache.size, 1, 'correct size')
})
test('creating with Rache.from', t => {
const cache = Rache.from()
t.pass('can create new cache with Rache.from')
const sub = Rache.from(cache)
sub.set('key', 'value')
t.is(cache.globalSize, 1, 'from links back to the global cache')
})
test('gc triggers when full and removes 1 entry', t => {
const cache = new Rache({ maxSize: 3 })
cache.set('key', 'value')
cache.set('key2', 'value2')
cache.set('key3', 'value3')
t.is(cache.size, 3, 'sanity check')
cache.set('key4', 'value4')
t.is(cache.size, 3, 'cache did not grow past maxSize')
const values = [cache.get('key'), cache.get('key2'), cache.get('key3')]
const nrMisses = values.filter(v => v === undefined).length
t.is(nrMisses, 1, 'exactly 1 old item got removed from the cache')
})
test('subs share same cache, but no key conflicts', t => {
const cache = new Rache({ maxSize: 3 })
const sub1 = cache.sub()
const sub2 = cache.sub()
sub1.set('key', 'value1')
sub2.set('key', 'value2')
t.is(cache.globalSize, 2, '2 entries')
t.is(sub1.get('key'), 'value1', 'sub1 value')
t.is(sub2.get('key'), 'value2', 'sub2 value')
sub1.set('key2', 'value1')
sub1.set('key3', 'value1')
const entries = [sub1.get('key'), sub2.get('key'), sub1.get('key2'), sub1.get('key3')]
const nrMisses = entries.filter(e => e === undefined).length
t.is(nrMisses, 1, '1 entry got cleared from the global cache')
})
test('delete', t => {
const cache = new Rache()
const sub = cache.sub()
cache.set('key', 'value')
cache.set('what', 'ever')
sub.set('key', 'value')
sub.set('what2', 'ever 2')
t.is(cache.globalSize, 4, 'sanity check')
t.is(cache.size, 2, 'sanity check')
{
const deleted = cache.delete('key')
t.is(deleted, true, 'true when deleted')
}
t.is(cache.globalSize, 3, 'removed globally')
t.is(cache.size, 1, 'removed locally')
t.is(cache.get('key'), undefined, 'no entry')
{
const deleted = sub.delete('key')
t.is(deleted, true, 'true when deleted')
}
t.is(sub.globalSize, 2, 'removed globally')
t.is(sub.size, 1, 'removed locally')
t.is(sub.get('key'), undefined, 'no entry')
t.is(
sub.delete('nothing here'),
false,
'false when nothing to delete'
)
})
test('iterator', t => {
const cache = new Rache()
const sub = cache.sub()
cache.set('key', 'value')
cache.set('key2', 'value2')
sub.set('key', 'value')
sub.set('what2', 'ever2')
{
const res = []
const expected = [['key', 'value'], ['key2', 'value2']]
for (const entry of cache) res.push(entry)
t.alike(res, expected, 'iterator entries')
}
{
const res = []
const expected = [['key', 'value'], ['what2', 'ever2']]
for (const entry of sub) res.push(entry)
t.alike(res, expected, 'iterator entries')
}
})
test('keys()', t => {
const cache = new Rache()
const sub = cache.sub()
cache.set('key', 'value')
cache.set('key2', 'value2')
sub.set('key', 'value')
sub.set('what2', 'ever2')
t.alike([...cache.keys()], ['key', 'key2'], 'expected keys')
t.alike([...sub.keys()], ['key', 'what2'], 'expected keys')
})
test('values()', t => {
const cache = new Rache()
const sub = cache.sub()
cache.set('key', 'value')
cache.set('key2', 'value2')
sub.set('key', 'value')
sub.set('what2', 'ever2')
t.alike([...cache.values()], ['value', 'value2'], 'expected values')
t.alike([...sub.values()], ['value', 'ever2'], 'expected values')
})
test('clear()', t => {
const cache = new Rache()
const sub = cache.sub()
cache.set('key', 'value')
cache.set('key2', 'value2')
sub.set('key', 'value')
sub.set('what2', 'ever2')
sub.clear()
t.is(sub.size, 0, 'cleared')
// DEVNOTE: this assertion documents the current behaviour,
// that cleared entries linger on in the _array. But if this
// test ever fails because we no longer have those entries linger
// then it's fine to modify this assertion
t.is(sub.globalSize, 4, 'elements still exist in the array')
sub.set('key', 'value') // Reset it
t.is(sub.globalSize, 5, 'old and new entry exist simultaneously')
sub.delete('key')
t.is(sub.globalSize, 4, 'old entry unaffected by new entry deletion')
})
test('destroy()', t => {
const cache = new Rache()
const sub = cache.sub()
cache.set('key', 'value')
cache.set('key2', 'value2')
sub.set('key', 'value')
sub.set('what2', 'ever2')
sub.destroy()
t.is(sub._array, null, 'null internal array')
t.is(sub._map, null, 'null internal map')
t.is(cache._array !== null, true, 'no null for cache')
t.is(cache._map !== null, true, 'no null for cache')
// DEVNOTE: this assertion documents the current behaviour,
// that destroyed entries linger on in the _array. But if this
// test ever fails because we no longer have those entries linger
// then it's fine to modify this assertion
t.is(cache.globalSize, 4, 'not yet removed from internal array')
})
test('internal structure remains consistent', t => {
const cache = new Rache({ maxSize: 3 })
for (let i = 0; i < 1000; i++) {
cache.set(`key${i}`, `value${i}`)
if (i > 100) cache.set(`key${i - 50}`, 'updated')
ensureConsistent(cache)
}
})
function ensureConsistent (cache) {
if (cache.globalSize > cache.maxSize || cache.globalSize !== cache._array.length) throw new Error('size')
for (const entry of cache._array) {
const mapEntry = entry.map.get(entry.key)
if (mapEntry.entry !== entry) throw new Error('different entry obj in map/cache')
}
for (const { entry } of cache._map.values()) {
const arrayEntry = cache._array[entry.index]
if (arrayEntry !== entry) throw new Error('different entry obj in map/cache')
}
}