-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
190 lines (163 loc) · 3.04 KB
/
main_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
package fanout_test
import (
"context"
"sync"
"testing"
"time"
"github.com/badu/fanout"
)
func TestNormalUsage(t *testing.T) {
type Payload struct {
Name string
}
command := fanout.New[Payload]()
ctx, cancel := context.WithCancel(context.Background())
g1c, g2c, g3c := false, false, false
var wg sync.WaitGroup
wg.Add(3)
go func() {
ch := command.Sub()
wg.Done()
for {
select {
case <-ch:
g1c = true
wg.Done()
case <-ctx.Done():
command.Cancel(ch)
wg.Done()
return
}
}
}()
go func() {
ch := command.Sub()
wg.Done()
for {
select {
case <-ch:
g2c = true
wg.Done()
case <-ctx.Done():
command.Cancel(ch)
wg.Done()
return
}
}
}()
go func() {
ch := command.Sub()
wg.Done()
for {
select {
case <-ch:
g3c = true
wg.Done()
case <-ctx.Done():
command.Cancel(ch)
wg.Done()
return
}
}
}()
wg.Wait() // goroutines above needs to be 'ready'
wg.Add(3) // to wait goroutine receive the payload
command.Pub(Payload{Name: "Fire at will!"})
wg.Wait()
command.Close()
// test closing two time
command.Close()
// test publishing after close
command.Pub(Payload{Name: "should NOT work"})
wg.Add(3) // to wait all goroutines exit
cancel()
wg.Wait() // wait for goroutines to receive exit
if !g1c || !g2c || !g3c {
t.Fatal("one goroutine was not called", g1c, g2c, g3c)
}
}
func TestSomeGiveUp(t *testing.T) {
type Payload struct {
Name string
}
command := fanout.New[Payload]()
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
g1calls := 0
wg.Add(3)
go func() {
wg.Done()
ch := command.Sub()
for {
select {
case <-ch:
g1calls++
command.Cancel(ch) // this one gives up after receiving first command
wg.Done()
return
case <-ctx.Done():
t.Fatal("goroutine1 exit should be never called")
return
}
}
}()
g2calls := 0
go func() {
wg.Done()
done := make(chan struct{})
time.AfterFunc(100*time.Millisecond, func() {
close(done)
})
ch := command.Sub()
for {
select {
case <-ch:
g2calls++
t.Fatal("goroutine2 should never receive")
wg.Done()
case <-ctx.Done():
t.Fatal("goroutine2 exit should be never called")
return
case <-done:
command.Cancel(ch)
return
}
}
}()
g3calls := 0
go func() {
wg.Done()
ch := command.Sub()
for {
select {
case <-ch:
g3calls++
wg.Done()
case <-ctx.Done():
command.Cancel(ch)
wg.Done()
return
}
}
}()
wg.Wait() // goroutines above needs to be 'ready'
<-time.After(500 * time.Millisecond) // wait for goroutine #2 to 'expire'
wg.Add(2)
command.Pub(Payload{Name: "Fire at will!"})
wg.Wait()
wg.Add(1)
command.Pub(Payload{Name: "Fire at will #2!"})
wg.Wait()
wg.Add(1)
cancel()
wg.Wait() // wait for goroutines to receive exit
if g1calls != 1 {
t.Log("goroutine #1 should be called once")
}
if g2calls != 0 {
t.Log("goroutine #2 should never been called")
}
if g3calls != 2 {
t.Log("goroutine #3 should be called twice")
}
}