-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
211 lines (198 loc) · 6.23 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
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
package gocachelib
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestRevoke(t *testing.T) {
// Run refresh & revoke loops quicker than usual
defaultLoopInterval := loopInterval
defer func() {
stop()
loopInterval = defaultLoopInterval
}()
loopInterval = 10 * time.Millisecond
StartWith(1, 1, 1, 15*time.Millisecond)
key := "TestRevoke"
i := CacheItem{
Key: key,
Value: []byte("TestRevoke"),
Expiration: 10 * time.Millisecond,
GetFunc: noopGetFunc,
}
AddItem(i)
assert.True(t, string(GetValue(key)) == "TestRevoke")
time.Sleep(25 * time.Millisecond)
assert.True(t, nil == GetValue(key), "Item should have been revoked by now")
}
func TestTTLLessThanExpiration(t *testing.T) {
// Run refresh & revoke loops quicker than usual
defaultLoopInterval := loopInterval
defer func() {
stop()
loopInterval = defaultLoopInterval
}()
loopInterval = 10 * time.Millisecond
StartWith(1, 1, 1, 10*time.Millisecond)
key := "TestTTLLessThanExpiration"
i := CacheItem{
Key: key,
Value: []byte("TestTTLLessThanExpiration"),
TTL: 10 * time.Millisecond,
Expiration: 20 * time.Millisecond,
GetFunc: noopGetFunc,
}
AddItem(i)
time.Sleep(15 * time.Millisecond)
assert.True(t, string(GetValue(key)) == "TestTTLLessThanExpiration", "Item should still be in cache after the first revocation loop was run")
}
func TestGetValuePostponesRevoke(t *testing.T) {
StartWith(1, 1, 1, 1*time.Second)
defer stop()
key := "TestGetValuePostponesRevoke"
i := CacheItem{
Key: key,
Value: []byte("TestGetValuePostponesRevoke"),
TTL: 10 * time.Millisecond,
Expiration: 10 * time.Millisecond,
GetFunc: noopGetFunc,
}
now := time.Now()
AddItem(i)
item, ok := cache.Get(key)
if !ok {
t.Errorf("Should have got item %s from cache", key)
}
revokeAfterAdd := item.(timedCacheItem).RevokeTime
assert.True(t, now.Before(revokeAfterAdd))
assert.True(t, string(GetValue(key)) == "TestGetValuePostponesRevoke", "Item should be in cache")
time.Sleep(5 * time.Millisecond)
item, ok = cache.Get(key)
if !ok {
t.Errorf("Should have got item %s from cache after first GetValue", key)
}
revokeAfterGet := item.(timedCacheItem).RevokeTime
assert.True(t, revokeAfterAdd.Before(revokeAfterGet), "Revoke time should be postponed after GetValue: %v < %v", revokeAfterAdd, revokeAfterGet)
}
func TestExpire(t *testing.T) {
// Run refresh & revoke loops quicker than usual
defaultLoopInterval := loopInterval
defer func() {
stop()
loopInterval = defaultLoopInterval
}()
loopInterval = 10 * time.Millisecond
StartWith(1, 11, 1, 1*time.Second)
key := "TestExpire"
value := randomGetFunc("")
i := CacheItem{
Key: key,
Value: value,
Expiration: 10 * time.Millisecond,
GetFunc: randomGetFunc,
}
AddItem(i)
time.Sleep(20 * time.Millisecond)
assert.NotEqual(t, string(value), string(GetValue(key)), "Item should have new value in cache")
}
func TestItemWithShortestTTLIsRevokedWhenCacheFillsUp(t *testing.T) {
StartWith(1, 11, 2, 1*time.Second)
defer stop()
AddItem(CacheItem{
Key: "TestItemWithShortestTTLIsRevokedWhenCacheFillsUp1",
Value: []byte("TestItemWithShortestTTLIsRevokedWhenCacheFillsUp1"),
Expiration: 1 * time.Second,
TTL: 2 * time.Second,
GetFunc: noopGetFunc,
})
AddItem(CacheItem{
Key: "TestItemWithShortestTTLIsRevokedWhenCacheFillsUp2",
Value: []byte("TestItemWithShortestTTLIsRevokedWhenCacheFillsUp2"),
Expiration: 1 * time.Second,
TTL: 1 * time.Second,
GetFunc: noopGetFunc,
})
AddItem(CacheItem{
Key: "TestItemWithShortestTTLIsRevokedWhenCacheFillsUp3",
Value: []byte("TestItemWithShortestTTLIsRevokedWhenCacheFillsUp3"),
Expiration: 1 * time.Second,
TTL: 2 * time.Second,
GetFunc: noopGetFunc,
})
assert.Equal(t, "TestItemWithShortestTTLIsRevokedWhenCacheFillsUp1", string(GetValue("TestItemWithShortestTTLIsRevokedWhenCacheFillsUp1")))
assert.Equal(t, "TestItemWithShortestTTLIsRevokedWhenCacheFillsUp3", string(GetValue("TestItemWithShortestTTLIsRevokedWhenCacheFillsUp3")))
for k, _ := range cache.Items() {
assert.NotEqual(t, "TestItemWithShortestTTLIsRevokedWhenCacheFillsUp2", k, "Item #2 should have been revoked when cache was full")
}
}
func TestConcurrentRefreshAndGetValueBug(t *testing.T) {
// Run refresh & revoke loops quicker than usual
defaultLoopInterval := loopInterval
defer func() {
stop()
loopInterval = defaultLoopInterval
}()
loopInterval = 10 * time.Millisecond
// Make sure revoke does not interfere here
StartWith(1, 11, 1, 5*time.Second)
// continuously spamming GetValue should manifest the bug
c := make(chan []byte, 1)
key := "TestConcurrentRefreshAndGetValueBug"
go busyGet(t, c, key)
value := randomGetFunc("")
i := CacheItem{
Key: key,
Value: value,
Expiration: 10 * time.Millisecond,
GetFunc: randomGetFunc,
}
AddItem(i)
// sleep long enough for the bug to kick in repeatably
time.Sleep(1013 * time.Millisecond)
c <- []byte("stop")
v, _ := cache.Get(key)
ci := v.(timedCacheItem)
assert.NotEqual(t, ci.Updating, true, "Item Should not be in updating state")
}
func TestConcurrentRevokeAndGetValueBug(t *testing.T) {
// Run refresh & revoke loops quicker than usual
defaultLoopInterval := loopInterval
defer func() {
stop()
loopInterval = defaultLoopInterval
}()
loopInterval = 10 * time.Millisecond
StartWith(1, 11, 1, 20*time.Millisecond)
// Continuously spamming GetValue should force ConcurrentRefreshAndGetBug to manifest if it is present
c := make(chan []byte, 1)
key := "TestConcurrentRevokeAndGetValueBug"
go busyGet(t, c, key)
value := randomGetFunc("")
i := CacheItem{
Key: key,
Value: value,
Expiration: 15 * time.Millisecond,
GetFunc: randomGetFunc,
}
AddItem(i)
// sleep long enough for the bug to kick in repeatably
time.Sleep(1 * time.Second)
c <- []byte("stop")
time.Sleep(53 * time.Millisecond)
assert.True(t, nil == GetValue(key), "Item should have been revoked by now")
}
func noopGetFunc(s string) []byte {
return nil
}
func randomGetFunc(s string) []byte {
return []byte(uuid.New().String())
}
func busyGet(t *testing.T, c chan []byte, k string) {
defer func() {
_ = <-c
}()
for len(c) == 0 {
_ = GetValue(k)
}
}