-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_test.go
More file actions
239 lines (194 loc) · 5.34 KB
/
timer_test.go
File metadata and controls
239 lines (194 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"sync"
"testing"
"time"
)
// TestNewSecondsTimer verifies that a new timer is created with correct initial state
func TestNewSecondsTimer(t *testing.T) {
duration := 5 * time.Second
timer := NewSecondsTimer(duration)
if timer == nil {
t.Fatal("NewSecondsTimer returned nil")
}
// Check that end time is approximately correct (within 100ms tolerance)
expectedEnd := time.Now().Add(duration)
actualEnd := timer.End()
diff := actualEnd.Sub(expectedEnd).Abs()
if diff > 100*time.Millisecond {
t.Errorf("End time diff too large: expected ~%v, got %v (diff: %v)", expectedEnd, actualEnd, diff)
}
// Clean up
timer.Stop()
}
// TestTimerReset verifies that Reset correctly updates the timer
func TestTimerReset(t *testing.T) {
timer := NewSecondsTimer(time.Hour)
timer.Stop()
// Reset to a new duration
newDuration := 10 * time.Second
timer.Reset(newDuration)
// Verify end time is updated
expectedEnd := time.Now().Add(newDuration)
actualEnd := timer.End()
diff := actualEnd.Sub(expectedEnd).Abs()
if diff > 100*time.Millisecond {
t.Errorf("After Reset, end time diff too large: diff=%v", diff)
}
// Clean up
timer.Stop()
}
// TestTimerStop verifies that Stop prevents timer from firing
func TestTimerStop(t *testing.T) {
timer := NewSecondsTimer(100 * time.Millisecond)
// Stop immediately
stopped := timer.Stop()
if !stopped {
t.Error("Stop() returned false, expected true for active timer")
}
// Wait to ensure timer doesn't fire
time.Sleep(200 * time.Millisecond)
// Timer should not have fired
select {
case <-timer.C():
t.Error("Timer fired after Stop()")
default:
// Expected - timer didn't fire
}
}
// TestTimerTimeRemaining verifies TimeRemaining calculation
func TestTimerTimeRemaining(t *testing.T) {
duration := 2 * time.Second
timer := NewSecondsTimer(duration)
defer timer.Stop()
// Check immediately after creation
remaining := timer.TimeRemaining()
if remaining < time.Second || remaining > duration {
t.Errorf("TimeRemaining outside expected range: got %v, expected ~%v", remaining, duration)
}
// Wait and check again
time.Sleep(500 * time.Millisecond)
remaining = timer.TimeRemaining()
if remaining < 1*time.Second || remaining > 2*time.Second {
t.Errorf("After 500ms, TimeRemaining = %v, expected ~1.5s", remaining)
}
}
// TestTimerTimeRemainingExpired verifies TimeRemaining returns 0 for expired timer
func TestTimerTimeRemainingExpired(t *testing.T) {
timer := NewSecondsTimer(50 * time.Millisecond)
// Wait for expiration
time.Sleep(100 * time.Millisecond)
remaining := timer.TimeRemaining()
if remaining != 0 {
t.Errorf("Expired timer TimeRemaining = %v, expected 0", remaining)
}
// Drain the channel
select {
case <-timer.C():
default:
}
}
// TestTimerConcurrentAccess verifies thread safety
func TestTimerConcurrentAccess(t *testing.T) {
timer := NewSecondsTimer(time.Hour)
defer timer.Stop()
var wg sync.WaitGroup
iterations := 100
// Multiple goroutines reading End()
for range 10 {
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
_ = timer.End()
time.Sleep(time.Microsecond)
}
}()
}
// Multiple goroutines reading TimeRemaining()
for range 10 {
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
_ = timer.TimeRemaining()
time.Sleep(time.Microsecond)
}
}()
}
// One goroutine doing Reset
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
timer.Reset(time.Hour)
time.Sleep(time.Millisecond)
}
}()
// Wait for all goroutines
wg.Wait()
}
// TestTimerFiring verifies timer actually fires
func TestTimerFiring(t *testing.T) {
duration := 100 * time.Millisecond
timer := NewSecondsTimer(duration)
// Wait for timer to fire
select {
case <-timer.C():
// Expected - timer fired
case <-time.After(200 * time.Millisecond):
t.Error("Timer did not fire within expected time")
}
}
// TestTimerResetMultipleTimes verifies multiple Reset calls work correctly
func TestTimerResetMultipleTimes(t *testing.T) {
timer := NewSecondsTimer(time.Hour)
timer.Stop()
for i := range 5 {
duration := time.Duration(i+1) * time.Second
timer.Reset(duration)
remaining := timer.TimeRemaining()
if remaining < duration-100*time.Millisecond || remaining > duration {
t.Errorf("Reset #%d: TimeRemaining = %v, expected ~%v", i, remaining, duration)
}
}
timer.Stop()
}
// TestTimerChannelReadOnly verifies C() returns read-only channel
func TestTimerChannelReadOnly(t *testing.T) {
timer := NewSecondsTimer(time.Hour)
defer timer.Stop()
// This should compile - C() returns <-chan time.Time (read-only)
ch := timer.C()
// Verify it's a channel
if ch == nil {
t.Error("C() returned nil channel")
}
}
// BenchmarkTimerTimeRemaining benchmarks TimeRemaining performance
func BenchmarkTimerTimeRemaining(b *testing.B) {
timer := NewSecondsTimer(time.Hour)
defer timer.Stop()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = timer.TimeRemaining()
}
}
// BenchmarkTimerEnd benchmarks End performance
func BenchmarkTimerEnd(b *testing.B) {
timer := NewSecondsTimer(time.Hour)
defer timer.Stop()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = timer.End()
}
}
// BenchmarkTimerReset benchmarks Reset performance
func BenchmarkTimerReset(b *testing.B) {
timer := NewSecondsTimer(time.Hour)
defer timer.Stop()
b.ResetTimer()
for i := 0; i < b.N; i++ {
timer.Reset(time.Hour)
}
}