TestTrackTimers verifies that only leaked timers are reported and expired, stopped timers are ignored.
(t *testing.T)
| 95 | // TestTrackTimers verifies that only leaked timers are reported and expired, |
| 96 | // stopped timers are ignored. |
| 97 | func 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 | |
| 136 | func TestLeakChecker_DetectsLeak(t *testing.T) { |
| 137 | TrackAsyncReporters() |
nothing calls this directly
no test coverage detected