...
1
18
19 package leakcheck
20
21 import (
22 "fmt"
23 "strings"
24 "testing"
25 "time"
26 )
27
28 type testErrorfer struct {
29 errorCount int
30 errors []string
31 }
32
33 func (e *testErrorfer) Errorf(format string, args ...interface{}) {
34 e.errors = append(e.errors, fmt.Sprintf(format, args...))
35 e.errorCount++
36 }
37
38 func TestCheck(t *testing.T) {
39 const leakCount = 3
40 for i := 0; i < leakCount; i++ {
41 go func() { time.Sleep(2 * time.Second) }()
42 }
43 if ig := interestingGoroutines(); len(ig) == 0 {
44 t.Error("blah")
45 }
46 e := &testErrorfer{}
47 check(e, time.Second)
48 if e.errorCount != leakCount {
49 t.Errorf("check found %v leaks, want %v leaks", e.errorCount, leakCount)
50 t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
51 }
52 check(t, 3*time.Second)
53 }
54
55 func ignoredTestingLeak(d time.Duration) {
56 time.Sleep(d)
57 }
58
59 func TestCheckRegisterIgnore(t *testing.T) {
60 RegisterIgnoreGoroutine("ignoredTestingLeak")
61 const leakCount = 3
62 for i := 0; i < leakCount; i++ {
63 go func() { time.Sleep(2 * time.Second) }()
64 }
65 go func() { ignoredTestingLeak(3 * time.Second) }()
66 if ig := interestingGoroutines(); len(ig) == 0 {
67 t.Error("blah")
68 }
69 e := &testErrorfer{}
70 check(e, time.Second)
71 if e.errorCount != leakCount {
72 t.Errorf("check found %v leaks, want %v leaks", e.errorCount, leakCount)
73 t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
74 }
75 check(t, 3*time.Second)
76 }
77
View as plain text