-
Notifications
You must be signed in to change notification settings - Fork 3
/
group_test.go
78 lines (61 loc) · 1.39 KB
/
group_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
package wait
import (
"context"
"fmt"
"sync/atomic"
"testing"
)
func do() { fmt.Print("do\n") }
func doWithArgs(i, j int) { fmt.Printf("doWith args: %d %d\n", i, j) }
func doWithContext(ctx context.Context) { fmt.Printf("doWithContext\n") }
func TestExamplesFromReadme(t *testing.T) {
wg := Group{}
wg.Add(do)
wg.Add(func() {
doWithArgs(1, 2)
})
wg.AddWithContext(context.TODO(), doWithContext)
wg.Wait()
}
func TestAddWithMultipleFuncs(t *testing.T) {
value1 := int32(0)
value2 := int32(0)
value3 := int32(0)
wg := Group{}
wg.Add(func() {
atomic.StoreInt32(&value1, 1)
})
wg.Add(func() {
atomic.StoreInt32(&value2, 1)
})
wg.Add(func() {
atomic.StoreInt32(&value3, 1)
})
wg.Wait()
if value1 != 1 || value2 != 1 || value3 != 1 {
t.Error()
}
}
func TestAddWithContextFuncWaitForDone(t *testing.T) {
wg := Group{}
ctx, cancel := context.WithCancel(context.Background())
wg.AddWithContext(ctx, func(ctx context.Context) {
<-ctx.Done()
})
go cancel()
wg.Wait()
}
func TestAddMany(t *testing.T) {
var counter uint32
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
do := func() { atomic.AddUint32(&counter, 1) }
doCtx := func(context.Context) { do() }
wg := Group{}
wg.AddMany(100, do)
wg.AddManyWithContext(ctx, 100, doCtx)
wg.Wait()
if atomic.LoadUint32(&counter) != 200 {
t.Error()
}
}