Debian Codegraph @

internal/holsterv4/clock/system_test.go

raw
1 package clock
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestSleep(t *testing.T) {
11 start := Now()
12
13 // When
14 Sleep(100 * time.Millisecond)
15
16 // Then
17 if Now().Sub(start) < 100*time.Millisecond {
18 assert.Fail(t, "Sleep did not last long enough")
19 }
20 }
21
22 func TestAfter(t *testing.T) {
23 start := Now()
24
25 // When
26 end := <-After(100 * time.Millisecond)
27
28 // Then
29 if end.Sub(start) < 100*time.Millisecond {
30 assert.Fail(t, "Sleep did not last long enough")
31 }
32 }
33
34 func TestAfterFunc(t *testing.T) {
35 start := Now()
36 endCh := make(chan time.Time, 1)
37
38 // When
39 AfterFunc(100*time.Millisecond, func() { endCh <- time.Now() })
40
41 // Then
42 end := <-endCh
43 if end.Sub(start) < 100*time.Millisecond {
44 assert.Fail(t, "Sleep did not last long enough")
45 }
46 }
47
48 func TestNewTimer(t *testing.T) {
49 start := Now()
50
51 // When
52 timer := NewTimer(100 * time.Millisecond)
53
54 // Then
55 end := <-timer.C()
56 if end.Sub(start) < 100*time.Millisecond {
57 assert.Fail(t, "Sleep did not last long enough")
58 }
59 }
60
61 func TestTimerStop(t *testing.T) {
62 timer := NewTimer(50 * time.Millisecond)
63
64 // When
65 active := timer.Stop()
66
67 // Then
68 assert.Equal(t, true, active)
69 time.Sleep(100)
70 select {
71 case <-timer.C():
72 assert.Fail(t, "Timer should not have fired")
73 default:
74 }
75 }
76
77 func TestTimerReset(t *testing.T) {
78 t.Skip("fail on the CI for darwin")
79 start := time.Now()
80 timer := NewTimer(300 * time.Millisecond)
81
82 // When
83 timer.Reset(100 * time.Millisecond)
84
85 // Then
86 end := <-timer.C()
87 if end.Sub(start) >= 150*time.Millisecond {
88 assert.Fail(t, "Waited too long", end.Sub(start).String())
89 }
90 }
91
92 func TestNewTicker(t *testing.T) {
93 start := Now()
94
95 // When
96 timer := NewTicker(100 * time.Millisecond)
97
98 // Then
99 end := <-timer.C()
100 if end.Sub(start) < 100*time.Millisecond {
101 assert.Fail(t, "Sleep did not last long enough")
102 }
103 end = <-timer.C()
104 if end.Sub(start) < 200*time.Millisecond {
105 assert.Fail(t, "Sleep did not last long enough")
106 }
107
108 timer.Stop()
109 time.Sleep(150)
110 select {
111 case <-timer.C():
112 assert.Fail(t, "Ticker should not have fired")
113 default:
114 }
115 }
116
117 func TestTick(t *testing.T) {
118 start := Now()
119
120 // When
121 ch := Tick(100 * time.Millisecond)
122
123 // Then
124 end := <-ch
125 if end.Sub(start) < 100*time.Millisecond {
126 assert.Fail(t, "Sleep did not last long enough")
127 }
128 end = <-ch
129 if end.Sub(start) < 200*time.Millisecond {
130 assert.Fail(t, "Sleep did not last long enough")
131 }
132 }
133
134 func TestNewStoppedTimer(t *testing.T) {
135 timer := NewStoppedTimer()
136
137 // When/Then
138 select {
139 case <-timer.C():
140 assert.Fail(t, "Timer should not have fired")
141 default:
142 }
143 assert.Equal(t, false, timer.Stop())
144 }