-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
183 lines (140 loc) · 3.2 KB
/
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
package ccache_test
import (
"math/rand"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/gavrilaf/ccache"
)
func TestCacheBasic(t *testing.T) {
c := ccache.NewCache(10)
t.Run("empty", func(t *testing.T) {
_, ok := c.Get("k1")
assert.False(t, ok)
})
t.Run("set/get simple value", func(t *testing.T) {
added := c.Set("k1", 1, 0)
assert.True(t, added)
v, ok := c.Get("k1")
assert.True(t, ok)
assert.Equal(t, 1, v)
})
t.Run("add again", func(t *testing.T) {
added := c.Set("k1", 1, 0)
assert.False(t, added)
})
t.Run("replace value", func(t *testing.T) {
added := c.Set("k1", 2, 0)
assert.False(t, added)
assertValue(t, c, "k1", 2)
})
t.Run("several values", func(t *testing.T) {
c.Set("k2", 2, 0)
c.Set("k3", 3, 0)
assertValue(t, c, "k1", 2)
assertValue(t, c, "k2", 2)
assertValue(t, c, "k3", 3)
})
}
func TestCacheEvixction(t *testing.T) {
oldTick := ccache.EvictionTick
ccache.EvictionTick = 100*time.Millisecond
t.Run("eviction on set", func(t *testing.T) {
c := ccache.NewCache(5)
for i := 0; i < 7; i++ {
c.Set(strconv.Itoa(i), i, 0)
}
time.Sleep(200*time.Millisecond)
assertNoValue(t, c, "0")
assertNoValue(t, c, "1")
for i := 0; i < 5; i++ {
assertValue(t, c, strconv.Itoa(i+2), i+2)
}
})
t.Run("eviction when item was touched", func(t *testing.T) {
c := ccache.NewCache(3)
for i := 0; i < 3; i++ {
c.Set(strconv.Itoa(i), i, 0)
}
assertValue(t, c, "0", 0)
c.Set("3", 3, 0) // should evict key "1", because "0" was touched
time.Sleep(200*time.Millisecond)
assertNoValue(t, c, "1")
assertValue(t, c, "0", 0)
assertValue(t, c, "2", 2)
assertValue(t, c, "3", 3)
})
ccache.EvictionTick = oldTick
}
func TestCacheConcurrency(t *testing.T) {
c := ccache.NewCache(10)
wg := sync.WaitGroup{}
fn := func() {
for i := 0; i < 10; i++ {
c.Set(strconv.Itoa(i), i, 0)
}
wg.Done()
}
for i := 0; i < 5; i++ {
wg.Add(1)
go fn()
}
wg.Wait()
for i := 0; i < 10; i++ {
assertValue(t, c, strconv.Itoa(i), i)
}
}
func BenchmarkCache(b *testing.B) {
c := ccache.NewCache(500)
const valsCount = 1000
vals := make([]int, valsCount)
keys := make([]string, valsCount)
for i := 0; i < valsCount; i++ {
n := rand.Intn(1000)
vals[i] = n
keys[i] = strconv.Itoa(n)
}
b.Run("bench set", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Set(keys[i % valsCount], vals[i % valsCount], 0)
}
})
b.Run("bench get", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Get(keys[i % valsCount])
}
})
b.Run("set in parallel", func(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
c.Set(keys[i % valsCount], vals[i % valsCount], 0)
i++
}
})
})
b.Run("get in parallel", func(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
c.Get(keys[i % valsCount])
i++
}
})
})
}
func assertValue(t *testing.T, c *ccache.Cache, k string, v interface{}) {
vv, ok := c.Get(k)
assert.True(t, ok)
assert.Equal(t, v, vv)
}
func assertNoValue(t *testing.T, c *ccache.Cache, k string) {
_, ok := c.Get(k)
assert.False(t, ok)
}