-
Notifications
You must be signed in to change notification settings - Fork 213
/
watch_test.go
319 lines (283 loc) · 6.78 KB
/
watch_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package memdb
import (
"bytes"
"context"
"fmt"
"runtime/pprof"
"strings"
"testing"
"time"
)
// testWatch makes a bunch of watch channels based on the given size and fires
// the one at the given fire index to make sure it's detected (or a timeout
// occurs if the fire index isn't hit). useCtx parameterizes whether the context
// based watch is used or timer based.
func testWatch(size, fire int, useCtx bool) error {
shouldTimeout := true
ws := NewWatchSet()
for i := 0; i < size; i++ {
watchCh := make(chan struct{})
ws.Add(watchCh)
if fire == i {
close(watchCh)
shouldTimeout = false
}
}
var timeoutCh chan time.Time
var ctx context.Context
var cancelFn context.CancelFunc
if useCtx {
ctx, cancelFn = context.WithCancel(context.Background())
defer cancelFn()
} else {
timeoutCh = make(chan time.Time)
}
doneCh := make(chan bool, 1)
go func() {
if useCtx {
doneCh <- ws.WatchCtx(ctx) != nil
} else {
doneCh <- ws.Watch(timeoutCh)
}
}()
if shouldTimeout {
select {
case <-doneCh:
return fmt.Errorf("should not trigger")
default:
}
if useCtx {
cancelFn()
} else {
close(timeoutCh)
}
select {
case didTimeout := <-doneCh:
if !didTimeout {
return fmt.Errorf("should have timed out")
}
case <-time.After(10 * time.Second):
return fmt.Errorf("should have timed out")
}
} else {
select {
case didTimeout := <-doneCh:
if didTimeout {
return fmt.Errorf("should not have timed out")
}
case <-time.After(10 * time.Second):
return fmt.Errorf("should have triggered")
}
if useCtx {
cancelFn()
} else {
close(timeoutCh)
}
}
return nil
}
func TestWatch(t *testing.T) {
testFactory := func(useCtx bool) func(t *testing.T) {
return func(t *testing.T) {
// Sweep through a bunch of chunks to hit the various cases of dividing
// the work into watchFew calls.
for size := 0; size < 3*aFew; size++ {
// Fire each possible channel slot.
for fire := 0; fire < size; fire++ {
if err := testWatch(size, fire, useCtx); err != nil {
t.Fatalf("err %d %d: %v", size, fire, err)
}
}
// Run a timeout case as well.
fire := -1
if err := testWatch(size, fire, useCtx); err != nil {
t.Fatalf("err %d %d: %v", size, fire, err)
}
}
}
}
t.Run("Timer", testFactory(false))
t.Run("Context", testFactory(true))
}
func testWatchCh(size, fire int) error {
shouldTimeout := true
ws := NewWatchSet()
for i := 0; i < size; i++ {
watchCh := make(chan struct{})
ws.Add(watchCh)
if fire == i {
close(watchCh)
shouldTimeout = false
}
}
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
doneCh := make(chan bool, 1)
go func() {
err := <-ws.WatchCh(ctx)
doneCh <- err != nil
}()
if shouldTimeout {
select {
case <-doneCh:
return fmt.Errorf("should not trigger")
default:
}
cancelFn()
select {
case didTimeout := <-doneCh:
if !didTimeout {
return fmt.Errorf("should have timed out")
}
case <-time.After(10 * time.Second):
return fmt.Errorf("should have timed out")
}
} else {
select {
case didTimeout := <-doneCh:
if didTimeout {
return fmt.Errorf("should not have timed out")
}
case <-time.After(10 * time.Second):
return fmt.Errorf("should have triggered")
}
cancelFn()
}
return nil
}
func TestWatchChan(t *testing.T) {
// Sweep through a bunch of chunks to hit the various cases of dividing
// the work into watchFew calls.
for size := 0; size < 3*aFew; size++ {
// Fire each possible channel slot.
for fire := 0; fire < size; fire++ {
if err := testWatchCh(size, fire); err != nil {
t.Fatalf("err %d %d: %v", size, fire, err)
}
}
// Run a timeout case as well.
fire := -1
if err := testWatchCh(size, fire); err != nil {
t.Fatalf("err %d %d: %v", size, fire, err)
}
}
}
func TestWatch_AddWithLimit(t *testing.T) {
// Make sure nil doesn't crash.
{
var ws WatchSet
ch := make(chan struct{})
ws.AddWithLimit(10, ch, ch)
}
// Run a case where we trigger a channel that should be in
// there.
{
ws := NewWatchSet()
inCh := make(chan struct{})
altCh := make(chan struct{})
ws.AddWithLimit(1, inCh, altCh)
nopeCh := make(chan struct{})
ws.AddWithLimit(1, nopeCh, altCh)
close(inCh)
didTimeout := ws.Watch(time.After(1 * time.Second))
if didTimeout {
t.Fatalf("bad")
}
}
// Run a case where we trigger the alt channel that should have
// been added.
{
ws := NewWatchSet()
inCh := make(chan struct{})
altCh := make(chan struct{})
ws.AddWithLimit(1, inCh, altCh)
nopeCh := make(chan struct{})
ws.AddWithLimit(1, nopeCh, altCh)
close(altCh)
didTimeout := ws.Watch(time.After(1 * time.Second))
if didTimeout {
t.Fatalf("bad")
}
}
// Run a case where we trigger the nope channel that should not have
// been added.
{
ws := NewWatchSet()
inCh := make(chan struct{})
altCh := make(chan struct{})
ws.AddWithLimit(1, inCh, altCh)
nopeCh := make(chan struct{})
ws.AddWithLimit(1, nopeCh, altCh)
close(nopeCh)
didTimeout := ws.Watch(time.After(1 * time.Second))
if !didTimeout {
t.Fatalf("bad")
}
}
}
func TestWatchCtxLeak(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// We add a large number of channels to a WatchSet then
// call WatchCtx. If one of those channels fires, we
// expect to see all the goroutines spawned by WatchCtx
// cleaned up.
pprof.Do(ctx, pprof.Labels("foo", "bar"), func(ctx context.Context) {
ws := NewWatchSet()
fireCh := make(chan struct{})
ws.Add(fireCh)
for i := 0; i < 10000; i++ {
watchCh := make(chan struct{})
ws.Add(watchCh)
}
result := make(chan error)
go func() {
result <- ws.WatchCtx(ctx)
}()
fireCh <- struct{}{}
if err := <-result; err != nil {
t.Fatalf("expected no err got: %v", err)
}
})
numRetries := 3
var gced bool
for i := 0; i < numRetries; i++ {
var pb bytes.Buffer
profiler := pprof.Lookup("goroutine")
if profiler == nil {
t.Fatal("unable to find profile")
}
err := profiler.WriteTo(&pb, 1)
if err != nil {
t.Fatalf("unable to read profile: %v", err)
}
// If the debug profile dump contains the string "foo",
// it means one of the goroutines spawned in pprof.Do above
// still appears in the capture.
if !strings.Contains(pb.String(), "foo") {
gced = true
break
} else {
t.Log("retrying")
time.Sleep(1 * time.Second)
}
}
if !gced {
t.Errorf("goroutines were not garbage collected after %d retries", numRetries)
}
}
func BenchmarkWatch(b *testing.B) {
ws := NewWatchSet()
for i := 0; i < 1024; i++ {
watchCh := make(chan struct{})
ws.Add(watchCh)
}
timeoutCh := make(chan time.Time)
close(timeoutCh)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ws.Watch(timeoutCh)
}
}