-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_test.go
224 lines (183 loc) · 4.81 KB
/
clock_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
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
package clock
import (
"reflect"
"testing"
"time"
)
func TestNewClock(t *testing.T) {
c := NewClock()
first := c.Now()
time.Sleep(100 * time.Millisecond)
second := c.Now()
if first != second {
t.Error("results from subsequent calls to Now() don't match")
t.Errorf("first: %s", first)
t.Errorf("second: %s", second)
}
}
func TestNewClockAt(t *testing.T) {
now := time.Date(2020, 4, 1, 12, 12, 12, 0, time.UTC)
var expected time.Time
var actual time.Time
T := func(testName string) {
if !actual.Equal(expected) {
t.Errorf("test failed: %s", testName)
t.Errorf("expected: %s", expected)
t.Errorf("actual: %s", actual)
}
}
c := NewClockAt(now)
expected = now
actual = c.Now()
T("initial call to Now()")
d := 10 * time.Second
c.Sleep(d)
expected = now.Add(d)
actual = c.Now()
T("check Now() after Sleep()")
}
func TestNewClockFromSlice(t *testing.T) {
time1 := time.Date(2020, 4, 1, 12, 12, 12, 0, time.UTC)
time2 := time.Date(2021, 4, 1, 12, 12, 12, 0, time.UTC)
var expected time.Time
var actual time.Time
T := func(testName string) {
if !actual.Equal(expected) {
t.Errorf("test failed: %s", testName)
t.Errorf("expected: %s", expected)
t.Errorf("actual: %s", actual)
}
}
c := NewClockFromSlice(time1, time2)
expected = time1
actual = c.Now()
T("first instant returned first")
expected = time2
actual = c.Now()
T("second instant returned second")
// We ran out of instants, so on the next call we should panic
defer func() {
if err := recover(); err != nil {
t.Log("recovered from panic as expected")
}
}()
// panics
c.Now()
t.Error("missing panic")
}
func TestNewClockFromCallbacks(t *testing.T) {
fixedTime := time.Date(2020, 4, 1, 12, 12, 12, 0, time.UTC)
nowCallCt := 0
nowLastCalledWith := -1
nowFn := func(i int) time.Time {
nowCallCt++
nowLastCalledWith = i
return fixedTime
}
sleepCallCt := 0
sleepLastCalledWith := -1 * time.Second
sleepFn := func(d time.Duration) {
sleepCallCt++
sleepLastCalledWith = d
}
c := NewClockFromCallbacks(nowFn, sleepFn)
actual := c.Now()
if nowLastCalledWith != 0 {
t.Error("1: failed to call nowFn")
}
if sleepCallCt != 0 {
t.Error("1: unexpectedly called sleepFn")
}
if !actual.Equal(fixedTime) {
t.Error("1: call to Now()")
}
actual = c.Now()
if nowLastCalledWith != 1 { // counter increments correctly
t.Error("2: failed to call nowFn")
}
if sleepCallCt != 0 {
t.Error("2: unexpectedly called sleepFn")
}
if !actual.Equal(fixedTime) { // our function returns the same result
t.Error("2: call to Now()")
}
c.Sleep(1 * time.Minute)
actual = c.Now()
if nowLastCalledWith != 2 { // counter increments correctly
t.Error("3: failed to call nowFn")
}
if sleepCallCt != 1 || sleepLastCalledWith != 1*time.Minute {
t.Error("3: unexpected result calling sleepFn")
}
if !actual.Equal(fixedTime) { // our function returns the same result
t.Error("3: call to Now()")
}
}
func TestClock_Now(t *testing.T) {
tolerance := 50 * time.Millisecond
// When c is nil we should actually return time.Now()
var c *Clock
now := time.Now()
actual := c.Now()
diff := actual.Sub(now)
if diff < -tolerance || diff > tolerance {
t.Error("didn't return real time.Now() value as expected")
t.Errorf("expected: %s ± %d ms", now, tolerance/time.Millisecond)
t.Errorf("actual: %s", actual)
}
}
func TestClock_Sleep(t *testing.T) {
d := 500 * time.Millisecond
tolerance := d / 10
// When c is nil we should actually call time.Sleep()
var c *Clock
before := time.Now()
c.Sleep(d)
after := time.Now()
diff := after.Sub(before) - d
if diff < -tolerance || diff > tolerance {
t.Error("didn't call time.Sleep() as expected")
t.Errorf("expected sleep: %d ± %d ms", d/time.Millisecond, tolerance/time.Millisecond)
t.Errorf("actual sleep: %d ms", after.Sub(before)/time.Millisecond)
}
}
func TestClock_TimeSince(t *testing.T) {
d := 25 * time.Minute
now := time.Date(2020, 4, 1, 12, 12, 12, 0, time.UTC)
before := now.Add(-d)
c := NewClockAt(now)
actual := c.TimeSince(before)
if actual != d {
t.Errorf("expected: %s", d)
t.Errorf("actual: %s", actual)
}
}
func TestClock_NapDurations(t *testing.T) {
c := NewClock()
expected := []time.Duration{
10 * time.Millisecond,
20 * time.Millisecond,
30 * time.Millisecond,
}
for _, d := range expected {
c.Sleep(d)
}
actual := c.NapDurations()
if !reflect.DeepEqual(actual, expected) {
t.Errorf("expected: %s", expected)
t.Errorf("actual: %s", actual)
}
}
func TestClock_ClearNapDurations(t *testing.T) {
c := NewClock()
c.Sleep(10 * time.Millisecond)
if len(c.NapDurations()) != 1 {
t.Error("failed test sanity check")
}
c.ClearNapDurations()
if len(c.NapDurations()) != 0 {
t.Error("failed to clear nap durations")
}
// make sure we still have an initialized slice for napDurations
c.Sleep(10 * time.Millisecond)
}