MCPcopy
hub / github.com/grpc/grpc-go / TestTrackTimers

Function TestTrackTimers

internal/leakcheck/leakcheck_test.go:97–134  ·  view source on GitHub ↗

TestTrackTimers verifies that only leaked timers are reported and expired, stopped timers are ignored.

(t *testing.T)

Source from the content-addressed store, hash-verified

95// TestTrackTimers verifies that only leaked timers are reported and expired,
96// stopped timers are ignored.
97func TestTrackTimers(t *testing.T) {
98 TrackTimers()
99 const leakCount = 3
100 for i := 0; i < leakCount; i++ {
101 internal.TimeAfterFunc(2*time.Second, func() {
102 t.Logf("Timer %d fired.", i)
103 })
104 }
105 wg := sync.WaitGroup{}
106 // Let a couple of timers expire.
107 for i := 0; i < 2; i++ {
108 wg.Add(1)
109 internal.TimeAfterFunc(time.Millisecond, func() {
110 wg.Done()
111 })
112 }
113 wg.Wait()
114
115 // Stop a couple of timers.
116 for i := 0; i < leakCount; i++ {
117 t := internal.TimeAfterFunc(time.Hour, func() {
118 t.Error("Timer fired before test ended.")
119 })
120 t.Stop()
121 }
122
123 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
124 defer cancel()
125 e := &testLogger{}
126 CheckTimers(ctx, e)
127 if e.errorCount != leakCount {
128 t.Errorf("CheckTimers found %v leaks, want %v leaks", e.errorCount, leakCount)
129 t.Logf("leaked timers:\n%v", strings.Join(e.errors, "\n"))
130 }
131 ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
132 defer cancel()
133 CheckTimers(ctx, t)
134}
135
136func TestLeakChecker_DetectsLeak(t *testing.T) {
137 TrackAsyncReporters()

Callers

nothing calls this directly

Calls 10

TrackTimersFunction · 0.85
CheckTimersFunction · 0.85
WaitMethod · 0.80
JoinMethod · 0.80
LogfMethod · 0.65
AddMethod · 0.65
ErrorMethod · 0.65
StopMethod · 0.65
ErrorfMethod · 0.65
DoneMethod · 0.45

Tested by

no test coverage detected