-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcyclicbarrier_test.go
353 lines (313 loc) · 7.29 KB
/
cyclicbarrier_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package cyclicbarrier
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
)
func checkBarrier(t *testing.T, b CyclicBarrier,
expectedParties, expectedNumberWaiting int, expectedIsBroken bool) {
parties, numberWaiting := b.GetParties(), b.GetNumberWaiting()
isBroken := b.IsBroken()
if expectedParties >= 0 && parties != expectedParties {
t.Error("barrier must have parties = ", expectedParties, ", but has ", parties)
}
if expectedNumberWaiting >= 0 && numberWaiting != expectedNumberWaiting {
t.Error("barrier must have numberWaiting = ", expectedNumberWaiting, ", but has ", numberWaiting)
}
if isBroken != expectedIsBroken {
t.Error("barrier must have isBroken = ", expectedIsBroken, ", but has ", isBroken)
}
}
func TestNew(t *testing.T) {
tests := []func(){
func() {
b := New(10)
checkBarrier(t, b, 10, 0, false)
if b.(*cyclicBarrier).barrierAction != nil {
t.Error("barrier have unexpected barrierAction")
}
},
func() {
defer func() {
if recover() == nil {
t.Error("Panic expected")
}
}()
_ = New(0)
},
func() {
defer func() {
if recover() == nil {
t.Error("Panic expected")
}
}()
_ = New(-1)
},
}
for _, test := range tests {
test()
}
}
func TestNewWithAction(t *testing.T) {
tests := []func(){
func() {
b := NewWithAction(10, func() error { return nil })
checkBarrier(t, b, 10, 0, false)
if b.(*cyclicBarrier).barrierAction == nil {
t.Error("barrier doesn't have expected barrierAction")
}
},
func() {
b := NewWithAction(10, nil)
checkBarrier(t, b, 10, 0, false)
if b.(*cyclicBarrier).barrierAction != nil {
t.Error("barrier have unexpected barrierAction")
}
},
func() {
defer func() {
if recover() == nil {
t.Error("Panic expected")
}
}()
_ = NewWithAction(0, func() error { return nil })
},
func() {
defer func() {
if recover() == nil {
t.Error("Panic expected")
}
}()
_ = NewWithAction(-1, func() error { return nil })
},
}
for _, test := range tests {
test()
}
}
func TestAwaitOnce(t *testing.T) {
n := 100 // goroutines count
b := New(n)
ctx := context.Background()
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
err := b.Await(ctx)
if err != nil {
panic(err)
}
wg.Done()
}()
}
wg.Wait()
checkBarrier(t, b, n, 0, false)
}
func TestAwaitMany(t *testing.T) {
n := 100 // goroutines count
m := 1000 // inner cycle count
b := New(n)
ctx := context.Background()
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func(num int) {
for j := 0; j < m; j++ {
err := b.Await(ctx)
if err != nil {
panic(err)
}
}
wg.Done()
}(i)
}
wg.Wait()
checkBarrier(t, b, n, 0, false)
}
func TestAwaitOnceCtxDone(t *testing.T) {
n := 100 // goroutines count
b := New(n + 1) // parties are more than goroutines count so all goroutines will wait
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
var deadlineCount, brokenBarrierCount int32
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func(num int) {
err := b.Await(ctx)
if err == context.DeadlineExceeded {
atomic.AddInt32(&deadlineCount, 1)
} else if err == ErrBrokenBarrier {
atomic.AddInt32(&brokenBarrierCount, 1)
} else {
panic("must be context.DeadlineExceeded or ErrBrokenBarrier error")
}
wg.Done()
}(i)
}
wg.Wait()
checkBarrier(t, b, n+1, -1, true)
if deadlineCount == 0 {
t.Error("must be more than 0 context.DeadlineExceeded errors, but found", deadlineCount)
}
if deadlineCount+brokenBarrierCount != int32(n) {
t.Error("must be exactly", n, "context.DeadlineExceeded and ErrBrokenBarrier errors, but found", deadlineCount+brokenBarrierCount)
}
}
func TestAwaitManyCtxDone(t *testing.T) {
n := 100 // goroutines count
b := New(n)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
for {
err := b.Await(ctx)
if err != nil {
if err != context.DeadlineExceeded && err != ErrBrokenBarrier {
panic("must be context.DeadlineExceeded or ErrBrokenBarrier error")
}
break
}
}
wg.Done()
}()
}
wg.Wait()
checkBarrier(t, b, n, -1, true)
}
func TestAwaitAction(t *testing.T) {
n := 100 // goroutines count
m := 1000 // inner cycle count
ctx := context.Background()
cnt := 0
b := NewWithAction(n, func() error {
cnt++
return nil
})
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
for j := 0; j < m; j++ {
err := b.Await(ctx)
if err != nil {
panic(err)
}
}
wg.Done()
}()
}
wg.Wait()
checkBarrier(t, b, n, 0, false)
if cnt != m {
t.Error("cnt must be equal to = ", m, ", but it's ", cnt)
}
}
func TestReset(t *testing.T) {
n := 100 // goroutines count
b := New(n + 1) // parties are more than goroutines count so all goroutines will wait
ctx := context.Background()
go func() {
time.Sleep(30 * time.Millisecond)
b.Reset()
}()
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
err := b.Await(ctx)
if err != ErrBrokenBarrier {
panic(err)
}
wg.Done()
}()
}
wg.Wait()
checkBarrier(t, b, n+1, 0, false)
}
func TestAwaitErrorInActionThenReset(t *testing.T) {
n := 100 // goroutines count
ctx := context.Background()
errExpected := errors.New("test error")
isActionCalled := false
var expectedErrCount, errBrokenBarrierCount int32
b := NewWithAction(n, func() error {
isActionCalled = true
return errExpected
})
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
err := b.Await(ctx)
if err == errExpected {
atomic.AddInt32(&expectedErrCount, 1)
} else if err == ErrBrokenBarrier {
atomic.AddInt32(&errBrokenBarrierCount, 1)
} else {
panic(err)
}
wg.Done()
}()
}
wg.Wait()
checkBarrier(t, b, n, n, true) // check that barrier is broken
if !isActionCalled {
t.Error("barrier action must be called")
}
if !b.IsBroken() {
t.Error("barrier must be broken via action error")
}
if expectedErrCount != 1 {
t.Error("expectedErrCount must be equal to", 1, ", but it equals to", expectedErrCount)
}
if errBrokenBarrierCount != int32(n-1) {
t.Error("expectedErrCount must be equal to", n-1, ", but it equals to", errBrokenBarrierCount)
}
// call await on broken barrier must return ErrBrokenBarrier
if b.Await(ctx) != ErrBrokenBarrier {
t.Error("call await on broken barrier must return ErrBrokenBarrier")
}
// do reset broken barrier
b.Reset()
if b.IsBroken() {
t.Error("barrier must not be broken after reset")
}
checkBarrier(t, b, n, 0, false)
}
func TestAwaitTooMuchGoroutines(t *testing.T) {
n := 100 // goroutines count
m := 1000 // inner cycle count
b := New(1)
ctx := context.Background()
var panicCount int32
wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
go func(num int) {
defer func() {
if recover() != nil {
atomic.AddInt32(&panicCount, 1)
}
wg.Done()
}()
for j := 0; j < m; j++ {
err := b.Await(ctx)
if err != nil {
panic(err)
}
}
}(i)
}
wg.Wait()
checkBarrier(t, b, 1, 0, false)
if panicCount == 0 {
t.Error("barrier must panic when await is called from too much goroutines")
}
}