-
Notifications
You must be signed in to change notification settings - Fork 175
/
fastcache_test.go
287 lines (262 loc) · 7.09 KB
/
fastcache_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
package fastcache
import (
"fmt"
"runtime"
"sync"
"testing"
"time"
)
func TestCacheSmall(t *testing.T) {
c := New(1)
defer c.Reset()
if v := c.Get(nil, []byte("aaa")); len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from small cache: %q", v)
}
if v, exist := c.HasGet(nil, []byte("aaa")); exist || len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from small cache: %q", v)
}
c.Set([]byte("key"), []byte("value"))
if v := c.Get(nil, []byte("key")); string(v) != "value" {
t.Fatalf("unexpected value obtained; got %q; want %q", v, "value")
}
if v := c.Get(nil, nil); len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from small cache: %q", v)
}
if v, exist := c.HasGet(nil, nil); exist {
t.Fatalf("unexpected nil-keyed value obtained in small cache: %q", v)
}
if v := c.Get(nil, []byte("aaa")); len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from small cache: %q", v)
}
c.Set([]byte("aaa"), []byte("bbb"))
if v := c.Get(nil, []byte("aaa")); string(v) != "bbb" {
t.Fatalf("unexpected value obtained; got %q; want %q", v, "bbb")
}
if v, exist := c.HasGet(nil, []byte("aaa")); !exist || string(v) != "bbb" {
t.Fatalf("unexpected value obtained; got %q; want %q", v, "bbb")
}
c.Reset()
if v := c.Get(nil, []byte("aaa")); len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from empty cache: %q", v)
}
if v, exist := c.HasGet(nil, []byte("aaa")); exist || len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from small cache: %q", v)
}
// Test empty value
k := []byte("empty")
c.Set(k, nil)
if v := c.Get(nil, k); len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from empty entry: %q", v)
}
if v, exist := c.HasGet(nil, k); !exist {
t.Fatalf("cannot find empty entry for key %q", k)
} else if len(v) != 0 {
t.Fatalf("unexpected non-empty value obtained from empty entry: %q", v)
}
if !c.Has(k) {
t.Fatalf("cannot find empty entry for key %q", k)
}
if c.Has([]byte("foobar")) {
t.Fatalf("non-existing entry found in the cache")
}
}
func TestCacheWrap(t *testing.T) {
c := New(bucketsCount * chunkSize * 1.5)
defer c.Reset()
calls := uint64(5e6)
for i := uint64(0); i < calls; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
c.Set(k, v)
vv := c.Get(nil, k)
if string(vv) != string(v) {
t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
}
}
for i := uint64(0); i < calls/10; i++ {
x := i * 10
k := []byte(fmt.Sprintf("key %d", x))
v := []byte(fmt.Sprintf("value %d", x))
vv := c.Get(nil, k)
if len(vv) > 0 && string(v) != string(vv) {
t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
}
}
var s Stats
c.UpdateStats(&s)
getCalls := calls + calls/10
if s.GetCalls != getCalls {
t.Fatalf("unexpected number of getCalls; got %d; want %d", s.GetCalls, getCalls)
}
if s.SetCalls != calls {
t.Fatalf("unexpected number of setCalls; got %d; want %d", s.SetCalls, calls)
}
if s.Misses == 0 || s.Misses >= calls/10 {
t.Fatalf("unexpected number of misses; got %d; it should be between 0 and %d", s.Misses, calls/10)
}
if s.Collisions != 0 {
t.Fatalf("unexpected number of collisions; got %d; want 0", s.Collisions)
}
if s.EntriesCount < calls/5 {
t.Fatalf("unexpected number of items; got %d; cannot be smaller than %d", s.EntriesCount, calls/5)
}
if s.BytesSize < 1024 {
t.Fatalf("unexpected BytesSize; got %d; cannot be smaller than %d", s.BytesSize, 1024)
}
if s.MaxBytesSize < 32*1024*1024 {
t.Fatalf("unexpected MaxBytesSize; got %d; cannot be smaller than %d", s.MaxBytesSize, 32*1024*1024)
}
}
func TestCacheDel(t *testing.T) {
c := New(1024)
defer c.Reset()
for i := 0; i < 100; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
c.Set(k, v)
vv := c.Get(nil, k)
if string(vv) != string(v) {
t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
}
c.Del(k)
vv = c.Get(nil, k)
if len(vv) > 0 {
t.Fatalf("unexpected non-empty value got for key %q: %q", k, vv)
}
}
}
func TestCacheBigKeyValue(t *testing.T) {
c := New(1024)
defer c.Reset()
// Both key and value exceed 64Kb
k := make([]byte, 90*1024)
v := make([]byte, 100*1024)
c.Set(k, v)
vv := c.Get(nil, k)
if len(vv) > 0 {
t.Fatalf("unexpected non-empty value got for key %q: %q", k, vv)
}
// len(key) + len(value) > 64Kb
k = make([]byte, 40*1024)
v = make([]byte, 40*1024)
c.Set(k, v)
vv = c.Get(nil, k)
if len(vv) > 0 {
t.Fatalf("unexpected non-empty value got for key %q: %q", k, vv)
}
}
func TestCacheSetGetSerial(t *testing.T) {
itemsCount := 10000
c := New(30 * itemsCount)
defer c.Reset()
if err := testCacheGetSet(c, itemsCount); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}
func TestCacheGetSetConcurrent(t *testing.T) {
itemsCount := 10000
const gorotines = 10
c := New(30 * itemsCount * gorotines)
defer c.Reset()
ch := make(chan error, gorotines)
for i := 0; i < gorotines; i++ {
go func() {
ch <- testCacheGetSet(c, itemsCount)
}()
}
for i := 0; i < gorotines; i++ {
select {
case err := <-ch:
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout")
}
}
}
func testCacheGetSet(c *Cache, itemsCount int) error {
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
c.Set(k, v)
vv := c.Get(nil, k)
if string(vv) != string(v) {
return fmt.Errorf("unexpected value for key %q after insertion; got %q; want %q", k, vv, v)
}
}
misses := 0
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("key %d", i))
vExpected := fmt.Sprintf("value %d", i)
v := c.Get(nil, k)
if string(v) != string(vExpected) {
if len(v) > 0 {
return fmt.Errorf("unexpected value for key %q after all insertions; got %q; want %q", k, v, vExpected)
}
misses++
}
}
if misses >= itemsCount/100 {
return fmt.Errorf("too many cache misses; got %d; want less than %d", misses, itemsCount/100)
}
return nil
}
func TestCacheResetUpdateStatsSetConcurrent(t *testing.T) {
c := New(12334)
stopCh := make(chan struct{})
// run workers for cache reset
var resettersWG sync.WaitGroup
for i := 0; i < 10; i++ {
resettersWG.Add(1)
go func() {
defer resettersWG.Done()
for {
select {
case <-stopCh:
return
default:
c.Reset()
runtime.Gosched()
}
}
}()
}
// run workers for update cache stats
var statsWG sync.WaitGroup
for i := 0; i < 10; i++ {
statsWG.Add(1)
go func() {
defer statsWG.Done()
var s Stats
for {
select {
case <-stopCh:
return
default:
c.UpdateStats(&s)
runtime.Gosched()
}
}
}()
}
// run workers for setting data to cache
var settersWG sync.WaitGroup
for i := 0; i < 10; i++ {
settersWG.Add(1)
go func() {
defer settersWG.Done()
for j := 0; j < 100; j++ {
key := []byte(fmt.Sprintf("key_%d", j))
value := []byte(fmt.Sprintf("value_%d", j))
c.Set(key, value)
runtime.Gosched()
}
}()
}
// wait for setters
settersWG.Wait()
close(stopCh)
statsWG.Wait()
resettersWG.Wait()
}