-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbarrier_test.go
106 lines (90 loc) · 1.61 KB
/
barrier_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
package barrier
import (
"sync"
"testing"
)
func TestFall(t *testing.T) {
var w sync.WaitGroup
defer w.Wait() // Main should wait for its goroutines
var b Barrier
w.Add(1)
go func() {
defer w.Done()
defer b.Fall()
<-b.Barrier() // Many goroutines can wait on the barrier
}()
w.Add(1)
go func() {
defer w.Done()
defer b.Fall()
// If any goroutine finishes, they all finish
}()
}
func TestForwards(t *testing.T) {
var w sync.WaitGroup
defer w.Wait() // Main should wait for its goroutines
var b Barrier
var b2 Barrier
// If b falls, b2 should fall
b.Forward(&b2)
w.Add(1)
go func() {
defer w.Done()
defer b.Fall()
<-b2.Barrier()
<-b.Barrier()
}()
w.Add(1)
go func() {
defer w.Done()
defer b.Fall()
}()
}
func TestForwardFallen(t *testing.T) {
// When a fallen barrier is forwarded, the forwardee should immediately fall
var b, f Barrier
b.Fall()
b.Forward(&f)
<-f.Barrier()
}
func BenchmarkBarrier(b *testing.B) {
for i := 0; i < b.N; i++ {
var b Barrier
go b.Fall()
<-b.Barrier()
}
}
func BenchmarkBarrierNoGoroutine(b *testing.B) {
for i := 0; i < b.N; i++ {
var b Barrier
b.Fall()
<-b.Barrier()
}
}
func BenchmarkForward(b *testing.B) {
for i := 0; i < b.N; i++ {
var b, f Barrier
b.Forward(&f)
go b.Fall()
<-f.Barrier()
}
}
func BenchmarkForward2(b *testing.B) {
for i := 0; i < b.N; i++ {
var b, f, g Barrier
b.Forward(&f)
f.Forward(&g)
go b.Fall()
<-g.Barrier()
}
}
func BenchmarkForward3(b *testing.B) {
for i := 0; i < b.N; i++ {
var b, f, g, h Barrier
b.Forward(&f)
f.Forward(&g)
g.Forward(&h)
go b.Fall()
<-h.Barrier()
}
}