forked from C-Pro/geche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater_test.go
244 lines (194 loc) · 4.72 KB
/
updater_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
package geche
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"testing"
"time"
)
var errThe = errors.New("OOPS! Somebody has dropped the production database")
func updateFn(key string) (string, error) {
return key, nil
}
func updateErrFn(key string) (string, error) {
return "", errThe
}
func ExampleNewCacheUpdater() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create a cache updater with updaterFunction
// that sets cache value equal to the key.
u := NewCacheUpdater[string, string](
NewMapTTLCache[string, string](ctx, time.Minute, time.Minute),
updateFn,
2,
)
// Value is not in the cache yet.
// But it will be set by the updater function.
v, _ := u.Get("nonexistent")
fmt.Println(v)
// Output: nonexistent
}
func TestUpdaterScenario(t *testing.T) {
u := NewCacheUpdater[string, string](
NewMapCache[string, string](),
updateFn,
2,
)
if u.Len() != 0 {
t.Errorf("expected length to be 0, but got %d", u.Len())
}
v1, err := u.Get("test")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
v2, err := u.Get("test")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if v1 != "test" || v1 != v2 {
t.Errorf("expected both values to be %q, but got %q, %q", "test", v1, v2)
}
if u.Len() != 1 {
t.Errorf("expected length to be 1, but got %d", u.Len())
}
if err := u.Del("test"); err != nil {
t.Errorf("unexpected error in del: %v", err)
}
v, err := u.Get("test")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if v != "test" {
t.Errorf("expected to get %q, but got %q", "test", v)
}
u.Set("test", "best")
v3, err := u.Get("test")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if v3 != "best" {
t.Errorf("expected to get %q, but got %q", "best", v3)
}
s := u.Snapshot()
if len(s) != 1 {
t.Errorf("expected snapshot length to be 1, but got %d", len(s))
}
if s["test"] != "best" {
t.Errorf("expected to get %q, but got %q", "best", s["test"])
}
}
func TestUpdaterErr(t *testing.T) {
u := NewCacheUpdater[string, string](
NewMapCache[string, string](),
updateErrFn,
2,
)
_, err := u.Get("test")
if err != errThe {
t.Errorf("expected to get theError, but got %v", err)
}
u.Set("test", "test")
v, err := u.Get("test")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if v != "test" {
t.Errorf("expected to get %q, but got %q", "test", v)
}
}
func TestUpdaterConcurrent(t *testing.T) {
var (
mux sync.Mutex
currCnt int
peakCnt int
)
updateFn := func(key string) (string, error) {
mux.Lock()
currCnt++
if currCnt > peakCnt {
peakCnt = currCnt
}
mux.Unlock()
defer func() {
mux.Lock()
currCnt--
mux.Unlock()
}()
time.Sleep(time.Millisecond)
return key, nil
}
poolSize := 4
u := NewCacheUpdater[string, string](
NewMapCache[string, string](),
updateFn,
poolSize,
)
wg := sync.WaitGroup{}
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func() {
defer wg.Done()
v, err := u.Get("test")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if v != "test" {
t.Errorf("expected to get %q, but got %q", "test", v)
}
}()
}
wg.Wait()
if currCnt != 0 {
t.Errorf("expected currCnt to be 0, got %d", currCnt)
}
// We are using one key, so peakCnt s(hould be 1.
if peakCnt != 1 {
t.Errorf("expected peakCnt to be %d, got %d", 1, peakCnt)
}
wg = sync.WaitGroup{}
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func(i int) {
defer wg.Done()
key := strconv.Itoa(i % 100)
v, err := u.Get(key)
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if v != key {
t.Errorf("expected to get %q, but got %q", key, v)
}
}(i)
}
wg.Wait()
if currCnt != 0 {
t.Errorf("expected currCnt to be 0, got %d", currCnt)
}
// For 100 distinct keys peakCnt should be limited to poolSize.
if peakCnt != poolSize {
t.Errorf("expected peakCnt to be %d, got %d", poolSize, peakCnt)
}
}
func TestUpdaterListByPrefix(t *testing.T) {
imp := NewCacheUpdater[string, string](NewKV[string](NewMapCache[string, string]()), updateFn, 2)
imp.Set("test9", "test9")
imp.Set("test2", "test2")
imp.Set("test1", "test1")
imp.Set("test3", "test3")
_ = imp.Del("test2")
expected := []string{"test1", "test3", "test9"}
actual, err := imp.ListByPrefix("test")
if err != nil {
t.Errorf("unexpected error in ListByPrefix: %v", err)
}
compareSlice(t, expected, actual)
}
func TestUpdaterListByPrefixUnsupported(t *testing.T) {
imp := NewCacheUpdater[string, string](NewMapCache[string, string](), updateFn, 2)
if !panics(func() { _, _ = imp.ListByPrefix("test") }) {
t.Error("ListByPrefix expected to panic if underlying cache does not provide ListByPrefix")
}
}