-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_test.go
284 lines (249 loc) · 7.76 KB
/
common_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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xcache/blob/main/LICENSE.
package xcache_test
import (
"context"
"errors"
"strconv"
"sync"
"testing"
"time"
"github.com/actforgood/xcache"
)
func testCacheWithNoExpireKey(subject xcache.Cache) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
key = "test-no-expire-key"
value = []byte("test value")
ctx = context.Background()
exp = xcache.NoExpire
)
// act & assert save
resultErr := subject.Save(ctx, key, value, exp)
requireNil(t, resultErr)
for i := 0; i < 50; i++ {
// act & assert load
resultValue, resultErr := subject.Load(ctx, key)
assertNil(t, resultErr)
assertEqual(t, value, resultValue)
}
// act & assert ttl
resultTTL, resultErr := subject.TTL(ctx, key)
assertNil(t, resultErr)
assertEqual(t, xcache.NoExpire, resultTTL)
}
}
func testCacheWithExpireKey(subject xcache.Cache) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
key = "test-expire-key"
value = []byte("test value")
ctx = context.Background()
exp = 500 * time.Millisecond
)
// act & assert save
resultErr := subject.Save(ctx, key, value, exp)
requireNil(t, resultErr)
// act & assert load
resultValue, resultErr := subject.Load(ctx, key)
assertNil(t, resultErr)
assertEqual(t, value, resultValue)
// act & assert load after expire time passed
time.Sleep(1100 * time.Millisecond)
resultValue, resultErr = subject.Load(ctx, key)
assertTrue(t, errors.Is(resultErr, xcache.ErrNotFound))
assertNil(t, resultValue)
// act & assert ttl
resultTTL, resultErr := subject.TTL(ctx, key)
assertNil(t, resultErr)
assertTrue(t, resultTTL < 0)
}
}
func testCacheWithNotExistKey(subject xcache.Cache) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
key = "test-this-key-does-not-exist"
ctx = context.Background()
)
// act & assert load
resultValue, resultErr := subject.Load(ctx, key)
assertTrue(t, errors.Is(resultErr, xcache.ErrNotFound))
assertNil(t, resultValue)
// act & assert ttl
resultTTL, resultErr := subject.TTL(ctx, key)
assertNil(t, resultErr)
assertTrue(t, resultTTL < 0)
}
}
func testCacheDeleteKey(subject xcache.Cache) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
key = "test-delete-key"
value = []byte("test value")
ctx = context.Background()
exp = xcache.NoExpire
)
// act & assert save
resultErr := subject.Save(ctx, key, value, exp)
requireNil(t, resultErr)
// act & assert load
resultValue, resultErr := subject.Load(ctx, key)
assertNil(t, resultErr)
assertEqual(t, value, resultValue)
// act & assert delete
resultErr = subject.Save(ctx, key, nil, -1)
requireNil(t, resultErr)
// act & assert load
resultValue, resultErr = subject.Load(ctx, key)
assertTrue(t, errors.Is(resultErr, xcache.ErrNotFound))
assertNil(t, resultValue)
// act & assert ttl
resultTTL, resultErr := subject.TTL(ctx, key)
assertNil(t, resultErr)
assertTrue(t, resultTTL < 0)
}
}
func testCacheTTLWithNotYetExpiredKey(subject xcache.Cache) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
key = "test-expire-ttl-key"
value = []byte("test value")
ctx = context.Background()
exp = time.Minute
)
// act & assert save
resultErr := subject.Save(ctx, key, value, exp)
requireNil(t, resultErr)
time.Sleep(time.Second)
// act & assert ttl
resultTTL, resultErr := subject.TTL(ctx, key)
assertNil(t, resultErr)
assertTrue(t, resultTTL < exp)
assertTrue(t, resultTTL > 0)
}
}
func testCacheStats(
subject xcache.Cache,
expectedMem, expectedMaxMem int64, memCheckOp string,
checkKeys bool,
) func(t *testing.T) {
return func(t *testing.T) {
// arrange
var (
hitKey = "test-stats-hit-key-"
missKey = "test-stats-miss-key"
expKey = "test-stats-exp-key-"
value = []byte("test value")
ctx = context.Background()
smallerExp = 2 * time.Second
biggerExp = 10 * time.Minute
)
for i := 0; i < 20; i++ { // delete keys needed for Keys reporting
key := hitKey + strconv.FormatInt(int64(i), 10)
_ = subject.Save(ctx, key, nil, -1)
}
prevStats, resultErr := subject.Stats(ctx)
requireNil(t, resultErr)
for i := 0; i < 20; i++ { // 20 x hit
key := hitKey + strconv.FormatInt(int64(i), 10)
resultErr = subject.Save(ctx, key, value, biggerExp)
requireNil(t, resultErr)
_, resultErr = subject.Load(ctx, key)
requireNil(t, resultErr)
}
for i := 0; i < 10; i++ { // 10 x hit, 10 x expired
key := expKey + strconv.FormatInt(int64(i), 10)
resultErr = subject.Save(ctx, key, value, smallerExp)
requireNil(t, resultErr)
_, resultErr = subject.Load(ctx, key)
requireNil(t, resultErr)
}
for i := 0; i < 25; i++ { // 25 x miss
_, resultErr = subject.Load(ctx, missKey)
assertTrue(t, errors.Is(resultErr, xcache.ErrNotFound))
}
time.Sleep(2500 * time.Millisecond) // let keys with smallerExp expire
for i := 0; i < 10; i++ { // 10 x miss
key := expKey + strconv.FormatInt(int64(i), 10) // load expired keys to let Freecache count the expiration
_, resultErr = subject.Load(ctx, key)
assertTrue(t, errors.Is(resultErr, xcache.ErrNotFound))
}
// act
resultStats, resultErr := subject.Stats(ctx)
// assert
assertNil(t, resultErr)
if memCheckOp == "==" {
assertEqual(t, expectedMem, resultStats.Memory)
assertEqual(t, expectedMaxMem, resultStats.MaxMemory)
} else {
assertTrue(t, resultStats.Memory >= expectedMem)
assertTrue(t, resultStats.MaxMemory >= expectedMaxMem)
}
assertTrue(t, resultStats.Hits >= prevStats.Hits+30)
assertTrue(t, resultStats.Misses >= prevStats.Misses+35)
assertTrue(t, resultStats.Expired >= prevStats.Expired+10)
assertTrue(t, resultStats.Evicted >= prevStats.Evicted)
if checkKeys {
assertTrue(t, resultStats.Keys >= prevStats.Keys+20)
}
}
}
func testCacheWithXConfConcurrency(subject xcache.Cache) func(t *testing.T) {
return func(t *testing.T) {
// Note: test to be run with -race and see no race conditions occur.
// arrange
var (
commonKey = "test-concurrency-key"
ctx, cancelCtx = context.WithTimeout(context.Background(), 10*time.Second)
goroutinesNo = 200
wg sync.WaitGroup
)
defer cancelCtx()
// save a common key that will be accessed
err := subject.Save(context.Background(), commonKey, []byte("test value"), 5*time.Minute)
requireNil(t, err)
wg.Add(goroutinesNo)
for threadNo := 0; threadNo < goroutinesNo; threadNo++ {
go func(ctx context.Context, cache xcache.Cache, waitGr *sync.WaitGroup, thread int) {
defer waitGr.Done()
for {
select {
case <-ctx.Done():
return
default:
// cover all APIs of Cache: perform save, load, ttl, stats operations.
for i := 0; i < 10; i++ {
key := "test-concurrency-key-" + strconv.FormatInt(int64(thread), 10) + "-" + strconv.FormatInt(int64(i), 10)
err := cache.Save(context.Background(), key, []byte("test value"), time.Minute)
assertNil(t, err)
_, err = cache.Load(context.Background(), key)
assertNil(t, err)
_, err = cache.Load(context.Background(), commonKey)
assertNil(t, err)
_, err = cache.TTL(context.Background(), key)
assertNil(t, err)
_, err = cache.TTL(context.Background(), commonKey)
assertNil(t, err)
_, err = cache.Stats(context.Background())
assertNil(t, err)
}
time.Sleep(100 * time.Millisecond)
}
}
}(ctx, subject, &wg, threadNo)
}
wg.Wait() // after context deadline expires (10s), goroutines will stop.
}
}