| 145 | } |
| 146 | |
| 147 | func TestTimerEdgeCases(t *testing.T) { |
| 148 | // Set a small MaxSleepTime for testing |
| 149 | setMaxSleepTimeForTest(t, testMaxSleepTime) |
| 150 | |
| 151 | t.Run("past time", func(t *testing.T) { |
| 152 | start := clock.Now() |
| 153 | target := start.Add(-1 * time.Second) |
| 154 | |
| 155 | timer := NewTimer(clock.Now, target) |
| 156 | select { |
| 157 | case <-timer.C: |
| 158 | case <-time.After(10 * time.Millisecond): |
| 159 | t.Error("timer did not trigger immediately for past time") |
| 160 | } |
| 161 | }) |
| 162 | |
| 163 | t.Run("exactly now", func(t *testing.T) { |
| 164 | start := clock.Now() |
| 165 | target := start |
| 166 | |
| 167 | timer := NewTimer(clock.Now, target) |
| 168 | select { |
| 169 | case <-timer.C: |
| 170 | case <-time.After(10 * time.Millisecond): |
| 171 | t.Error("timer did not trigger immediately for current time") |
| 172 | } |
| 173 | }) |
| 174 | |
| 175 | t.Run("very long duration", func(t *testing.T) { |
| 176 | start := clock.Now() |
| 177 | target := start.Add(100 * time.Millisecond) // Use a shorter duration for testing |
| 178 | |
| 179 | timer := NewTimer(clock.Now, target) |
| 180 | select { |
| 181 | case <-timer.C: |
| 182 | elapsed := clock.Now().Sub(start) |
| 183 | // Allow tolerance for timing variations |
| 184 | if elapsed < 100*time.Millisecond-20*time.Millisecond || elapsed > 100*time.Millisecond+50*time.Millisecond { |
| 185 | t.Errorf("very long timer triggered at wrong time: expected ~%v, got %v", 100*time.Millisecond, elapsed) |
| 186 | } |
| 187 | case <-time.After(200 * time.Millisecond): |
| 188 | t.Error("very long timer did not trigger within expected time") |
| 189 | } |
| 190 | }) |
| 191 | } |
| 192 | |
| 193 | func TestTimerChannelBehavior(t *testing.T) { |
| 194 | // Set a small MaxSleepTime for testing |