-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait_test.go
89 lines (81 loc) · 1.44 KB
/
wait_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
package utils
import (
"math/rand"
"sync"
"testing"
"time"
)
func TestWaitSingleJobDone(t *testing.T) {
status := false
var wg sync.WaitGroup
WaitSingleJobDone(&wg, func(wg *sync.WaitGroup) {
status = true
wg.Done()
})
if false == status {
t.Error("testing failed")
}
}
func TestWaitJobsQueueDone(t *testing.T) {
for i := 0; i < 100; i++ {
rand.Seed(time.Now().UnixNano())
r := rand.Intn(1234567890)
rr := r
var wg sync.WaitGroup
WaitJobsQueueDone(&wg, func(wg *sync.WaitGroup) {
r = r + 1
wg.Done()
}, func(wg *sync.WaitGroup) {
r = r * 2
wg.Done()
}, func(wg *sync.WaitGroup) {
r = r - 3
wg.Done()
}, func(wg *sync.WaitGroup) {
r = r * 4
wg.Done()
}, func(wg *sync.WaitGroup) {
r = r / 5
wg.Done()
})
expected := ((((rr + 1) * 2) - 3) * 4) / 5
if expected != r {
t.Error("testing failed")
}
}
}
func TestWaitJobsDone(t *testing.T) {
r1, r2, r3, r4, r5 := 0, 0, 0, 0, 0
var wg sync.WaitGroup
WaitJobsDone(&wg, func(wg *sync.WaitGroup) {
r1++
wg.Done()
}, func(wg *sync.WaitGroup) {
r2++
wg.Done()
}, func(wg *sync.WaitGroup) {
r3++
wg.Done()
}, func(wg *sync.WaitGroup) {
r4++
wg.Done()
}, func(wg *sync.WaitGroup) {
r5++
wg.Done()
})
if 1 != r1 {
t.Error("testing failed")
}
if 1 != r2 {
t.Error("testing failed")
}
if 1 != r3 {
t.Error("testing failed")
}
if 1 != r4 {
t.Error("testing failed")
}
if 1 != r5 {
t.Error("testing failed")
}
}