@@ -44,20 +44,21 @@ func (e *testLogger) Errorf(format string, args ...any) {
44
44
45
45
func TestCheck (t * testing.T ) {
46
46
const leakCount = 3
47
+ ch := make (chan struct {})
47
48
for i := 0 ; i < leakCount ; i ++ {
48
- go func () { time . Sleep ( 2 * time . Second ) }()
49
+ go func () { <- ch }()
49
50
}
50
- if ig := interestingGoroutines (); len (ig ) == 0 {
51
- t .Error ( "blah" )
51
+ if leaked := interestingGoroutines (); len (leaked ) != leakCount {
52
+ t .Errorf ( "interestingGoroutines() = %d, want length %d" , len ( leaked ), leakCount )
52
53
}
53
54
e := & testLogger {}
54
55
ctx , cancel := context .WithTimeout (context .Background (), time .Second )
55
56
defer cancel ()
56
- CheckGoroutines (ctx , e )
57
- if e .errorCount != leakCount {
58
- t .Errorf ("CheckGoroutines found %v leaks, want %v leaks" , e .errorCount , leakCount )
57
+ if CheckGoroutines (ctx , e ); e .errorCount < leakCount {
58
+ t .Errorf ("CheckGoroutines() = %d, want count %d" , e .errorCount , leakCount )
59
59
t .Logf ("leaked goroutines:\n %v" , strings .Join (e .errors , "\n " ))
60
60
}
61
+ close (ch )
61
62
ctx , cancel = context .WithTimeout (context .Background (), 3 * time .Second )
62
63
defer cancel ()
63
64
CheckGoroutines (ctx , t )
@@ -69,22 +70,23 @@ func ignoredTestingLeak(d time.Duration) {
69
70
70
71
func TestCheckRegisterIgnore (t * testing.T ) {
71
72
RegisterIgnoreGoroutine ("ignoredTestingLeak" )
73
+ go ignoredTestingLeak (3 * time .Second )
72
74
const leakCount = 3
75
+ ch := make (chan struct {})
73
76
for i := 0 ; i < leakCount ; i ++ {
74
- go func () { time . Sleep ( 2 * time . Second ) }()
77
+ go func () { <- ch }()
75
78
}
76
- go func () { ignoredTestingLeak (3 * time .Second ) }()
77
- if ig := interestingGoroutines (); len (ig ) == 0 {
78
- t .Error ("blah" )
79
+ if leaked := interestingGoroutines (); len (leaked ) != leakCount {
80
+ t .Errorf ("interestingGoroutines() = %d, want length %d" , len (leaked ), leakCount )
79
81
}
80
82
e := & testLogger {}
81
83
ctx , cancel := context .WithTimeout (context .Background (), time .Second )
82
84
defer cancel ()
83
- CheckGoroutines (ctx , e )
84
- if e .errorCount != leakCount {
85
- t .Errorf ("CheckGoroutines found %v leaks, want %v leaks" , e .errorCount , leakCount )
85
+ if CheckGoroutines (ctx , e ); e .errorCount < leakCount {
86
+ t .Errorf ("CheckGoroutines() = %d, want count %d" , e .errorCount , leakCount )
86
87
t .Logf ("leaked goroutines:\n %v" , strings .Join (e .errors , "\n " ))
87
88
}
89
+ close (ch )
88
90
ctx , cancel = context .WithTimeout (context .Background (), 3 * time .Second )
89
91
defer cancel ()
90
92
CheckGoroutines (ctx , t )
0 commit comments