-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_test.go
313 lines (253 loc) · 6.16 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package fixenv
import (
"math/rand"
"runtime"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
)
const waitTime = time.Second / 10
func TestCache_DeleteKeys(t *testing.T) {
t.Run("simple", func(t *testing.T) {
c := newCache()
k1 := cacheKey("k1")
k2 := cacheKey("k2")
k3 := cacheKey("k3")
val1 := "test1"
valFunc := func() (*Result, error) {
return NewResult(val1), nil
}
c.setOnce(k1, valFunc)
c.setOnce(k2, valFunc)
c.setOnce(k3, valFunc)
c.DeleteKeys(k1, k2)
_, ok := c.get(k1)
requireFalse(t, ok)
_, ok = c.get(k2)
requireFalse(t, ok)
res, ok := c.get(k3)
requireTrue(t, ok)
requireEquals(t, val1, res.res.Value.(string))
val2 := "test2"
c.setOnce(k1, func() (res *Result, err error) {
return NewResult(val2), nil
})
res, ok = c.get(k1)
requireTrue(t, ok)
requireEquals(t, val2, res.res.Value.(string))
})
t.Run("mutex", func(t *testing.T) {
c := newCache()
c.setOnce("asd", func() (res *Result, err error) {
return NewResult(nil), nil
})
c.m.RLock()
var wg sync.WaitGroup
wg.Add(1)
go func() {
c.DeleteKeys("asd")
wg.Done()
}()
time.Sleep(waitTime)
requireEquals(t, len(c.store), 1)
requireEquals(t, len(c.setLocks), 1)
c.m.RUnlock()
wg.Wait()
requireEquals(t, len(c.store), 0)
requireEquals(t, len(c.setLocks), 0)
})
}
func TestCache_Get(t *testing.T) {
t.Run("simple", func(t *testing.T) {
c := newCache()
_, ok := c.get("qwe")
requireFalse(t, ok)
c.store["asd"] = cacheVal{res: NewResult("val")}
res, ok := c.get("asd")
requireTrue(t, ok)
requireEquals(t, cacheVal{res: NewResult("val")}, res)
})
t.Run("read_mutex", func(t *testing.T) {
c := newCache()
c.setOnce("asd", func() (res *Result, err error) {
return NewResult(nil), nil
})
c.m.RLock()
_, ok := c.get("asd")
c.m.RUnlock()
requireTrue(t, ok)
})
t.Run("write_mutex", func(t *testing.T) {
c := newCache()
c.setOnce("asd", func() (res *Result, err error) {
return NewResult(nil), nil
})
c.m.Lock()
var ok bool
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, ok = c.get("asd")
wg.Done()
}()
time.Sleep(waitTime)
requireFalse(t, ok)
c.m.Unlock()
wg.Wait()
requireTrue(t, ok)
})
}
func TestCache_SetOnce(t *testing.T) {
t.Run("save_new_key", func(t *testing.T) {
c := newCache()
cnt := 0
key1 := cacheKey("1")
c.setOnce(key1, func() (res *Result, err error) {
cnt++
return NewResult(1), nil
})
requireEquals(t, 1, cnt)
requireEquals(t, 1, c.store[key1].res.Value)
noError(t, c.store[key1].err)
c.setOnce(key1, func() (res *Result, err error) {
cnt++
return NewResult(2), nil
})
requireEquals(t, 1, cnt)
requireEquals(t, 1, c.store[key1].res.Value)
noError(t, c.store[key1].err)
})
t.Run("second_set_val", func(t *testing.T) {
c := newCache()
key1 := cacheKey("1")
key2 := cacheKey("2")
cnt := 0
c.setOnce(key1, func() (res *Result, err error) {
cnt++
return NewResult(1), nil
})
c.setOnce(key2, func() (res *Result, err error) {
cnt++
return NewResult(2), nil
})
requireEquals(t, 2, cnt)
requireEquals(t, 1, c.store[key1].res.Value)
noError(t, c.store[key1].err)
requireEquals(t, 2, c.store[key2].res.Value)
noError(t, c.store[key2].err)
})
// exit without return value
t.Run("exit_without_return", func(t *testing.T) {
c := newCache()
key := cacheKey("3")
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
c.setOnce(key, func() (res *Result, err error) {
runtime.Goexit()
return NewResult(3), nil
})
}()
wg.Wait()
requireNil(t, c.store[key].res)
isError(t, c.store[key].err)
})
t.Run("second_func_same_key_wait", func(t *testing.T) {
c := newCache()
key := cacheKey("1")
var firstMuStarted = make(chan bool)
var firstMuNeedFinish = make(chan bool)
var firstMuFinished = make(chan bool)
// first
go func() {
c.setOnce(key, func() (res *Result, err error) {
close(firstMuStarted)
<-firstMuNeedFinish
return NewResult(1), nil
})
close(firstMuFinished)
}()
// wait first func start execute
<-firstMuStarted
var secondFinished = make(chan bool)
var doneSecond int64
go func() {
c.setOnce(key, func() (res *Result, err error) {
// func will not call never
return NewResult(2), nil
})
// must executed only after fist func finished
atomic.AddInt64(&doneSecond, 1)
close(secondFinished)
}()
time.Sleep(waitTime)
// second not work until first finished
doneSecondVal := atomic.LoadInt64(&doneSecond)
_, ok := c.store[key]
requireFalse(t, ok)
requireEquals(t, int64(0), doneSecondVal)
close(firstMuNeedFinish)
// wait first func finished
<-firstMuFinished
// wait second finished
<-secondFinished
doneSecondVal = atomic.LoadInt64(&doneSecond)
requireEquals(t, 1, c.store[key].res.Value)
requireEquals(t, int64(1), doneSecondVal)
})
t.Run("second_func_other_key_work", func(t *testing.T) {
c := newCache()
key1 := cacheKey("1")
key2 := cacheKey("2")
var firstMuStarted = make(chan bool)
var firstMuNeedFinish = make(chan bool)
var firstMuFinished = make(chan bool)
// first
go func() {
c.setOnce(key1, func() (res *Result, err error) {
close(firstMuStarted)
<-firstMuNeedFinish
return NewResult(1), nil
})
close(firstMuFinished)
}()
// wait first func start execute
<-firstMuStarted
// call second func in same goroutine
c.setOnce(key2, func() (res *Result, err error) {
// func will not call never
return NewResult(2), nil
})
// allow finish first func after second already finished
close(firstMuNeedFinish)
// wait first func finished
<-firstMuFinished
requireEquals(t, 1, c.store[key1].res.Value)
requireEquals(t, 2, c.store[key2].res.Value)
})
}
func TestCache_GetOrSetRaceCondition(_ *testing.T) {
parallels := 100
iterations := 1000
rndMaxBound := 1000
c := newCache()
var wg sync.WaitGroup
for i := 0; i < parallels; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
key := cacheKey(strconv.Itoa(rand.Intn(rndMaxBound)))
v, ok := c.GetOrSet(key, func() (res *Result, err error) {
return NewResult(1), nil
})
_ = v
_ = ok
}
}()
}
wg.Wait()
}