-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleflight_test.go
345 lines (297 loc) · 7.54 KB
/
singleflight_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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package ggcache
import (
"context"
"errors"
"sync"
"testing"
"time"
)
func TestGroup_Do_SingleCall(t *testing.T) {
g := NewGroup[string, int]()
calls := 0
loader := func(key string, ctx context.Context) (int, error) {
calls++
return 42, nil
}
val, called, err := g.Do("key1", loader, context.Background())
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !called {
t.Error("Expected loader to be called")
}
if val != 42 {
t.Errorf("Expected value 42, got %d", val)
}
if calls != 1 {
t.Errorf("Expected 1 call, got %d", calls)
}
}
func TestGroup_Do_ConcurrentCalls(t *testing.T) {
g := NewGroup[string, int]()
var mu sync.Mutex
calls := 0
loader := func(key string, ctx context.Context) (int, error) {
mu.Lock()
calls++
mu.Unlock()
time.Sleep(100 * time.Millisecond)
return 42, nil
}
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
val, _, err := g.Do("key1", loader, context.Background())
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if val != 42 {
t.Errorf("Expected value 42, got %d", val)
}
}()
}
wg.Wait()
if calls != 1 {
t.Errorf("Expected 1 call, got %d", calls)
}
}
func TestGroup_Do_Error(t *testing.T) {
g := NewGroup[string, int]()
loader := func(key string, ctx context.Context) (int, error) {
return 0, errors.New("test error")
}
val, called, err := g.Do("key1", loader, context.Background())
if err == nil || err.Error() != "test error" {
t.Errorf("Expected 'test error', got %v", err)
}
if !called {
t.Error("Expected loader to be called")
}
if val != 0 {
t.Errorf("Expected value 0, got %d", val)
}
}
func TestGroup_Do_ContextCancellation(t *testing.T) {
g := NewGroup[string, int]()
loader := func(key string, ctx context.Context) (int, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
case <-time.After(200 * time.Millisecond):
}
return 42, nil
}
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
val, called, err := g.Do("key1", loader, ctx)
if err != context.DeadlineExceeded {
t.Errorf("Expected DeadlineExceeded error, got %v", err)
}
if !called {
t.Error("Expected loader to be called")
}
if val != 0 {
t.Errorf("Expected value 0, got %d", val)
}
}
func TestGroup_Do_MultipleKeys(t *testing.T) {
g := NewGroup[string, int]()
calls := make(map[string]int)
var mu sync.Mutex
loader := func(key string, ctx context.Context) (int, error) {
mu.Lock()
calls[key]++
mu.Unlock()
time.Sleep(100 * time.Millisecond)
return len(key), nil
}
var wg sync.WaitGroup
for _, key := range []string{"a", "bb", "ccc", "a", "bb", "ccc"} {
wg.Add(1)
go func(k string) {
defer wg.Done()
val, _, err := g.Do(k, loader, context.Background())
if err != nil {
t.Errorf("Unexpected error for key %s: %v", k, err)
}
if val != len(k) {
t.Errorf("Expected value %d for key %s, got %d", len(k), k, val)
}
}(key)
}
wg.Wait()
for key, count := range calls {
if count != 1 {
t.Errorf("Expected 1 call for key %s, got %d", key, count)
}
}
}
func TestGroup_Do_LargeNumberOfCalls(t *testing.T) {
g := NewGroup[int, int]()
calls := 0
var mu sync.Mutex
loader := func(key int, ctx context.Context) (int, error) {
mu.Lock()
calls++
mu.Unlock()
time.Sleep(100 * time.Millisecond)
return key * 2, nil
}
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(k int) {
defer wg.Done()
val, _, err := g.Do(k%10, loader, context.Background())
if err != nil {
t.Errorf("Unexpected error for key %d: %v", k, err)
}
if val != (k%10)*2 {
t.Errorf("Expected value %d for key %d, got %d", (k%10)*2, k, val)
}
}(i)
}
wg.Wait()
if calls != 10 {
t.Errorf("Expected 10 calls, got %d", calls)
}
}
// Test basic functionality: single call
func TestGroup_Do_SingleCall2(t *testing.T) {
g := NewGroup[string, string]()
loader := func(key string, ctx context.Context) (string, error) {
return "value", nil
}
val, loaderCalled, err := g.Do("key", loader, context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if val != "value" {
t.Fatalf("expected value 'value', got %v", val)
}
if !loaderCalled {
t.Fatalf("expected loader to be called")
}
}
// Test concurrent calls with the same key
func TestGroup_Do_ConcurrentCalls2(t *testing.T) {
g := NewGroup[string, string]()
loader := func(key string, ctx context.Context) (string, error) {
time.Sleep(100 * time.Millisecond)
return "value", nil
}
var wg sync.WaitGroup
const n = 3000
results := make([]string, n)
loaderCalled := make([]bool, n)
errors := make([]error, n)
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
results[i], loaderCalled[i], errors[i] = g.Do("key", loader, context.Background())
}(i)
}
wg.Wait()
for i := 0; i < n; i++ {
if errors[i] != nil {
t.Fatalf("unexpected error: %v", errors[i])
}
if results[i] != "value" {
t.Fatalf("expected value 'value', got %v", results[i])
}
}
// Only one call should have actually called the loader
loaderCallCount := 0
for _, called := range loaderCalled {
if called {
loaderCallCount++
}
}
if loaderCallCount != 1 {
t.Fatalf("expected loader to be called once, but was called %d times", loaderCallCount)
}
}
// Test concurrent calls with the same key
func TestGroup_Do_ConcurrentCalls3(t *testing.T) {
g := NewGroup[string, string]()
loader := func(key string, ctx context.Context) (string, error) {
time.Sleep(100 * time.Millisecond)
return "value", nil
}
var wg sync.WaitGroup
const n = 2000
results := make([]string, n)
loaderCalled := make([]bool, n)
errors := make([]error, n)
startSignal := make(chan struct{})
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
<-startSignal
results[i], loaderCalled[i], errors[i] = g.Do("key", loader, context.Background())
}(i)
}
close(startSignal)
wg.Wait()
for i := 0; i < n; i++ {
if errors[i] != nil {
t.Fatalf("unexpected error: %v", errors[i])
}
if results[i] != "value" {
t.Fatalf("expected value 'value', got %v", results[i])
}
}
// Only one call should have actually called the loader
loaderCallCount := 0
for _, called := range loaderCalled {
if called {
loaderCallCount++
}
}
if loaderCallCount != 1 {
t.Fatalf("expected loader to be called once, but was called %d times", loaderCallCount)
}
}
// Test error handling
func TestGroup_Do_Error2(t *testing.T) {
g := NewGroup[string, string]()
expectedErr := errors.New("loader error")
loader := func(key string, ctx context.Context) (string, error) {
return "", expectedErr
}
_, _, err := g.Do("key", loader, context.Background())
if err != expectedErr {
t.Fatalf("expected error %v, got %v", expectedErr, err)
}
}
func TestGroup_Do_PanicHandling(t *testing.T) {
group := NewGroup[string, int]()
loader := func(key string, ctx context.Context) (int, error) {
panic("simulated panic")
}
_, loaderCalled, err := group.Do("key1", loader, context.Background())
if !loaderCalled {
t.Errorf("expected loader to be called")
}
if err == nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestGroup_Do_ErrorHandling(t *testing.T) {
group := NewGroup[string, int]()
expectedErr := errors.New("simulated error")
loader := func(key string, ctx context.Context) (int, error) {
return 0, expectedErr
}
_, loaderCalled, err := group.Do("key1", loader, context.Background())
if !loaderCalled {
t.Errorf("expected loader to be called")
}
if err == nil || err != expectedErr {
t.Errorf("unexpected error: %v", err)
}
}