-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
236 lines (226 loc) · 5.33 KB
/
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package changroup_test
import (
"math/rand"
"sync"
"testing"
"time"
"github.com/maratori/changroup"
"github.com/stretchr/testify/require"
)
func TestGroup(t *testing.T) { //nolint:gocognit // yeah
t.Parallel()
t.Run("doesn't stuck if not acquired", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
assertDoesNotStuck(t, group.Send, 1)
assertDoesNotStuck(t, group.Send, 2)
assertDoesNotStuck(t, group.Send, 3)
assertDoesNotStuck(t, group.SendAsync, 4)
assertDoesNotStuck(t, group.SendAsync, 5)
assertDoesNotStuck(t, group.SendAsync, 6)
})
t.Run("release closes channel", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
ch, release := group.Acquire()
release()
assertChanClosed(t, ch)
})
t.Run("release can be called twice", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
_, release := group.Acquire()
release()
release()
})
t.Run("ReleaseAll closes all channels", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
ch1, _ := group.Acquire()
ch2, _ := group.Acquire()
group.ReleaseAll()
assertChanClosed(t, ch1)
assertChanClosed(t, ch2)
})
t.Run("ReleaseAll can be called twice and in parallel with ReleaseFunc", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
ch1, release1 := group.Acquire()
ch2, release2 := group.Acquire()
done := make(chan struct{})
for i := 0; i < 2; i++ {
go func() {
group.ReleaseAll()
done <- struct{}{}
}()
go func() {
release1()
done <- struct{}{}
}()
go func() {
release2()
done <- struct{}{}
}()
}
for i := 0; i < 2*3; i++ {
waitChan(t, done)
}
assertChanClosed(t, ch1)
assertChanClosed(t, ch2)
})
t.Run("doesn't send to released channel", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
ch1, _ := group.Acquire()
ch2, release := group.Acquire()
release()
go group.Send(1)
require.Equal(t, 1, waitChan(t, ch1))
assertChanClosed(t, ch2)
group.SendAsync(2)
require.Equal(t, 2, waitChan(t, ch1))
assertChanClosed(t, ch2)
})
t.Run("Send blocks", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
ch, _ := group.Acquire()
done1 := make(chan struct{})
done2 := make(chan struct{})
go func() {
defer close(done1)
group.Send(1)
}()
go func() {
defer close(done2)
waitChan(t, done1) //nolint:testifylint // it's ok to use require here
group.Send(2)
}()
time.Sleep(time.Second)
assertChanBlocked(t, done1)
assertChanBlocked(t, done2)
require.Equal(t, 1, waitChan(t, ch))
waitChan(t, done1)
assertChanBlocked(t, done2)
require.Equal(t, 2, waitChan(t, ch))
waitChan(t, done2)
})
t.Run("SendAsync doesn't block", func(t *testing.T) {
t.Parallel()
group := changroup.NewGroup[int]()
ch1, _ := group.Acquire()
ch2, _ := group.Acquire()
assertDoesNotStuck(t, group.SendAsync, 1)
assertDoesNotStuck(t, group.SendAsync, 2)
assertDoesNotStuck(t, group.SendAsync, 3)
// ch1 is not blocked even if no one is reading ch2
require.ElementsMatch(t, []int{1, 2, 3}, []int{waitChan(t, ch1), waitChan(t, ch1), waitChan(t, ch1)})
require.ElementsMatch(t, []int{1, 2, 3}, []int{waitChan(t, ch2), waitChan(t, ch2), waitChan(t, ch2)})
})
t.Run("concurrency", func(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("long test")
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var mu sync.Mutex
randDuration := func() time.Duration {
mu.Lock()
defer mu.Unlock()
return time.Duration(r.Int63n(int64(time.Millisecond)))
}
group := changroup.NewGroup[struct{}]()
done := make(chan struct{})
stop := make(chan struct{})
go func() {
defer func() { done <- struct{}{} }()
for {
group.Send(struct{}{})
select {
case <-time.After(randDuration()):
case <-stop:
return
}
}
}()
go func() {
defer func() { done <- struct{}{} }()
for {
group.SendAsync(struct{}{})
select {
case <-time.After(randDuration()):
case <-stop:
return
}
}
}()
const n = 1000
for i := 0; i < n; i++ {
go func() {
defer func() { done <- struct{}{} }()
select {
case <-time.After(randDuration()):
case <-stop:
return
}
ch, release := group.Acquire()
defer release()
for {
select {
case <-ch:
case <-time.After(randDuration()):
return
case <-stop:
return
}
}
}()
}
time.Sleep(5 * time.Second)
close(stop)
for i := 0; i < n+2; i++ {
select {
case <-done:
case <-time.After(5 * time.Second):
require.Fail(t, "timeout", "i=%d", i)
}
}
})
}
func waitChan[T any](t *testing.T, ch <-chan T) T {
select {
case v := <-ch:
return v
case <-time.After(5 * time.Second):
require.Fail(t, "timeout")
panic("")
}
}
func assertChanClosed[T any](t *testing.T, ch <-chan T) {
select {
case v, ok := <-ch:
if ok {
require.Failf(t, "chan not closed", "%+v", v)
}
default:
require.Fail(t, "chan blocked")
}
}
func assertChanBlocked[T any](t *testing.T, ch <-chan T) {
select {
case v, ok := <-ch:
if ok {
require.Failf(t, "chan not blocked", "%+v", v)
}
require.Fail(t, "chan closed")
default:
}
}
func assertDoesNotStuck[T any](t *testing.T, fn func(T), value T) {
done := make(chan struct{})
go func() {
defer close(done)
fn(value)
}()
waitChan(t, done)
}