-
Notifications
You must be signed in to change notification settings - Fork 178
/
bigcache_test.go
58 lines (53 loc) · 1.59 KB
/
bigcache_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
package fastcache
import (
"bytes"
"fmt"
"testing"
)
func TestSetGetBig(t *testing.T) {
c := New(256 * 1024 * 1024)
const valuesCount = 10
for _, valueSize := range []int{1, 100, 1<<16 - 1, 1 << 16, 1<<16 + 1, 1 << 17, 1<<17 + 1, 1<<17 - 1, 1 << 19} {
t.Run(fmt.Sprintf("valueSize_%d", valueSize), func(t *testing.T) {
for seed := 0; seed < 3; seed++ {
testSetGetBig(t, c, valueSize, valuesCount, seed)
}
})
}
}
func testSetGetBig(t *testing.T, c *Cache, valueSize, valuesCount, seed int) {
m := make(map[string][]byte)
var buf []byte
for i := 0; i < valuesCount; i++ {
key := []byte(fmt.Sprintf("key %d", i))
value := createValue(valueSize, seed)
c.SetBig(key, value)
m[string(key)] = value
buf = c.GetBig(buf[:0], key)
if !bytes.Equal(buf, value) {
t.Fatalf("seed=%d; unexpected value obtained for key=%q; got len(value)=%d; want len(value)=%d", seed, key, len(buf), len(value))
}
}
var s Stats
c.UpdateStats(&s)
if s.SetBigCalls < uint64(valuesCount) {
t.Fatalf("expecting SetBigCalls >= %d; got %d", valuesCount, s.SetBigCalls)
}
if s.GetBigCalls < uint64(valuesCount) {
t.Fatalf("expecting GetBigCalls >= %d; got %d", valuesCount, s.GetBigCalls)
}
// Verify that values stil exist
for key, value := range m {
buf = c.GetBig(buf[:0], []byte(key))
if !bytes.Equal(buf, value) {
t.Fatalf("seed=%d; unexpected value obtained for key=%q; got len(value)=%d; want len(value)=%d", seed, key, len(buf), len(value))
}
}
}
func createValue(size, seed int) []byte {
var buf []byte
for i := 0; i < size; i++ {
buf = append(buf, byte(i+seed))
}
return buf
}