-
Notifications
You must be signed in to change notification settings - Fork 1
/
workerpool_test.go
350 lines (286 loc) · 6.47 KB
/
workerpool_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
346
347
348
349
350
package workerpool
import (
"fmt"
"math/rand"
"testing"
"time"
)
type Job struct {
}
const e = 40
func (j *Job) execute(p interface{}) (interface{}, error) {
payload := p.(int)
f := func(p int) (int, error) {
time.Sleep(100 * time.Millisecond)
return p * 2, nil
}
return f(payload)
}
func (j *Job) slow(p interface{}) (interface{}, error) {
time.Sleep(3 * time.Second)
return nil, nil
}
func (j *Job) unreliable(p interface{}) (interface{}, error) {
n := rand.Intn(100)
if n%2 == 0 {
return nil, fmt.Errorf("fail hehe")
}
return nil, nil
}
func TestWorkerPool(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
job := &Job{}
wp, err := New(job.execute,
WithMaxWorker(10),
WithEvaluationTime(1),
)
if err != nil {
t.Errorf(err.Error())
}
for i := 0; i < 1000; i++ {
wp.Feed(i)
}
// wait for completion
wp.Wait()
// Wait again, should not deadlock
wp.Wait()
// stop workerpool
wp.Stop()
// check velocity
var previous float64
v := wp.VelocityValues()
t.Logf("Velocity:\n")
for i := 1; i <= 100; i++ {
velocity, ok := v[i]
if !ok {
continue
}
t.Logf("%d%% -> %f op/s (avg: %.2fms)", i, velocity.Ops, velocity.Avg)
if velocity.Ops < previous {
t.Errorf("Expected velocity to increase steadily, got %f with previous %f", velocity.Ops, previous)
}
previous = velocity.Ops
if velocity.Avg < 100-e || velocity.Avg > 100+e {
t.Errorf("Expected average to be 100ms got %.2fms", velocity.Avg)
}
}
// check current velocity
percentil, ops, avg := wp.CurrentVelocityValues()
t.Logf("Current velocity: %d%% -> %f op/s (avg: %.2fms)\n", percentil, ops, avg)
if percentil != 100 {
t.Errorf("Expected use of full size, got %d%%", percentil)
}
if ops < 100-e || ops > 100+e {
t.Errorf("Expected around 100 op/s with 10 worker doing 10 op/s each, got %f", ops)
}
}
func TestResponses(t *testing.T) {
taskcount := 15
job := &Job{}
wp, err := New(job.execute,
WithMaxWorker(10),
WithEvaluationTime(1),
)
if err != nil {
t.Errorf(err.Error())
}
for i := 0; i < taskcount; i++ {
wp.Feed(i)
}
wp.Wait()
n := wp.AvailableResponses()
fmt.Printf("Available responses: %d\n", n)
if n != taskcount {
t.Errorf("Expected %d responses, got %d", taskcount, n)
}
wp.Stop()
// read all responses
var count int
for r := range wp.ReturnChannel {
count++
if r.Err != nil {
t.Errorf("Expected all errors to be nil")
}
}
if count != taskcount {
t.Errorf("Expected response count to be %d, got %d", taskcount, count)
}
n = wp.AvailableResponses()
fmt.Printf("Available responses: %d\n", n)
if n != 0 {
t.Errorf("Expected 0 responses, got %d", n)
}
}
func TestAllIn(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
job := &Job{}
wp, err := New(job.execute,
WithMaxWorker(10),
WithEvaluationTime(1),
WithSizePercentil(AllInSizesPercentil),
)
if err != nil {
t.Errorf(err.Error())
}
for i := 0; i < 1000; i++ {
wp.Feed(i)
}
// wait for completion
wp.Wait()
// stop workerpool
wp.Stop()
// check current velocity
percentil, ops, avg := wp.CurrentVelocityValues()
t.Logf("Current velocity: %d%% -> %f op/s (avg: %.2fms)\n", percentil, ops, avg)
if percentil != 100 {
t.Errorf("Expected use of full size, got %d%%", percentil)
}
if ops < 100-e || ops > 100+e {
t.Errorf("Expected around 100 op/s with 10 worker doing 10 op/s each, got %f", ops)
}
}
func TestSlow(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
job := &Job{}
wp, err := New(job.slow,
WithMaxWorker(100),
WithEvaluationTime(10),
WithSizePercentil(AllSizesPercentil),
)
if err != nil {
t.Errorf(err.Error())
}
for i := 0; i < 40; i++ {
wp.Feed(i)
}
// wait for completion
wp.Wait()
// stop workerpool
wp.Stop()
// check velocity
var previous float64
v := wp.VelocityValues()
t.Logf("Velocity:\n")
for i := 1; i <= 100; i++ {
velocity, ok := v[i]
if !ok {
continue
}
t.Logf("%d%% -> %f op/s (avg: %.2fms)", i, velocity.Ops, velocity.Avg)
if velocity.Ops < previous {
t.Errorf("Expected velocity to increase steadily, got %f with previous %f", velocity.Ops, previous)
}
previous = velocity.Ops
if velocity.Avg < 3000-e || velocity.Avg > 3000+e {
t.Errorf("Expected average to be 3000ms got %.2fms", velocity.Avg)
}
}
}
/*
** Test retry feature with unreliable job (ie: network)
*/
func TestRetry(t *testing.T) {
E := 30
job := &Job{}
wp, err := New(job.unreliable,
WithRetry(0),
WithMaxWorker(100),
WithEvaluationTime(1),
WithSizePercentil(LogSizesPercentil),
)
if err != nil {
t.Errorf(err.Error())
}
n := 1000
for i := 0; i < n; i++ {
wp.Feed(i)
}
// wait for completion
wp.Wait()
// stop workerpool
wp.Stop()
// Without retry unreliable job with 50% failure, we should have n/2 errors
nerrors := wp.AvailableResponses()
if nerrors < (n/2)-E || nerrors > (n/2)+E {
t.Errorf("WithRetry(0): Expected ~%d errors (50%% of %d), got %d", n/2, n, nerrors)
}
// now that control test is established, run with retry
wp, err = New(job.unreliable,
WithRetry(5),
WithMaxWorker(100),
WithEvaluationTime(1),
WithSizePercentil(LogSizesPercentil),
)
if err != nil {
t.Errorf(err.Error())
}
n = 1000
for i := 0; i < n; i++ {
wp.Feed(i)
}
// wait for completion
wp.Wait()
// stop workerpool
wp.Stop()
// With 3 possible retry, we should have close to 0 errors
nerrors = wp.AvailableResponses()
if nerrors > E {
t.Errorf("WithRetry(5): Expected ~0 errors, got %d", nerrors)
}
}
func TestMaxQueue(t *testing.T) {
job := &Job{}
wp, err := New(job.execute,
WithMaxQueue(10),
)
if err != nil {
t.Errorf(err.Error())
}
for i := 0; i < 100; i++ {
wp.Feed(i)
}
// wait for completion
wp.Wait()
// stop workerpool
wp.Stop()
}
func TestExec(t *testing.T) {
job := &Job{}
if testing.Short() {
t.Skip("skipping test in short mode.")
}
wp, err := New(job.execute,
WithMaxWorker(15),
WithEvaluationTime(1),
WithSizePercentil(AllInSizesPercentil),
)
if err != nil {
t.Errorf(err.Error())
}
for i := 0; i < 10; i++ {
go func(i int) {
begin := time.Now()
resp, err := wp.Exec(i)
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if resp.(int) != 2*i {
t.Errorf("Expected i = %d, got %d", 2*i, resp.(int))
return
}
d := time.Since(begin)
if d > (100+e)*time.Millisecond || d < (100-e)*time.Millisecond {
t.Errorf("Expected Exec of 100ms, got %s", d)
}
}(i)
}
time.Sleep(3 * time.Second)
wp.Stop()
}