-
Notifications
You must be signed in to change notification settings - Fork 0
/
errgroup_test.go
201 lines (189 loc) · 3.78 KB
/
errgroup_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
package errgroup
import (
"context"
"errors"
"log"
"sync/atomic"
"testing"
"time"
)
func TestErrgroup(t *testing.T) {
var count int64 = 1
countBackup := count
eg := WithContext(context.Background())
// go 协程
eg.Go(func(ctx context.Context) error {
atomic.AddInt64(&count, 1)
return nil
})
// go 协程
eg.Go(func(ctx context.Context) error {
atomic.AddInt64(&count, 1)
return nil
})
// go 协程
eg.Go(func(ctx context.Context) error {
atomic.AddInt64(&count, 1)
return errors.New("error ,reset count")
})
// wait 协程 Done
if err := eg.Wait(); err != nil {
// do some thing
count = countBackup
log.Println(err)
//return
}
log.Println(count)
}
func TestErrgroupLimit1(t *testing.T) {
var (
eg Group
goNum = 3 // every times run goNum goroutine
)
for i := 0; i < 11; i++ {
var count = int64(i)
eg.Go(func(ctx context.Context) error {
atomic.AddInt64(&count, 1)
log.Println("count:", count)
return nil
})
if eg.WorkNum() == goNum {
if err := eg.Wait(); err != nil {
log.Println("err1:", err)
// to do something you need
}
log.Println("wait")
time.Sleep(time.Second)
}
}
if err := eg.Wait(); err != nil {
log.Println("err2:", err)
}
}
func TestErrgroupLimit2(t *testing.T) {
var (
eg Group
)
eg.GOMAXPROCS(3)
for i := 0; i < 11; i++ {
var count = int64(i)
eg.Go(func(ctx context.Context) error {
atomic.AddInt64(&count, 1)
log.Println("count:", count)
time.Sleep(time.Second * 3)
return nil
})
}
if err := eg.Wait(); err != nil {
log.Println("err2:", err)
}
}
func timeSleep1(c context.Context) error {
data := make(chan string, 1)
go func() {
time.Sleep(1 * time.Second)
data <- "timeSleep1 done"
}()
select {
case <-c.Done():
log.Println("timeSleep1 cancel")
case rsp := <-data:
log.Println(rsp)
}
return nil
}
func timeErrSleep3(c context.Context) error {
time.Sleep(3 * time.Second)
return errors.New("timeSleep3 error")
}
func timeTimeoutSleep3(c context.Context) error {
data := make(chan string, 1)
go func() {
time.Sleep(3 * time.Second)
data <- "timeSleep3 done"
}()
select {
case <-c.Done():
log.Println("timeSleep3 timeout")
case rsp := <-data:
log.Println(rsp)
}
return nil
}
func timeCancelSleep5(c context.Context) error {
data := make(chan string, 1)
go func() {
time.Sleep(5 * time.Second)
data <- "timeCancelSleep5 done"
}()
select {
case <-c.Done():
log.Println("timeCancelSleep5 cancel")
case rsp := <-data:
log.Println(rsp)
}
return nil
}
func timeTimeoutSleep5(c context.Context) error {
data := make(chan string, 1)
go func() {
time.Sleep(5 * time.Second)
data <- "timeTimeoutSleep5 done"
}()
select {
case <-c.Done():
return errors.New("timeTimeoutSleep5 timeout")
case rsp := <-data:
log.Println(rsp)
}
return nil
}
func TestErrgroupWithCancel(t *testing.T) {
var (
eg = WithCancel(context.Background())
)
eg.Go(func(ctx context.Context) error {
err := timeSleep1(ctx)
return err
})
eg.Go(func(ctx context.Context) error {
if err := timeErrSleep3(ctx); err != nil {
log.Println(err)
return err
}
return nil
})
eg.Go(func(ctx context.Context) error {
if err := timeCancelSleep5(ctx); err != nil {
log.Printf("err:%v", err)
return err
}
return nil
})
if err := eg.Wait(); err != nil {
// do some thing
}
}
func TestErrgroupWithTimeout(t *testing.T) {
var (
eg = WithTimeout(context.Background(), time.Second*4)
)
eg.Go(func(ctx context.Context) error {
err := timeSleep1(ctx)
return err
})
eg.Go(func(ctx context.Context) error {
err := timeTimeoutSleep3(ctx)
return err
})
eg.Go(func(ctx context.Context) error {
if err := timeTimeoutSleep5(ctx); err != nil {
log.Printf("err:%v", err)
return err
}
return nil
})
if err := eg.Wait(); err != nil {
// do some thing
}
}