MCPcopy
hub / github.com/kopia/kopia / TestTimerEdgeCases

Function TestTimerEdgeCases

internal/sleepable/sleepable_timer_test.go:147–191  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

145}
146
147func 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
193func TestTimerChannelBehavior(t *testing.T) {
194 // Set a small MaxSleepTime for testing

Callers

nothing calls this directly

Calls 9

NowFunction · 0.92
setMaxSleepTimeForTestFunction · 0.85
NewTimerFunction · 0.85
AfterMethod · 0.80
SubMethod · 0.80
ErrorfMethod · 0.80
RunMethod · 0.65
ErrorMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected