Skip to content

leakcheck: Fix flaky test TestCheck #8309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions internal/leakcheck/leakcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ func (e *testLogger) Errorf(format string, args ...any) {

func TestCheck(t *testing.T) {
const leakCount = 3
ch := make(chan struct{})
for i := 0; i < leakCount; i++ {
go func() { time.Sleep(2 * time.Second) }()
go func() { <-ch }()
}
if ig := interestingGoroutines(); len(ig) == 0 {
t.Error("blah")
if leaked := interestingGoroutines(); len(leaked) != leakCount {
t.Errorf("interestingGoroutines() = %d, want length %d", len(leaked), leakCount)
}
e := &testLogger{}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
CheckGoroutines(ctx, e)
if e.errorCount != leakCount {
t.Errorf("CheckGoroutines found %v leaks, want %v leaks", e.errorCount, leakCount)
if CheckGoroutines(ctx, e); e.errorCount < leakCount {
t.Errorf("CheckGoroutines() = %d, want count %d", e.errorCount, leakCount)
t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
}
close(ch)
ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
CheckGoroutines(ctx, t)
Expand All @@ -69,22 +70,23 @@ func ignoredTestingLeak(d time.Duration) {

func TestCheckRegisterIgnore(t *testing.T) {
RegisterIgnoreGoroutine("ignoredTestingLeak")
go ignoredTestingLeak(3 * time.Second)
const leakCount = 3
ch := make(chan struct{})
for i := 0; i < leakCount; i++ {
go func() { time.Sleep(2 * time.Second) }()
go func() { <-ch }()
}
go func() { ignoredTestingLeak(3 * time.Second) }()
if ig := interestingGoroutines(); len(ig) == 0 {
t.Error("blah")
if leaked := interestingGoroutines(); len(leaked) != leakCount {
t.Errorf("interestingGoroutines() = %d, want length %d", len(leaked), leakCount)
}
e := &testLogger{}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
CheckGoroutines(ctx, e)
if e.errorCount != leakCount {
t.Errorf("CheckGoroutines found %v leaks, want %v leaks", e.errorCount, leakCount)
if CheckGoroutines(ctx, e); e.errorCount < leakCount {
t.Errorf("CheckGoroutines() = %d, want count %d", e.errorCount, leakCount)
t.Logf("leaked goroutines:\n%v", strings.Join(e.errors, "\n"))
}
close(ch)
ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
CheckGoroutines(ctx, t)
Expand Down