-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil_test.go
53 lines (49 loc) · 1.31 KB
/
testutil_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
package smartpoll
import (
"bytes"
"fmt"
"runtime"
"runtime/pprof"
"testing"
"time"
)
// checkNumGoroutines is intended to be used to check for errant goroutines,
// like `defer checkNumGoroutines(time.Second * 3)(t)`.
func checkNumGoroutines(max time.Duration) func(t *testing.T) {
before := runtime.NumGoroutine()
return func(t *testing.T) {
if t != nil {
t.Helper()
}
after := waitNumGoroutines(max, func(n int) bool { return n <= before })
if after > before {
var b bytes.Buffer
_ = pprof.Lookup("goroutine").WriteTo(&b, 1)
testingErrorfOrPanic(t, "%s\n\nstarted with %d goroutines finished with %d", b.Bytes(), before, after)
}
}
}
// waitNumGoroutines will block until there are a target number of goroutines
// remaining, or a max duration is exceeded.
func waitNumGoroutines(maxDur time.Duration, fn func(n int) bool) (n int) {
const minDur = time.Millisecond * 10
if maxDur < minDur {
maxDur = minDur
}
count := int(maxDur / minDur)
maxDur /= time.Duration(count)
n = runtime.NumGoroutine()
for i := 0; i < count && !fn(n); i++ {
time.Sleep(maxDur)
runtime.GC()
n = runtime.NumGoroutine()
}
return
}
func testingErrorfOrPanic(t *testing.T, format string, values ...interface{}) {
if t == nil {
panic(fmt.Errorf(format, values...))
}
t.Helper()
t.Errorf(format, values...)
}