-
Notifications
You must be signed in to change notification settings - Fork 12
/
awg_test.go
496 lines (380 loc) · 10.4 KB
/
awg_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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package awg
import (
"context"
"errors"
"runtime"
"testing"
"time"
)
var count int64
func slowFunc() error {
for i := 0; i < 10000000; i++ {
count *= int64(i)
}
return nil
}
func fastFunc() error {
// do nothing
return nil
}
func errorFunc() error {
for i := 0; i < 10000; i++ {
count *= int64(i)
}
return errors.New("Test error")
}
func panicFunc() error {
panic("Test panic")
}
// TestAdvancedWorkGroupTimeout test for timeout
func Test_AdvancedWorkGroupTimeout(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.SetTimeout(time.Nanosecond * 10).Start()
if wg.Status() != StatusTimeout {
t.Error("AWG should stops by timeout!")
}
err := wg.GetLastError()
if err == nil {
t.Error("AWG should stops with error by timeout!")
}
if _, ok := err.(ErrorTimeout); !ok {
t.Errorf("Wrong error type. Got %[1]T: %[1]q", err)
}
}
// Test_AdvancedWorkGroupTimeout_Context test for timeout
func Test_AdvancedWorkGroupTimeout_Context(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Nanosecond))
defer cancel()
wg.WithContext(ctx).Start()
if wg.Status() != StatusTimeout {
t.Error("AWG should stops by timeout!")
}
err := wg.GetLastError()
if err == nil {
t.Error("AWG should stops with error by timeout!")
}
if _, ok := err.(ErrorTimeout); !ok {
t.Errorf("Wrong error type. Got %[1]T: %[1]q", err)
}
}
// TestAdvancedWorkGroupError test for error
func Test_AdvancedWorkGroupError(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(errorFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.SetStopOnError(true).Start()
if wg.Status() != StatusError {
t.Error("AWG should stops by error!")
}
}
// TestAdvancedWorkGroupSuccess test for success case
func Test_AdvancedWorkGroupSuccess(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
if errs := wg.SetStopOnError(true).Start().GetAllErrors(); len(errs) != 0 {
t.Errorf("AWG result should be 'success'! But got errors %v", errs)
}
if wg.Status() != StatusSuccess {
t.Error("AWG result should be 'success'!")
}
}
// Test_AdvancedWorkGroupSuccess_WithCapacity test for success case with capacity
func Test_AdvancedWorkGroupSuccess_WithCapacity(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.SetCapacity(2)
if errs := wg.SetStopOnError(true).Start().GetAllErrors(); len(errs) != 0 {
t.Errorf("AWG result should be 'success'! But got errors %v", errs)
}
if wg.Status() != StatusSuccess {
t.Error("AWG result should be 'success'!")
}
}
// Test_AdvancedWorkGroupCancel_Success test for cancel case
func Test_AdvancedWorkGroupCancel_Success(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
time.Sleep(5 * time.Microsecond)
cancel()
}()
if errs := wg.WithContext(ctx).SetStopOnError(true).Start().GetAllErrors(); len(errs) != 0 {
t.Errorf("AWG result should be 'success'! But got errors %v", errs)
}
if wg.Status() != StatusSuccess {
t.Error("AWG result should be 'success'!")
}
}
// Test_AdvancedWorkGroupCancelWithCapacity_Success test for cancel case with capacity
func Test_AdvancedWorkGroupCancelWithCapacity_Success(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.SetCapacity(2)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
time.Sleep(5 * time.Microsecond)
cancel()
}()
if errs := wg.WithContext(ctx).SetStopOnError(true).Start().GetAllErrors(); len(errs) != 0 {
t.Errorf("AWG result should be 'success'! But got errors %v", errs)
}
if wg.Status() != StatusSuccess {
t.Error("AWG result should be 'success'!")
}
}
// TestAdvancedWorkGroupPanicError test for success case
func Test_AdvancedWorkGroupPanicError(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(slowFunc)
wg.Add(panicFunc)
wg.SetStopOnError(true).Start()
if wg.Status() != StatusError {
t.Error("AWG result should be 'error'!")
}
t.Log(wg.GetLastError())
}
// TestAdvancedWorkGroupPanicSuccess test for success case
func TestAdvancedWorkGroupPanicSuccess(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(slowFunc)
wg.Add(panicFunc)
err := wg.SetStopOnError(false).Start().GetLastError()
if err == nil {
t.Error("Panic should be an error")
}
}
// Test_AdvancedWorkGroupStopOnErrorPanic test for success case
func Test_AdvancedWorkGroupStopOnErrorPanic(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(slowFunc)
wg.Add(panicFunc)
wg.SetStopOnError(true).
Start()
if wg.Status() != StatusError {
t.Error("AWG result should be 'error'!")
}
}
// TestAWGStopOnError tests AdvancedWaitGroup with StopOnError set to false
// and with failing task.
func TestAWGStopOnError(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(errorFunc)
wg.Add(fastFunc)
wg.SetStopOnError(false).
Start()
}
// Test_AdvancedWorkGroup_NoLeak tests for goroutines leaks
func Test_AdvancedWorkGroup_NoLeak(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(errorFunc)
wg.SetStopOnError(true).
Start()
time.Sleep(2 * time.Second)
numGoroutines := runtime.NumGoroutine()
var wg2 AdvancedWaitGroup
wg2.Add(errorFunc)
wg2.Add(slowFunc)
wg2.SetStopOnError(true).
Start()
time.Sleep(3 * time.Second)
numGoroutines2 := runtime.NumGoroutine()
if numGoroutines != numGoroutines2 {
t.Fatalf("We leaked %d goroutine(s)", numGoroutines2-numGoroutines)
}
}
// Test_AdvancedWorkGroupAddSliceTimeout test for timeout
func Test_AdvancedWorkGroupAddSliceTimeout(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc, fastFunc, fastFunc, slowFunc, slowFunc, slowFunc})
wg.SetTimeout(time.Nanosecond * 10).SetStopOnError(true).Start()
if wg.Status() != StatusTimeout {
t.Error("AWG should stops by timeout!", wg.Status())
}
err := wg.GetLastError()
if err == nil {
t.Error("AWG should stops with error by timeout!")
}
if _, ok := err.(ErrorTimeout); !ok {
t.Errorf("Wrong error type. Got %[1]T: %[1]q", err)
}
}
// Test_AdvancedWorkGroupAddSliceTimeoutSuccess test for timeout that if too long
func Test_AdvancedWorkGroupAddSliceTimeoutSuccess(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc, fastFunc, fastFunc})
wg.SetTimeout(time.Second * 10).SetStopOnError(true).Start()
if wg.Status() != StatusSuccess {
t.Error("AWG shouldn`t stops by timeout!", wg.Status())
}
err := wg.GetLastError()
if err != nil {
t.Error("AWG shouldn`t stops with error by timeout!")
}
}
// Test_AdvancedWorkGroupGetLastError test
func Test_AdvancedWorkGroupGetLastErrorSuccess(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc})
wg.Start()
err := wg.GetLastError()
if err != nil {
t.Error("Shouldn`t get errors!")
}
}
// Test_AdvancedWorkGroupGetLastErrorSingleError test
func Test_AdvancedWorkGroupGetLastErrorSingleError(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc, errorFunc})
wg.Start()
err := wg.GetLastError()
if err == nil {
t.Error("Should get error!")
}
}
// Test_AdvancedWorkGroupGetAllErrorsSuccess test
func Test_AdvancedWorkGroupGetAllErrorsSuccess(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc})
wg.Start()
errs := wg.GetAllErrors()
if len(errs) != 0 {
t.Error("Shouldn`t get errors!")
}
}
// Test_AdvancedWorkGroupGetAllErrorsManyErrors test
func Test_AdvancedWorkGroupGetAllErrorsManyErrors(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc, errorFunc, errorFunc})
wg.Start()
errs := wg.GetAllErrors()
if len(errs) != 2 {
t.Error("Should get errors")
}
}
// Test_AdvancedWorkGroupGetAllErrorsSingleError test
func Test_AdvancedWorkGroupGetAllErrorsSingleError(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc, errorFunc})
wg.Start()
errs := wg.GetAllErrors()
if len(errs) != 1 {
t.Error("Should get one error!")
}
}
// Test_AdvancedWorkGroupReset test
func Test_AdvancedWorkGroupReset(t *testing.T) {
var wg AdvancedWaitGroup
wg.AddSlice([]WaitgroupFunc{fastFunc, errorFunc})
wg.Start()
wg.Reset()
if wg.Status() != StatusIdle {
t.Error("Cleaned wg should have idle status")
}
wg.Add(errorFunc, errorFunc)
if errs := wg.Start().GetAllErrors(); len(errs) != 2 {
t.Error("Should get two errors on cleaned wg")
}
}
// Test_AdvancedWorkGroupDoubleStart test
func Test_AdvancedWorkGroupDoubleStart(t *testing.T) {
var wg1, wg2 AdvancedWaitGroup
wg1.AddSlice([]WaitgroupFunc{slowFunc, errorFunc, fastFunc, errorFunc})
chDone := make(chan struct{})
go func() {
wg1.Start()
chDone <- struct{}{}
}()
<-chDone
if errs := wg1.Start().GetAllErrors(); len(errs) != 2 {
t.Error("Should get two errors on wg")
}
chDone2 := make(chan struct{})
wg2.AddSlice([]WaitgroupFunc{slowFunc, errorFunc, fastFunc, errorFunc})
go func() {
<-chDone2
wg2.Start()
}()
wg2.Start()
chDone2 <- struct{}{}
if errs := wg2.GetAllErrors(); len(errs) != 2 {
t.Error("Should get two errors on wg")
}
}
var results = make(chan bool, 100)
func fastFuncWithResult() error {
results <- true
return nil
}
// TestAdvancedWorkGroupTimeout test for timeout
func Test_AdvancedWorkGroupTimeout_Execution(t *testing.T) {
var wg AdvancedWaitGroup
maxProcs := 8 * runtime.NumCPU()
for i := 0; i < maxProcs; i++ {
wg.Add(errorFunc)
}
for i := 0; i < maxProcs; i++ {
wg.Add(fastFuncWithResult)
}
wg.SetTimeout(time.Microsecond).SetStopOnError(true).Start()
time.Sleep(time.Second)
close(results)
err := wg.GetLastError()
if err == nil {
t.Error("AWG should stops with error")
}
if errs := wg.GetAllErrors(); len(errs) > 0 {
for _, err := range errs {
t.Log(err)
}
}
count := 0
for range results {
count++
}
if count >= maxProcs {
t.Errorf("Some wg functions should be interrupted")
}
//Debug
t.Logf("Done %v of %v", count, maxProcs)
}