-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaitgroup_test.go
More file actions
94 lines (91 loc) · 1.5 KB
/
waitgroup_test.go
File metadata and controls
94 lines (91 loc) · 1.5 KB
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
package kit
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func TestWaitGroup(t *testing.T) {
tests := []struct {
name string
run func(*WaitGroup) int
expect int
}{
{
name: "NewWaitGroup not nil",
run: func(w *WaitGroup) int {
if w == nil || w.wg == nil {
return 0
}
return 1
},
expect: 1,
},
{
name: "Do multiple funcs",
run: func(w *WaitGroup) int {
var count atomic.Int64
fn := func() {
time.Sleep(10 * time.Millisecond)
count.Add(1)
}
w.Do(fn, fn, fn)
w.Wait()
return int(count.Load())
},
expect: 3,
},
{
name: "Do empty funcs",
run: func(w *WaitGroup) int {
start := time.Now()
w.Do()
w.Wait()
if time.Since(start) > time.Second {
return 0
}
return 1
},
expect: 1,
},
{
name: "Get underlying WaitGroup",
run: func(w *WaitGroup) int {
if w.Get() == nil {
return 0
}
return 1
},
expect: 1,
},
{
name: "Concurrent increment",
run: func(w *WaitGroup) int {
var mu sync.Mutex
count := 0
increment := func() {
mu.Lock()
count++
mu.Unlock()
}
funcs := make([]func(), 100)
for i := range 100 {
funcs[i] = increment
}
w.Do(funcs...)
w.Wait()
return count
},
expect: 100,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := NewWaitGroup()
got := tt.run(w)
if got != tt.expect {
t.Fatalf("expected %d, got %d", tt.expect, got)
}
})
}
}