forked from ysmood/gotrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testleak.go
67 lines (54 loc) · 1.72 KB
/
testleak.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
package testleak
import (
"context"
"log"
"os"
"testing"
"time"
"github.com/ysmood/gotrace"
)
const leakErrMsg = "leaking goroutines:"
func defaultMaxWait(t time.Duration) time.Duration {
if t <= 0 {
return 3 * time.Second
}
return t
}
// CheckMain reports error if goroutines are leaking after all tests are done. Default timeout is 3s.
// It's powerful but less accurate than Check, if you only use CheckMain it will be hard to tell which test
// is the cause of the leak.
func CheckMain(m *testing.M, maxWait time.Duration, ignores ...gotrace.Ignore) {
code := m.Run()
if code != 0 {
os.Exit(code)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultMaxWait(maxWait))
defer cancel()
if traces := gotrace.Wait(ctx, ignores...); traces.Any() {
log.Fatalln(leakErrMsg, traces)
}
}
// Check reports error if the test is leaking goroutine.
// Default timeout is 3s. Default ignore is gotrace.IgnoreCurrent() .
//
// This check will become useless if t.Parallel() is called for multiple tests,
// because the test framework will execute tests at the same time which makes it impossible to
// write a correct ignore function to detect which goroutine is spawned by current test.
// But you can still use CheckMain to check leak, because it runs after all tests are settled.
func Check(t *testing.T, maxWait time.Duration, ignores ...gotrace.Ignore) {
t.Helper()
if len(ignores) == 0 {
ignores = []gotrace.Ignore{gotrace.IgnoreCurrent()}
}
t.Cleanup(func() {
t.Helper()
if t.Failed() {
return
}
ctx, cancel := context.WithTimeout(context.Background(), defaultMaxWait(maxWait))
defer cancel()
if traces := gotrace.Wait(ctx, ignores...); traces.Any() {
t.Error(leakErrMsg, traces)
}
})
}