Skip to content

Commit 9af90a0

Browse files
committed
replace sleep() with channel
1 parent 67f7beb commit 9af90a0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

internal/leakcheck/leakcheck_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ func (e *testLogger) Errorf(format string, args ...any) {
4444

4545
func TestCheck(t *testing.T) {
4646
const leakCount = 3
47+
ch := make(chan struct{})
4748
for i := 0; i < leakCount; i++ {
48-
go func() { time.Sleep(2 * time.Second) }()
49+
go func() { <-ch }()
4950
}
5051
if leaked := interestingGoroutines(); len(leaked) != leakCount {
5152
t.Errorf("interestingGoroutines found %v leaks, want %v leaks", len(leaked), leakCount)
5253
}
5354
e := &testLogger{}
5455
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
5556
defer cancel()
56-
if CheckGoroutines(ctx, e); e.errorCount == 0 {
57+
if CheckGoroutines(ctx, e); e.errorCount < 3 {
5758
t.Errorf("CheckGoroutines found %v leaks, want %v leaks", e.errorCount, leakCount)
5859
}
60+
close(ch)
5961
ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
6062
defer cancel()
6163
CheckGoroutines(ctx, t)
@@ -68,8 +70,9 @@ func ignoredTestingLeak(d time.Duration) {
6870
func TestCheckRegisterIgnore(t *testing.T) {
6971
RegisterIgnoreGoroutine("ignoredTestingLeak")
7072
const leakCount = 3
73+
ch := make(chan struct{})
7174
for i := 0; i < leakCount; i++ {
72-
go func() { time.Sleep(2 * time.Second) }()
75+
go func() { <-ch }()
7376
}
7477
if leaked := interestingGoroutines(); len(leaked) != leakCount {
7578
t.Errorf("interestingGoroutines found %v leaks, want %v leaks", len(leaked), leakCount)
@@ -78,9 +81,10 @@ func TestCheckRegisterIgnore(t *testing.T) {
7881
e := &testLogger{}
7982
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
8083
defer cancel()
81-
if CheckGoroutines(ctx, e); e.errorCount == 0 {
84+
if CheckGoroutines(ctx, e); e.errorCount < 3 {
8285
t.Errorf("CheckGoroutines found %v leaks, want %v leaks", e.errorCount, leakCount)
8386
}
87+
close(ch)
8488
ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
8589
defer cancel()
8690
CheckGoroutines(ctx, t)

0 commit comments

Comments
 (0)