This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
common_test.go
85 lines (75 loc) · 1.67 KB
/
common_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
// Copyright (c) 2014 Project Iris. All rights reserved.
//
// The current language binding is an official support library of the Iris
// cloud messaging framework, and as such, the same licensing terms apply.
// For details please see http://iris.karalabe.com/downloads#License
package iris
import "sync"
// Configuration values shared by all the tests.
var config = struct {
relay int
cluster string
topic string
}{
relay: 55555,
cluster: "go-binding-test-cluster",
topic: "go-binding-test-topic",
}
// Simple barrier to support synchronizing a batch of goroutines.
type barrier struct {
pend sync.WaitGroup
hold sync.WaitGroup
pass sync.WaitGroup
cont sync.WaitGroup
errc chan error
}
func newBarrier(size int) *barrier {
b := &barrier{
errc: make(chan error, size),
}
b.pend.Add(size)
b.hold.Add(1)
return b
}
// Syncs the goroutines up to begin the next phase.
func (b *barrier) Sync() {
b.pass.Add(1)
b.pend.Done()
b.hold.Wait()
b.pend.Add(1)
b.pass.Done()
b.cont.Wait()
}
// Removes one goroutine from the barrier after the phase.
func (b *barrier) Exit(err error) {
if err != nil {
b.errc <- err
}
b.pass.Add(1)
b.pend.Done()
b.hold.Wait()
b.pass.Done()
}
// Waits for all goroutines to reach the barrier and permits continuation.
func (b *barrier) Wait() []error {
// Wait for all the goroutines to arrive
b.pend.Wait()
b.cont.Add(1)
// Collect all the occurred errors
errs := []error{}
for done := false; !done; {
select {
case err := <-b.errc:
errs = append(errs, err)
default:
done = true
}
}
// Permit all goroutines to continue
b.hold.Done()
b.pass.Wait()
b.hold.Add(1)
b.cont.Done()
// Report the results
return errs
}