| 90 | } |
| 91 | |
| 92 | func TestTimerStop(t *testing.T) { |
| 93 | // Set a small MaxSleepTime for testing |
| 94 | setMaxSleepTimeForTest(t, testMaxSleepTime) |
| 95 | |
| 96 | t.Run("stop before trigger", func(t *testing.T) { |
| 97 | start := clock.Now() |
| 98 | target := start.Add(100 * time.Millisecond) |
| 99 | |
| 100 | timer := NewTimer(clock.Now, target) |
| 101 | timer.Stop() |
| 102 | time.Sleep(20 * time.Millisecond) |
| 103 | |
| 104 | select { |
| 105 | case <-timer.C: |
| 106 | t.Error("timer triggered after being stopped") |
| 107 | default: |
| 108 | } |
| 109 | }) |
| 110 | |
| 111 | t.Run("stop after trigger", func(t *testing.T) { |
| 112 | start := clock.Now() |
| 113 | target := start.Add(10 * time.Millisecond) |
| 114 | |
| 115 | timer := NewTimer(clock.Now, target) |
| 116 | <-timer.C |
| 117 | timer.Stop() |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | func TestTimerConcurrentStop(t *testing.T) { |
| 122 | // Set a small MaxSleepTime for testing |