-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer_test.go
50 lines (39 loc) · 994 Bytes
/
timer_test.go
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
package valkyrietry
import (
"testing"
"time"
)
func TestTimerStart(t *testing.T) {
timer := NewTimer()
duration := 100 * time.Millisecond
start := time.Now()
timer.Start(duration)
<-timer.C()
if time.Since(start) < duration {
t.Errorf("Timer fired before the expected duration")
}
}
func TestTimerReset(t *testing.T) {
timer := NewTimer()
firstDuration := 50 * time.Millisecond
secondDuration := 100 * time.Millisecond
timer.Start(firstDuration)
time.Sleep(30 * time.Millisecond)
timer.Start(secondDuration)
start := time.Now()
<-timer.C()
if elapsed := time.Since(start); elapsed < secondDuration {
t.Errorf("Timer fired before the expected reset duration, elapsed: %v", elapsed)
}
}
func TestTimerStop(t *testing.T) {
timer := NewTimer()
duration := 100 * time.Millisecond
timer.Start(duration)
timer.Stop()
select {
case <-timer.C():
t.Errorf("Timer channel should not receive after being stopped")
case <-time.After(150 * time.Millisecond):
}
}