-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexpirable_cache_test.go
167 lines (139 loc) · 4.98 KB
/
expirable_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
package lcw
import (
"fmt"
"sort"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExpirableCache(t *testing.T) {
lc, err := NewExpirableCache(MaxKeys(5), TTL(time.Millisecond*100))
require.NoError(t, err)
for i := 0; i < 5; i++ {
i := i
_, e := lc.Get(fmt.Sprintf("key-%d", i), func() (interface{}, error) {
return fmt.Sprintf("result-%d", i), nil
})
assert.NoError(t, e)
time.Sleep(10 * time.Millisecond)
}
assert.Equal(t, 5, lc.Stat().Keys)
assert.Equal(t, int64(5), lc.Stat().Misses)
keys := lc.Keys()
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
assert.EqualValues(t, []string{"key-0", "key-1", "key-2", "key-3", "key-4"}, keys)
_, e := lc.Get("key-xx", func() (interface{}, error) {
return "result-xx", nil
})
assert.NoError(t, e)
assert.Equal(t, 5, lc.Stat().Keys)
assert.Equal(t, int64(6), lc.Stat().Misses)
// let key-0 expire, GitHub Actions friendly way
for lc.Stat().Keys > 4 {
lc.backend.DeleteExpired() // enforce DeleteExpired for GitHub earlier than TTL/2
time.Sleep(time.Millisecond * 10)
}
assert.Equal(t, 4, lc.Stat().Keys)
time.Sleep(210 * time.Millisecond)
assert.Equal(t, 0, lc.keys())
assert.Equal(t, []string{}, lc.Keys())
assert.NoError(t, lc.Close())
}
func TestExpirableCache_MaxKeys(t *testing.T) {
var coldCalls int32
lc, err := NewExpirableCache(MaxKeys(5), MaxValSize(10))
require.NoError(t, err)
// put 5 keys to cache
for i := 0; i < 5; i++ {
i := i
res, e := lc.Get(fmt.Sprintf("key-%d", i), func() (interface{}, error) {
atomic.AddInt32(&coldCalls, 1)
return fmt.Sprintf("result-%d", i), nil
})
assert.NoError(t, e)
assert.Equal(t, fmt.Sprintf("result-%d", i), res.(string))
assert.Equal(t, int32(i+1), atomic.LoadInt32(&coldCalls))
}
// check if really cached
res, err := lc.Get("key-3", func() (interface{}, error) {
return "result-blah", nil
})
assert.NoError(t, err)
assert.Equal(t, "result-3", res.(string), "should be cached")
// try to cache after maxKeys reached
res, err = lc.Get("key-X", func() (interface{}, error) {
return "result-X", nil
})
assert.NoError(t, err)
assert.Equal(t, "result-X", res.(string))
assert.Equal(t, 5, lc.keys())
// put to cache and make sure it cached
res, err = lc.Get("key-Z", func() (interface{}, error) {
return "result-Z", nil
})
assert.NoError(t, err)
assert.Equal(t, "result-Z", res.(string))
res, err = lc.Get("key-Z", func() (interface{}, error) {
return "result-Zzzz", nil
})
assert.NoError(t, err)
assert.Equal(t, "result-Zzzz", res.(string), "got non-cached value")
assert.Equal(t, 5, lc.keys())
assert.NoError(t, lc.Close())
}
func TestExpirableCache_BadOptions(t *testing.T) {
_, err := NewExpirableCache(MaxCacheSize(-1))
assert.EqualError(t, err, "failed to set cache option: negative max cache size")
_, err = NewExpirableCache(MaxKeySize(-1))
assert.EqualError(t, err, "failed to set cache option: negative max key size")
_, err = NewExpirableCache(MaxKeys(-1))
assert.EqualError(t, err, "failed to set cache option: negative max keys")
_, err = NewExpirableCache(MaxValSize(-1))
assert.EqualError(t, err, "failed to set cache option: negative max value size")
_, err = NewExpirableCache(TTL(-1))
assert.EqualError(t, err, "failed to set cache option: negative ttl")
}
func TestExpirableCacheWithBus(t *testing.T) {
ps := &mockPubSub{}
lc1, err := NewExpirableCache(MaxKeys(5), TTL(time.Millisecond*100), EventBus(ps))
require.NoError(t, err)
defer lc1.Close()
lc2, err := NewExpirableCache(MaxKeys(50), TTL(time.Millisecond*5000), EventBus(ps))
require.NoError(t, err)
defer lc2.Close()
// add 5 keys to the first node cache
for i := 0; i < 5; i++ {
i := i
_, e := lc1.Get(fmt.Sprintf("key-%d", i), func() (interface{}, error) {
return fmt.Sprintf("result-%d", i), nil
})
assert.NoError(t, e)
time.Sleep(10 * time.Millisecond)
}
assert.Equal(t, 0, len(ps.CalledKeys()), "no events")
assert.Equal(t, 5, lc1.Stat().Keys)
assert.Equal(t, int64(5), lc1.Stat().Misses)
// add key-1 key to the second node
_, e := lc2.Get("key-1", func() (interface{}, error) {
return "result-111", nil
})
assert.NoError(t, e)
assert.Equal(t, 1, lc2.Stat().Keys)
assert.Equal(t, int64(1), lc2.Stat().Misses, lc2.Stat())
// let key-0 expire, GitHub Actions friendly way
for lc1.Stat().Keys > 4 {
lc1.backend.DeleteExpired() // enforce DeleteExpired for GitHub earlier than TTL/2
ps.Wait() // wait for onBusEvent goroutines to finish
time.Sleep(time.Millisecond * 10)
}
assert.Equal(t, 4, lc1.Stat().Keys)
assert.Equal(t, 1, lc2.Stat().Keys, "key-1 still in cache2")
assert.Equal(t, 1, len(ps.CalledKeys()))
time.Sleep(210 * time.Millisecond) // let all keys expire
ps.Wait() // wait for onBusEvent goroutines to finish
assert.Equal(t, 6, len(ps.CalledKeys()), "6 events, key-1 expired %+v", ps.calledKeys)
assert.Equal(t, 0, lc1.Stat().Keys)
assert.Equal(t, 0, lc2.Stat().Keys, "key-1 removed from cache2")
}