forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_test.go
302 lines (269 loc) · 6.76 KB
/
batch_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
package batch_test
import (
"context"
"errors"
"strings"
"sync"
"testing"
"time"
"github.com/denkhaus/thunder/batch"
)
// TestBasic tests that batch.Func with default options batches calls.
func TestBasic(t *testing.T) {
var mu sync.Mutex
calls := 0
f := (&batch.Func{
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
mu.Lock()
defer mu.Unlock()
calls++
return args, nil
},
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if result, err := f(ctx, i); err != nil || result != i {
t.Error(err, i)
}
}(i)
}
wg.Wait()
// Expect 1, allow for 2 in case of races.
if calls > 2 {
t.Error(calls)
}
}
// TestWaitInterval tests that the function invocation is delayed while we
// consistently invoke the batch function.
func TestWaitInterval(t *testing.T) {
const loopCount = 20
const sleepDuration = 10 * time.Millisecond
testcases := []struct {
description string
interval time.Duration
maxDuration time.Duration
sleepDuration time.Duration
expectedCount int
}{
{
description: "Expect sleep less than WaitDuration to result in a single call being made.",
interval: 2 * sleepDuration,
sleepDuration: sleepDuration,
maxDuration: 500 * sleepDuration,
expectedCount: 1,
},
{
description: "Expect sleep greater than WaitDuration to result in loopCount calls being made.",
interval: sleepDuration / 2,
sleepDuration: sleepDuration,
maxDuration: 500 * sleepDuration,
expectedCount: loopCount,
},
{
description: "Expect sleep less than than WaitDuration but aggregate over MaxDuration to result in 2 calls being made.",
interval: 2 * sleepDuration,
sleepDuration: sleepDuration,
maxDuration: (loopCount - 1) * sleepDuration,
expectedCount: 2,
},
}
for i, testcase := range testcases {
var mu sync.Mutex
count := 0
f := (&batch.Func{
WaitInterval: testcase.interval,
MaxDuration: testcase.maxDuration,
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
mu.Lock()
defer mu.Unlock()
count++
return args, nil
},
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < loopCount; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if result, err := f(ctx, i); err != nil || result != i {
t.Error(err, i)
}
}(i)
time.Sleep(testcase.sleepDuration)
}
wg.Wait()
if count != testcase.expectedCount {
t.Errorf("Test %d: %s: Expected=%v, Actual=%v", i, testcase.description, testcase.expectedCount, count)
}
}
}
// TestBackToBack tests that two back-to-back invocations of batch.Func from
// multiple goroutines get batched in a total of two calls.
func TestBackToBack(t *testing.T) {
var mu sync.Mutex
calls := 0
f := (&batch.Func{
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
mu.Lock()
defer mu.Unlock()
calls++
return args, nil
},
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if result, err := f(ctx, i); err != nil || result != i {
t.Error(err, i)
}
if result, err := f(ctx, i); err != nil || result != i {
t.Error(err, i)
}
}(i)
}
wg.Wait()
// Expect 2, allow for 3 in case of races.
if calls > 3 {
t.Error(calls)
}
}
// TestShard tests that Func.Shard shards invocations according to the shard.
func TestShard(t *testing.T) {
var mu sync.Mutex
calls := 0
f := (&batch.Func{
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
mu.Lock()
defer mu.Unlock()
for _, i := range args {
if i.(int)%3 != args[0].(int)%3 {
return nil, errors.New("bad shard")
}
}
calls++
return args, nil
},
Shard: func(arg interface{}) interface{} {
return arg.(int) % 3
},
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if result, err := f(ctx, i); err != nil || result != i {
t.Error(err, i)
}
}(i)
}
wg.Wait()
// Expect 3 calls, one for each shard, allow for 4 in case of races.
if calls > 4 {
t.Error(calls)
}
}
// TestMaxSize tests that no more than Func.MaxSize arguments get batched
// together.
func TestMaxSize(t *testing.T) {
var mu sync.Mutex
calls := 0
f := (&batch.Func{
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
mu.Lock()
defer mu.Unlock()
if len(args) > 5 {
return nil, errors.New("too many")
}
calls++
return args, nil
},
MaxSize: 5,
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if result, err := f(ctx, i); err != nil || result != i {
t.Error(err, i)
}
}(i)
}
wg.Wait()
// Expect 4 calls, one for each shard, allow for 5 in case of races.
if calls > 5 {
t.Error(calls)
}
}
// TestIncorrectNumberOfResults tests that a batch function that returns the
// wrong number of results is handled gracefully.
func TestIncorrectNumberOfResults(t *testing.T) {
f := (&batch.Func{
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
return append(args, nil), nil
},
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if _, err := f(ctx, i); err == nil || !strings.Contains(err.Error(), "incorrect number of results") {
t.Error(err, i)
}
}(i)
}
wg.Wait()
}
// TestPanic tests that a batch function that panics is handled gracefully.
func TestPanic(t *testing.T) {
f := (&batch.Func{
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
panic("foo")
},
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if _, err := f(ctx, i); err == nil || !strings.Contains(err.Error(), "panicked: foo") {
t.Error(err, i)
}
}(i)
}
wg.Wait()
}
// TestError tests that a batch function that returns an error is handled correctly.
func TestError(t *testing.T) {
f := (&batch.Func{
Many: func(ctx context.Context, args []interface{}) ([]interface{}, error) {
return nil, errors.New("some error")
},
}).Invoke
ctx := batch.WithBatching(context.Background())
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if _, err := f(ctx, i); err == nil || !strings.Contains(err.Error(), "some error") {
t.Error(err, i)
}
}(i)
}
wg.Wait()
}