forked from araddon/lruttl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache_test.go
228 lines (186 loc) · 5.08 KB
/
lru_cache_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
// 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 cache
import (
u "github.com/araddon/gou"
"strconv"
"testing"
"time"
)
func init() {
u.SetupLogging("debug")
}
type CacheValue struct {
size int
}
func (cv *CacheValue) Size() int {
return cv.size
}
func TestInitialState(t *testing.T) {
cache := NewLRUCache(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 TestSetInsertsValue(t *testing.T) {
cache := NewLRUCache(100)
data := &CacheValue{0}
key := "key"
cache.Set(key, data)
v, ok := cache.Get(key)
if !ok || v.(*CacheValue) != data {
t.Errorf("Cache has incorrect value: %v != %v", data, v)
}
}
func TestGetValueWithMultipleTypes(t *testing.T) {
cache := NewLRUCache(100)
data := &CacheValue{0}
key := "key"
cache.Set(key, data)
v, ok := cache.Get("key")
if !ok || v.(*CacheValue) != data {
t.Errorf("Cache has incorrect value for \"key\": %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 TestSetUpdatesSize(t *testing.T) {
cache := NewLRUCache(100)
emptyValue := &CacheValue{0}
key := "key1"
cache.Set(key, emptyValue)
if _, sz, _, _ := cache.Stats(); sz != 0 {
t.Errorf("cache.Size() = %v, expected 0", sz)
}
someValue := &CacheValue{20}
key = "key2"
cache.Set(key, someValue)
if _, sz, _, _ := cache.Stats(); sz != 20 {
t.Errorf("cache.Size() = %v, expected 20", sz)
}
}
func TestSetWithOldKeyUpdatesValue(t *testing.T) {
cache := NewLRUCache(100)
emptyValue := &CacheValue{0}
key := "key1"
cache.Set(key, emptyValue)
someValue := &CacheValue{20}
cache.Set(key, someValue)
v, ok := cache.Get(key)
if !ok || v.(*CacheValue) != someValue {
t.Errorf("Cache has incorrect value: %v != %v", someValue, v)
}
}
func TestSetWithOldKeyUpdatesSize(t *testing.T) {
cache := NewLRUCache(100)
emptyValue := &CacheValue{0}
key := "key1"
cache.Set(key, emptyValue)
if _, sz, _, _ := cache.Stats(); sz != 0 {
t.Errorf("cache.Size() = %v, expected %v", sz, 0)
}
someValue := &CacheValue{20}
cache.Set(key, someValue)
expected := uint64(someValue.size)
if _, sz, _, _ := cache.Stats(); sz != expected {
t.Errorf("cache.Size() = %v, expected %v", sz, expected)
}
}
func TestGetNonExistent(t *testing.T) {
cache := NewLRUCache(100)
if _, ok := cache.Get("crap"); ok {
t.Error("Cache returned a crap value after no inserts.")
}
}
func TestDelete(t *testing.T) {
cache := NewLRUCache(100)
value := &CacheValue{1}
key := "key"
if cache.Delete(key) {
t.Error("Item unexpectedly already in cache.")
}
cache.Set(key, value)
if !cache.Delete(key) {
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(key); ok {
t.Error("Cache returned a value after deletion.")
}
}
func TestClear(t *testing.T) {
cache := NewLRUCache(100)
value := &CacheValue{1}
key := "key"
cache.Set(key, value)
cache.Clear()
if _, sz, _, _ := cache.Stats(); sz != 0 {
t.Errorf("cache.Size() = %v, expected 0 after Clear()", sz)
}
}
func TestCapacityIsObeyed(t *testing.T) {
size := uint64(3)
cache := NewLRUCache(size)
value := &CacheValue{1}
// Insert up to the cache's capacity.
cache.Set("key1", value)
cache.Set("key2", value)
cache.Set("key3", 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("key4", value)
if _, sz, _, _ := cache.Stats(); sz != size {
t.Errorf("post-evict cache.Size() = %v, expected %v", sz, size)
}
}
func TestLRUIsEvicted(t *testing.T) {
size := uint64(3)
cache := NewLRUCache(size)
cache.Set("key1", &CacheValue{1})
cache.Set("key2", &CacheValue{1})
cache.Set("key3", &CacheValue{1})
// lru: [key3, key2, key1]
// Look up the elements. This will rearrange the LRU ordering.
cache.Get("key3")
cache.Get("key2")
cache.Get("key1")
// lru: [key1, key2, key3]
cache.Set("key0", &CacheValue{1})
// lru: [key0, key1, key2]
// The least recently used one should have been evicted.
if _, ok := cache.Get("key3"); ok {
t.Error("Least recently used element was not evicted.")
}
}
func TestLRUTtlEvict(t *testing.T) {
// ttl = 1 second, 100 ms heartbeats, should check 10 times
l := NewTimeEvictLru(10000, 200*time.Millisecond, 50*time.Millisecond)
var evictCt int = 0
l.EvictCallback = func(key string, v Value) {
evictCt++
//u.Debug("evicting ", key)
}
for i := 0; i < 100; i++ {
istr := strconv.FormatInt(int64(i), 10)
l.Set("key"+istr, &CacheValue{i})
}
u.WaitFor(func() bool {
return l.Len() == 0
}, 10)
u.Assert(l.Len() == 0, t, "should have evicted all items but still have %d", l.Len())
u.Assert(evictCt == 100, t, "Should have evicted 100 items but have %d", evictCt)
}