Repository: grpc/grpc-go
File: internal/leakcheck/leakcheck_test.go (Go)

Code before:

type testLogger struct {
	errorCount int
	errors     []string
}

func (e *testLogger) Logf(string, ...any) {
}

func (e *testLogger) Errorf(format string, args ...any) {
	e.errors = append(e.errors, fmt.Sprintf(format, args...))
	e.errorCount++
}

func TestCheck(t *testing.T) {
	const leakCount = 3
	for i := 0; i < leakCount; i++ {
		go func() { time.Sleep(2 * time.Second) }()
	}
	if leaked := interestingGoroutines(); len(leaked) != leakCount {
		t.Errorf("interestingGoroutines found %v leaks, want %v leaks", len(leaked), leakCount)
	}
	e := &testLogger{}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	if CheckGoroutines(ctx, e); e.errorCount == 0 {
		t.Errorf("CheckGoroutines found %v leaks, want %v leaks", e.errorCount, leakCount)
	}
	ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()
	CheckGoroutines(ctx, t)
}

func ignoredTestingLeak(d time.Duration) {
	time.Sleep(d)
}

func TestCheckRegisterIgnore(t *testing.T) {
	RegisterIgnoreGoroutine("ignoredTestingLeak")
	const leakCount = 3
	for i := 0; i < leakCount; i++ {
		go func() { time.Sleep(2 * time.Second) }()
	}
	if leaked := interestingGoroutines(); len(leaked) != leakCount {
		t.Errorf("interestingGoroutines found %v leaks, want %v leaks", len(leaked), leakCount)
	}
	go func() { ignoredTestingLeak(3 * time.Second) }()
	e := &testLogger{}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	if CheckGoroutines(ctx, e); e.errorCount == 0 {

Review comment (suggestion):
That works, or you can do

```suggestion
	if CheckGoroutines(ctx, e); ctx.Err() == nil {
```

Or even:

```suggestion
	if CheckGoroutines(ctx, e); e.errorCount < 3 {
```


Code after:
}

func (e *testLogger) Logf(string, ...any) {
}

func (e *testLogger) Errorf(format string, args ...any) {
	e.errors = append(e.errors, fmt.Sprintf(format, args...))
	e.errorCount++
}

func TestCheck(t *testing.T) {
	const leakCount = 3
	ch := make(chan struct{})
	for i := 0; i < leakCount; i++ {
		go func() { <-ch }()
	}
	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()
	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)
}

func ignoredTestingLeak(d time.Duration) {
	time.Sleep(d)
}

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() { <-ch }()
	}
	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()
	if CheckGoroutines(ctx, e); e.errorCount < leakCount {