/
lru_cache_key_uint64_test.go
337 lines (279 loc) · 8.79 KB
/
lru_cache_key_uint64_test.go
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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lru
import (
"encoding/json"
key "github.com/0studio/storage_key"
"testing"
)
func TestKeyUint64InitialState(t *testing.T) {
cache := NewLRUCacheKeyUint64(5)
l, sz, c := cache.Stats()
if l != 0 {
t.Errorf("length = %v, want 0", l)
}
if sz != 0 {
t.Errorf("size = %v, want 0", sz)
}
if c != 5 {
t.Errorf("capacity = %v, want 5", c)
}
}
func TestKeyUint64SetInsertsValue(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
data := &CacheValue{0}
var k key.KeyUint64 = 1
cache.Set(k, data)
v, ok := cache.Get(k)
if !ok || v.(*CacheValue) != data {
t.Errorf("Cache has incorrect value: %v != %v", data, v)
}
keys := cache.Keys()
if len(keys) != 1 || keys[0] != k {
t.Errorf("Cache.Keys() returned incorrect items: %v", k)
}
items := cache.Items()
if len(items) != 1 || items[0].Key != k {
t.Errorf("Cache.Values() returned incorrect items: %v", items)
}
values := cache.Values()
if len(values) != 1 {
t.Errorf("Cache.Values() returned incorrect values: %v", values)
}
}
func TestKeyUint64SetIfAbsent(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
data := &CacheValue{0}
var k key.KeyUint64 = 1
cache.SetIfAbsent(k, data)
v, ok := cache.Get(k)
if !ok || v.(*CacheValue) != data {
t.Errorf("Cache has incorrect value: %v != %v", data, v)
}
cache.SetIfAbsent(k, &CacheValue{1})
v, ok = cache.Get(k)
if !ok || v.(*CacheValue) != data {
t.Errorf("Cache has incorrect value: %v != %v", data, v)
}
}
func TestKeyUint64GetValueWithMultipleTypes(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
data := &CacheValue{0}
var k key.KeyUint64 = 1
cache.Set(k, data)
v, ok := cache.Get(k)
if !ok || v.(*CacheValue) != data {
t.Errorf("Cache has incorrect value for \"k\": %v != %v", data, v)
}
// v, ok = cache.Get(string([]byte{'k', 'e', 'y'}))
// if !ok || v.(*CacheValue) != data {
// t.Errorf("Cache has incorrect value for []byte {'k','e','y'}: %v != %v", data, v)
// }
}
func TestKeyUint64SetUpdatesSize(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
emptyValue := &CacheValue{0}
k := key.KeyUint64(1)
cache.Set(k, emptyValue)
if _, sz, _ := cache.Stats(); sz != 0 {
t.Errorf("cache.Size() = %v, expected 0", sz)
}
someValue := &CacheValue{20}
k = key.KeyUint64(2)
cache.Set(k, someValue)
if _, sz, _ := cache.Stats(); sz != 20 {
t.Errorf("cache.Size() = %v, expected 20", sz)
}
}
func TestKeyUint64SetWithOldKeyUpdatesValue(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
emptyValue := &CacheValue{0}
k := key.KeyUint64(1)
cache.Set(k, emptyValue)
someValue := &CacheValue{20}
cache.Set(k, someValue)
v, ok := cache.Get(k)
if !ok || v.(*CacheValue) != someValue {
t.Errorf("Cache has incorrect value: %v != %v", someValue, v)
}
}
func TestKeyUint64SetWithOldKeyUpdatesSize(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
emptyValue := &CacheValue{0}
k := key.KeyUint64(1)
cache.Set(k, emptyValue)
if _, sz, _ := cache.Stats(); sz != 0 {
t.Errorf("cache.Size() = %v, expected %v", sz, 0)
}
someValue := &CacheValue{20}
cache.Set(k, someValue)
expected := int64(someValue.size)
if _, sz, _ := cache.Stats(); sz != expected {
t.Errorf("cache.Size() = %v, expected %v", sz, expected)
}
}
func TestKeyUint64GetNonExistent(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
if _, ok := cache.Get(1); ok {
t.Error("Cache returned a crap value after no inserts.")
}
}
func TestKeyUint64Delete(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
value := &CacheValue{1}
var k key.KeyUint64 = 1
if cache.Delete(k) {
t.Error("Item unexpectedly already in cache.")
}
cache.Set(k, value)
if !cache.Delete(k) {
t.Error("Expected item to be in cache.")
}
if _, sz, _ := cache.Stats(); sz != 0 {
t.Errorf("cache.Size() = %v, expected 0", sz)
}
if _, ok := cache.Get(k); ok {
t.Error("Cache returned a value after deletion.")
}
}
func TestKeyUint64Clear(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
value := &CacheValue{1}
var k key.KeyUint64 = 1
cache.Set(k, value)
cache.Clear()
if _, sz, _ := cache.Stats(); sz != 0 {
t.Errorf("cache.Size() = %v, expected 0 after Clear()", sz)
}
}
func TestKeyUint64CapacityIsObeyed(t *testing.T) {
size := int64(3)
cache := NewLRUCacheKeyUint64(100)
cache.SetCapacity(size)
value := &CacheValue{1}
// Insert up to the cache's capacity.
cache.Set(1, value)
cache.Set(2, value)
cache.Set(3, value)
if _, sz, _ := cache.Stats(); sz != size {
t.Errorf("cache.Size() = %v, expected %v", sz, size)
}
// Insert one more; something should be evicted to make room.
cache.Set(4, value)
if _, sz, _ := cache.Stats(); sz != size {
t.Errorf("post-evict cache.Size() = %v, expected %v", sz, size)
}
// Check json stats
data := cache.StatsJSON()
m := make(map[string]interface{})
if err := json.Unmarshal([]byte(data), &m); err != nil {
t.Errorf("cache.StatsJSON() returned bad json data: %v %v", data, err)
}
if m["Size"].(float64) != float64(size) {
t.Errorf("cache.StatsJSON() returned bad size: %v", m)
}
// Check various other stats
if l := cache.Length(); l != size {
t.Errorf("cache.StatsJSON() returned bad length: %v", l)
}
if s := cache.Size(); s != size {
t.Errorf("cache.StatsJSON() returned bad size: %v", s)
}
if c := cache.Capacity(); c != size {
t.Errorf("cache.StatsJSON() returned bad length: %v", c)
}
// checks StatsJSON on nil
cache = nil
if s := cache.StatsJSON(); s != "{}" {
t.Errorf("cache.StatsJSON() on nil object returned %v", s)
}
}
func TestKeyUint64LRUIsEvicted(t *testing.T) {
size := int64(3)
cache := NewLRUCacheKeyUint64(size)
cache.Set(1, &CacheValue{1})
cache.Set(2, &CacheValue{1})
cache.Set(3, &CacheValue{1})
// lru: [k3, k2, k1]
// Look up the elements. This will rearrange the LRU ordering.
cache.Get(3)
// beforeKey2 := time.Now()
cache.Get(2)
// afterKey2 := time.Now()
cache.Get(1)
// lru: [k1, k2, k3]
cache.Set(0, &CacheValue{1})
// lru: [k0, k1, k2]
// The least recently used one should have been evicted.
if _, ok := cache.Get(3); ok {
t.Error("Least recently used element was not evicted.")
}
// // Check oldest
// if o := cache.Oldest(); o.Before(beforeKey2) || o.After(afterKey2) {
// t.Errorf("cache.Oldest returned an unexpected value: got %v, expected a value between %v and %v", o, beforeKey2, afterKey2)
// }
}
type PurgeCacheValueKeyUint64 struct {
}
func (cv *PurgeCacheValueKeyUint64) OnPurge(why PurgeReason) {
purgeReasonFlag4TestKeyUint64 = why
}
var purgeReasonFlag4TestKeyUint64 PurgeReason
func TestKeyUint64DeleteOnPurge(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
value := &PurgeCacheValueKeyUint64{}
purgeReasonFlag4TestKeyUint64 = PURGE_REASON_CACHEFULL // init
var k key.KeyUint64 = 1
cache.Set(k, value)
cache.Delete(k)
if purgeReasonFlag4TestKeyUint64 != PURGE_REASON_DELETE {
t.Errorf("after cache.Delete ,purgeReason should be %d ,but get %d", PURGE_REASON_DELETE, purgeReasonFlag4TestKeyUint64)
}
}
func TestKeyUint64UpdateOnPurge(t *testing.T) {
cache := NewLRUCacheKeyUint64(100)
value := &PurgeCacheValueKeyUint64{}
purgeReasonFlag4TestKeyUint64 = PURGE_REASON_CACHEFULL // init
var k key.KeyUint64 = 1
cache.Set(k, value)
cache.Set(k, value) // set again
if purgeReasonFlag4TestKeyUint64 != PURGE_REASON_UPDATE {
t.Errorf("after cache.Delete ,purgeReason should be %d ,but get %d", PURGE_REASON_UPDATE, purgeReasonFlag4TestKeyUint64)
}
}
func TestKeyUint64CacheFullOnPurge(t *testing.T) {
cache := NewLRUCacheKeyUint64(1)
value := &PurgeCacheValueKeyUint64{}
k1 := key.KeyUint64(1)
k2 := key.KeyUint64(2)
cache.Set(k1, value) // after this cache is full
purgeReasonFlag4TestKeyUint64 = PURGE_REASON_DELETE // init
cache.Set(k2, value) // after this k1 is delete and reason set to PURGE_REASON_CACHEFULL
if purgeReasonFlag4TestKeyUint64 != PURGE_REASON_CACHEFULL {
t.Errorf("after cache.Delete ,purgeReason should be %d ,but get %d", PURGE_REASON_CACHEFULL, purgeReasonFlag4TestKeyUint64)
}
}
func TestKeyUint64ClearOnPurge(t *testing.T) {
cache := NewLRUCacheKeyUint64(1)
value := &PurgeCacheValueKeyUint64{}
k1 := key.KeyUint64(1)
purgeReasonFlag4TestKeyUint64 = PURGE_REASON_DELETE // init
cache.Set(k1, value) // after this cache is full
cache.Clear()
if purgeReasonFlag4TestKeyUint64 != PURGE_REASON_CLEAR_ALL {
t.Errorf("after cache.Delete ,purgeReason should be %d ,but get %d", PURGE_REASON_CLEAR_ALL, purgeReasonFlag4TestKeyUint64)
}
}
func TestKeyUint64OnMiss(t *testing.T) {
fun := func(k key.KeyUint64) (Cacheable, bool) {
return 1, true
}
cache := NewLRUCacheKeyUint64(1)
cache.OnMiss(fun)
k1 := key.KeyUint64(1)
v, ok := cache.Get(k1) //
if ok != true && v != 1 {
t.Errorf("lru.onMiss is errror")
}
}