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

Code before:
	}
	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")
	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() = %v, want length %v", 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 < leakCount {
		t.Errorf("CheckGoroutines() = %v, want count %v", 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)
}

// TestTrackTimers verifies that only leaked timers are reported and expired,
// stopped timers are ignored.
func TestTrackTimers(t *testing.T) {
	TrackTimers()
	const leakCount = 3
	for i := 0; i < leakCount; i++ {
		internal.TimeAfterFunc(2*time.Second, func() {
			t.Logf("Timer %d fired.", i)
		})
	}
	wg := sync.WaitGroup{}
	// Let a couple of timers expire.
	for i := 0; i < 2; i++ {
		wg.Add(1)
		internal.TimeAfterFunc(time.Millisecond, func() {
			wg.Done()

Review comment (bug):
This assertion will never fail because we have asserted `len(leaked) == leakCount` above. Should we add a check for the upper bound also to ensure `CheckGoroutines` is actually ignoring the spawned goroutine?

Code after:
	}
	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 {
		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)
}

// TestTrackTimers verifies that only leaked timers are reported and expired,
// stopped timers are ignored.
func TestTrackTimers(t *testing.T) {
	TrackTimers()
	const leakCount = 3
	for i := 0; i < leakCount; i++ {
		internal.TimeAfterFunc(2*time.Second, func() {
			t.Logf("Timer %d fired.", i)
		})
	}
	wg := sync.WaitGroup{}
	// Let a couple of timers expire.
	for i := 0; i < 2; i++ {
		wg.Add(1)
		internal.TimeAfterFunc(time.Millisecond, func() {
			wg.Done()