-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait_group_test.go
215 lines (163 loc) · 4.55 KB
/
wait_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
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
package syncx
import (
"context"
"errors"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var errTest = errors.New("test error")
func slowFunc() error {
time.Sleep(2 * time.Second)
context.WithTimeout(context.Background(), 10*time.Second)
return nil
}
func fastFunc() error {
// do nothing
return nil
}
func errorFunc() error {
return errTest
}
func panicFunc() error {
panic("test panic")
}
func incFunc(v *int) func() error {
return func() error {
*v++
return nil
}
}
// Test_AdvancedWaitGroup_Success test for success case
func Test_AdvancedWaitGroup_Success(t *testing.T) {
var wg AdvancedWaitGroup
var r1, r2, r3, r4 int
wg.Add(incFunc(&r1))
wg.Add(incFunc(&r2))
wg.Add(incFunc(&r3))
wg.Add(incFunc(&r4))
wg.Start()
sum := r1 + r2 + r3 + r4
assert.Equal(t, 4, sum)
assert.Equal(t, StatusSuccess, wg.Status())
assert.NoError(t, wg.LastError())
assert.Len(t, wg.AllErrors(), 0)
}
// Test_AdvancedWaitGroup_SuccessWithErrors test for success case
func Test_AdvancedWaitGroup_SuccessWithErrors(t *testing.T) {
var wg AdvancedWaitGroup
var r1, r2 int
wg.Add(errorFunc)
wg.Add(incFunc(&r1))
wg.Add(incFunc(&r2))
wg.Add(errorFunc)
wg.Start()
sum := r1 + r2
assert.Equal(t, 2, sum)
assert.Equal(t, StatusSuccess, wg.Status())
assert.Error(t, errTest, wg.LastError())
assert.Len(t, wg.AllErrors(), 2)
}
// Test_AdvancedWaitGroup_Timeout test for timeout
func Test_AdvancedWaitGroup_Timeout(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
start := time.Now()
wg.SetContext(context.Background()).SetTimeout(time.Nanosecond * 10).
Start()
diff := time.Now().Sub(start).Nanoseconds()
assert.True(t, diff < (time.Second).Nanoseconds(), "AWG should be canceled immediately")
assert.Equal(t, StatusTimeout, wg.Status(), "AWG should stops by timeout!")
}
// Test_AdvancedWaitGroup_Cancel test for cancel
func Test_AdvancedWaitGroup_Cancel(t *testing.T) {
var wg AdvancedWaitGroup
ctx, cancelFunc := context.WithCancel(context.Background())
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(fastFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
wg.Add(slowFunc)
go func() {
time.Sleep(200 * time.Millisecond)
cancelFunc()
}()
start := time.Now()
wg.SetContext(ctx).
Start()
diff := time.Now().Sub(start).Nanoseconds()
assert.True(t, diff < (time.Second).Nanoseconds(), "AWG should be canceled immediately")
assert.Equal(t, StatusCanceled, wg.Status(), "AWG status should be StatusCanceled")
}
// Test_AdvancedWaitGroup_DontExecCanceled test for don't run goroutines if cantext is canceled
func Test_AdvancedWaitGroup_DontExecCanceled(t *testing.T) {
var wg AdvancedWaitGroup
ctx, cancelFunc := context.WithCancel(context.Background())
var i int
wg.Add(incFunc(&i))
cancelFunc()
wg.SetContext(ctx).
Start()
assert.Equal(t, StatusCanceled, wg.Status(), "AWG status should be StatusCanceled")
assert.Equal(t, 0, i)
}
// Test_AdvancedWaitGroup_StopOnError test for error
func Test_AdvancedWaitGroup_StopOnError(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(errorFunc)
wg.Add(slowFunc)
start := time.Now()
wg.SetStopOnError(true).
Start()
diff := time.Now().Sub(start).Nanoseconds()
assert.True(t, diff < (time.Second).Nanoseconds(), "AWG should be canceled immediately")
assert.Equal(t, errTest, wg.LastError())
assert.Equal(t, StatusError, wg.Status(), "AWG status should be StatusError!")
}
// Test_AdvancedWaitGroup_Panic test for success case
func Test_AdvancedWaitGroup_Panic(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(slowFunc)
wg.Add(panicFunc)
wg.SetStopOnError(true).
Start()
assert.Equal(t, StatusError, wg.Status())
assert.Contains(t, wg.LastError().Error(), "test panic")
}
// Test_AdvancedWaitGroup_Reset test for reset
func Test_AdvancedWaitGroup_Reset(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(fastFunc).
SetTimeout(5 * time.Second).
SetContext(context.Background()).
SetStopOnError(true).
Start()
wg.Reset()
assert.Zero(t, wg)
}
// Test_AdvancedWaitGroup_NoLeak tests for goroutines leaks
func Test_AdvancedWaitGroup_NoLeak(t *testing.T) {
var wg AdvancedWaitGroup
wg.Add(errorFunc)
wg.SetStopOnError(true).
Start()
time.Sleep(3 * 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)
}
}