MCPcopy
hub / github.com/kopia/kopia / TestNewTimer

Function TestNewTimer

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

Source from the content-addressed store, hash-verified

26}
27
28func TestNewTimer(t *testing.T) {
29 // Set a small MaxSleepTime for testing
30 setMaxSleepTimeForTest(t, testMaxSleepTime)
31
32 tests := []struct {
33 name string
34 duration time.Duration
35 expected time.Duration
36 }{
37 {
38 name: "short duration",
39 duration: 20 * time.Millisecond,
40 expected: 20 * time.Millisecond,
41 },
42 {
43 name: "long duration should wait full duration",
44 duration: 1 * time.Second,
45 expected: 1 * time.Second,
46 },
47 {
48 name: "exactly maxSleepTime",
49 duration: testMaxSleepTime,
50 expected: testMaxSleepTime,
51 },
52 {
53 name: "zero duration",
54 duration: 0,
55 expected: 0,
56 },
57 }
58
59 for _, tt := range tests {
60 t.Run(tt.name, func(t *testing.T) {
61 start := clock.Now()
62 target := start.Add(tt.duration)
63
64 timer := NewTimer(clock.Now, target)
65
66 // Wait for timer to trigger
67 <-timer.C
68
69 elapsed := clock.Now().Sub(start)
70
71 if tt.duration == 0 {
72 if elapsed > 10*time.Millisecond {
73 t.Errorf("zero duration timer took too long: %v", elapsed)
74 }
75
76 return
77 }
78
79 // Allow some tolerance for timing variations
80 tolerance := 50 * time.Millisecond
81 if tt.duration < 100*time.Millisecond {
82 tolerance = 20 * time.Millisecond
83 }
84
85 if elapsed < tt.duration-tolerance || elapsed > tt.duration+tolerance {

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected