-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadcache_test.go
More file actions
240 lines (219 loc) · 5.94 KB
/
readcache_test.go
File metadata and controls
240 lines (219 loc) · 5.94 KB
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
package readcache
import (
"errors"
"fmt"
"sync"
"testing"
"time"
)
func TestGet_Once_WithNilValue_ShouldReturnNil(t *testing.T) {
cache := New(newGetter(nil, 100e9))
cache.Get("key")
}
func TestGet_Once_WithSomeValue_ShouldReturnValue(t *testing.T) {
cache := New(newGetter("foo", 100e9))
result, _ := cache.Get("key")
if result == nil {
t.Error("Something should have been returned.")
}
if result.(string) != "foo" {
t.Error("Did not get the expected value.")
}
}
func TestGet_Twice_WithSomeValue_ShouldReturnValue(t *testing.T) {
cache := New(newGetter("foo", 100e9))
cache.Get("key")
result, _ := cache.Get("key")
if result == nil {
t.Error("Something should have been returned.")
}
if result.(string) != "foo" {
t.Error("Did not get the expected value.")
}
}
func TestGet_Twice_WithSomeValue_ShouldNotFetchTwice(t *testing.T) {
fetchCount := 0
getter := func(key string) (interface{}, time.Time, error) {
fetchCount++
return "foo", time.Now().Add(100e9), nil
}
cache := New(getter)
cache.Get("key")
cache.Get("key")
if fetchCount != 1 {
t.Errorf("Should have only fetched once, but got %d", fetchCount)
}
}
func TestGet_Twice_WithExpiration_ShouldFetchTwice(t *testing.T) {
expiresAt := time.Now().Add(-1)
fetchCount := 0
getter := func(key string) (interface{}, time.Time, error) {
fetchCount++
return "foo", expiresAt, nil
}
cache := New(getter)
cache.Get("key")
cache.Get("key")
if fetchCount != 2 {
t.Errorf("Should have fetched twice, but got %d", fetchCount)
}
}
func TestGet_ConcurrentReads_WithLongExpiration_ShouldFetchOncePerKey(t *testing.T) {
fetchLock := new(sync.Mutex)
fetchCount := 0
getter := func(key string) (interface{}, time.Time, error) {
fetchLock.Lock()
fetchCount++
fetchLock.Unlock()
return "foo", time.Now().Add(100e9), nil
}
cache := New(getter)
numKeys := 32
runConcurrencyTest(cache, numKeys, numKeys)
if fetchCount != numKeys {
t.Errorf("Should have fetched %d times, but got %d", numKeys, fetchCount)
}
}
func TestGet_ConcurrentReads_StartingWithExpiredItems_ShouldFetchOncePerKey(t *testing.T) {
fetchLock := new(sync.Mutex)
prime := true
expiresAt := time.Now().Add(-1)
fetchCount := 0
getter := func(key string) (interface{}, time.Time, error) {
if prime {
return "foo", expiresAt, nil
}
fetchLock.Lock()
fetchCount++
fetchLock.Unlock()
return "foo", time.Now().Add(100e9), nil
}
cache := New(getter)
numKeys := 32
for i := 0; i < numKeys; i++ {
cache.Get(fmt.Sprintf("%d", i))
}
prime = false
runConcurrencyTest(cache, numKeys, numKeys)
if fetchCount != numKeys {
t.Errorf("Should have fetched %d times, but got %d", numKeys, fetchCount)
}
}
func TestGet_ErrorInGetter_ShouldReturnError(t *testing.T) {
getter := func(key string) (interface{}, time.Time, error) {
return nil, time.Now(), errors.New("Error message")
}
cache := New(getter)
_, err := cache.Get("key")
if err == nil {
t.Error("An error should have been returned")
} else if err.Error() != "Error message" {
t.Errorf("Expected 'Error message' but got '%s'", err.Error())
}
}
func TestGet_ErrorInGetter_ConcurrentReads_ShouldReturnError(t *testing.T) {
getter := func(key string) (interface{}, time.Time, error) {
return nil, time.Now(), errors.New("Error message")
}
cache := New(getter)
quit := make(chan bool)
for r := 0; r < 32; r++ {
seed := r
go func() {
for i := 0; i < 2048; i++ {
keyNum := ((i + 1) * seed) % 512
key := fmt.Sprintf("%d", keyNum)
_, err := cache.Get(key)
if err == nil {
t.Error("Get did not return an error")
} else if err.Error() != "Error message" {
t.Errorf("Unexpected error result: %s", err.Error())
}
}
quit <- true
}()
}
for r := 0; r < 32; r++ {
<-quit
}
}
func TestGet_WithPurgeRules_ShouldPurgeOldEntries(t *testing.T) {
fetchCount := 0
getter := func(key string) (interface{}, time.Time, error) {
fetchCount++
return "foo", time.Now().Add(100e9), nil
}
cache := New(getter)
cache.SetPurgeAt(3)
cache.SetPurgeTo(1)
cache.Get("1")
cache.Get("2")
cache.Get("1")
cache.Get("2")
if fetchCount != 2 {
t.Errorf("Expected fetchCount = 2 but was %d", fetchCount)
}
cache.Get("3") // {1, 2, 3} -> Purge -> {3}
cache.Get("3")
if fetchCount != 3 {
t.Errorf("Expected fetchCount = 3 but was %d", fetchCount)
}
cache.Get("1")
cache.Get("2") // {1, 2, 3} -> Purge -> {2}
if fetchCount != 5 {
t.Errorf("Expected fetchCount = 5 but was %d", fetchCount)
}
cache.Get("3")
if fetchCount != 6 {
t.Errorf("Expected fetchCount = 6 but was %d", fetchCount)
}
cache.Get("2")
if fetchCount != 6 {
t.Errorf("Expected fetchCount = 6 but was %d", fetchCount)
}
}
func BenchmarkGet_Concurrent_Performance(t *testing.B) {
getter := func(key string) (interface{}, time.Time, error) {
return "foo", time.Now().Add(100e9), nil
}
cache := New(getter)
runConcurrencyTest(cache, 8, t.N)
}
func BenchmarkGet_Concurrent_Purge_Performance(t *testing.B) {
getter := func(key string) (interface{}, time.Time, error) {
return "foo", time.Now().Add(100e9), nil
}
cache := New(getter)
cache.SetPurgeAt(200)
cache.SetPurgeTo(100)
runConcurrencyTest(cache, 8, t.N)
}
func runConcurrencyTest(cache Cache, numGoroutines int, numFetches int) {
numFetchesPerGoroutine := numFetches / numGoroutines
remainingFetches := numFetches % numGoroutines
numKeys := numFetches
quit := make(chan bool)
for r := 0; r < numGoroutines; r++ {
seed := r
go func() {
var numFetchesForThis = numFetchesPerGoroutine
if r == (numGoroutines - 1) {
numFetchesForThis += remainingFetches
}
for i := 0; i < numFetchesForThis; i++ {
keyNum := ((i + 1) * seed) % numKeys
key := fmt.Sprintf("%d", keyNum)
cache.Get(key)
}
quit <- true
}()
}
for r := 0; r < numGoroutines; r++ {
<-quit
}
}
func newGetter(item interface{}, expirationDelta time.Duration) func(string) (interface{}, time.Time, error) {
return func(key string) (interface{}, time.Time, error) {
return item, time.Now().Add(expirationDelta), nil
}
}