| 191 | } |
| 192 | |
| 193 | func TestTimerChannelBehavior(t *testing.T) { |
| 194 | // Set a small MaxSleepTime for testing |
| 195 | setMaxSleepTimeForTest(t, testMaxSleepTime) |
| 196 | |
| 197 | t.Run("channel closed only once", func(t *testing.T) { |
| 198 | start := clock.Now() |
| 199 | target := start.Add(10 * time.Millisecond) |
| 200 | timer := NewTimer(clock.Now, target) |
| 201 | <-timer.C |
| 202 | <-timer.C |
| 203 | <-timer.C |
| 204 | |
| 205 | select { |
| 206 | case <-timer.C: |
| 207 | default: |
| 208 | t.Error("timer channel should remain closed after trigger") |
| 209 | } |
| 210 | }) |
| 211 | |
| 212 | t.Run("stopped timer channel not closed", func(t *testing.T) { |
| 213 | start := clock.Now() |
| 214 | target := start.Add(100 * time.Millisecond) |
| 215 | timer := NewTimer(clock.Now, target) |
| 216 | timer.Stop() |
| 217 | time.Sleep(20 * time.Millisecond) |
| 218 | |
| 219 | select { |
| 220 | case <-timer.C: |
| 221 | t.Error("stopped timer channel should not be closed") |
| 222 | default: |
| 223 | } |
| 224 | }) |
| 225 | } |