-
Notifications
You must be signed in to change notification settings - Fork 8
/
manual_test.go
426 lines (336 loc) · 8.85 KB
/
manual_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package abtime
import (
"context"
"errors"
"testing"
"time"
)
const (
afterID = iota
sleepID
tickID
tickID2
afterFuncID
timerID
contextID
childContextID
)
func TestAfter(t *testing.T) {
at := NewManual()
// assuredly trigger before the After is even called
at.Trigger(afterID)
sent := make(chan time.Time)
go func() {
ch := at.After(time.Second, afterID)
t := <-ch
sent <- t
}()
result := <-sent
if result != at.now.Add(time.Second) {
t.Fatal("Got wrong time sent for After")
}
go func() {
ch := at.After(time.Second, afterID)
t := <-ch
sent <- t
}()
// Bootstrapping problem; we can't depend on abtime working to test
// abtime...
time.Sleep(time.Millisecond)
at.Trigger(afterID)
result = <-sent
if result != at.now.Add(time.Second) {
t.Fatal("Got the wrong time sent for the After after the call")
}
at.Advance(time.Second)
}
func TestSleep(t *testing.T) {
at := NewManual()
// trigger the sleep before it even exists
at.Trigger(sleepID)
finished := make(chan struct{})
go func() {
at.Sleep(time.Second, sleepID)
finished <- struct{}{}
}()
<-finished
go func() {
at.Sleep(time.Second, sleepID)
finished <- struct{}{}
}()
time.Sleep(time.Millisecond)
at.Trigger(sleepID)
<-finished
at.Trigger(2, 4)
for i := 0; i < 5; i++ {
go func(innerI int) {
at.Sleep(time.Second, innerI)
finished <- struct{}{}
}(i)
}
time.Sleep(time.Millisecond)
at.Trigger(0, 1, 3)
for i := 0; i < 5; i++ {
<-finished
}
// if we get here, we must not have deadlocked
}
func TestTick(t *testing.T) {
// significance of this date left as an exercise for the reader
testTime := time.Date(2012, 3, 28, 12, 0, 0, 0, time.UTC)
at := NewManualAtTime(testTime)
if at.Now() != testTime {
t.Fatal("Now is not working correctly.")
}
at.Trigger(tickID)
received := make(chan time.Time)
go func() {
ch := at.Tick(time.Second, tickID)
tick1 := <-ch
tick2 := <-ch
received <- tick1
received <- tick2
}()
time.Sleep(time.Millisecond)
at.Trigger(tickID)
time1 := <-received
time2 := <-received
if time1 != testTime.Add(time.Second) || time2 != testTime.Add(2*time.Second) {
t.Fatal("tick did not deliver the correct time")
}
ticker := at.NewTicker(time.Second, tickID2)
ch := ticker.Channel()
at.Trigger(tickID2)
<-ch
ticker.Reset(time.Second)
ticker.Stop()
at.Trigger(tickID2)
// if this test failed, it would hang the test waiting to write on ch.
}
func TestAfterFunc(t *testing.T) {
at := NewManual()
funcRun := make(chan struct{})
timer := at.AfterFunc(time.Second, func() {
funcRun <- struct{}{}
}, afterFuncID)
if timer.Channel() != nil {
t.Fatal("Channel on AfterFunc not working properly.")
}
// not that this really means much here
if timer.Reset(time.Second * 2) {
t.Fatal("Reset should not be returning true here")
}
at.Trigger(afterFuncID)
<-funcRun
timer2 := at.AfterFunc(time.Second, func() {
panic("I should never be run!")
}, afterFuncID+1)
if !timer2.Stop() {
t.Fatal("Stop should not return true like this")
}
at.Trigger(afterFuncID + 1)
if timer2.Stop() || !timer2.Reset(time.Second*3) {
t.Fatal("Stop/Reset should be returning true here")
}
}
func TestTimer(t *testing.T) {
at := NewManual()
timer := at.NewTimer(time.Second, timerID)
go func() {
at.Trigger(timerID)
}()
curTime := <-timer.Channel()
if at.now.Add(time.Second) != curTime {
t.Fatal("Timer not sending proper time")
}
timer = at.NewTimer(time.Second, timerID)
timer.Reset(2 * time.Second)
if !timer.Stop() {
t.Fatal("Stopping the timer should have returned true")
}
at.Trigger(timerID)
// no good way to test the stop worked, the Stop description in
// the time package explicitly says it does not close the channel.
}
// This tests for a bug I encountered in real code. If a timer is Stopped,
// then Reset, it got itself marked as not running permanently, so future
// Stops would return the wrong value, and things trying to drain it would
// hang because there was nothing to drain.
func TestTimerResetRunsCorrectly(t *testing.T) {
at := NewManual()
timer := at.NewTimer(time.Second, timerID)
ret := timer.Stop()
if !ret {
t.Fatal("timer should return true to indicate Stop() stopped it")
}
ret = timer.Stop()
if ret {
t.Fatal("timer should return false to indicate Stop() didn't stop it")
}
timer.Reset(time.Second)
ret = timer.Stop()
if !ret {
// This is where the bug would occur.
t.Fatal("timer should return true to indicate Stop() stopped it")
}
}
func TestInterfaceConformance(t *testing.T) {
// this just verifies that both implementations actually implement
// ManualTime. Nothing else in the package actually does....
var at AbstractTime // nolint: megacheck
at = NewManual()
at = NewRealTime()
at.Now()
}
func TestNowQueueing(t *testing.T) {
// this verifies that the "nows" queue in the expected manner
at := NewManual()
firstNow := at.Now()
desired := []time.Time{firstNow.Add(10 * time.Second), firstNow.Add(20 * time.Second)}
at.QueueNows(desired...)
if at.Now() != desired[0] {
t.Fatal("Failed to queue properly")
}
if at.Now() != desired[1] {
t.Fatal("Failed to advance queue properly")
}
if at.Now() != desired[1] {
t.Fatal("Failed to 'stick' the time properly.")
}
}
func TestTimerReset(t *testing.T) {
c := NewManual()
d := time.Hour
timer := c.NewTimer(d, timerID)
wasActive := timer.Reset(d)
if !wasActive {
t.Fatal("Unexpected value from the Reset method")
}
go func() {
c.Advance(d)
c.Trigger(timerID)
}()
<-timer.Channel()
wasActive = timer.Reset(d)
if wasActive {
t.Fatal("Unexpected reset result value")
}
}
func TestMultipleTimerCreation(t *testing.T) {
c := NewManual()
_ = c.NewTimer(time.Second, timerID)
// Klunky. More sign this is wrong. running "go" to register the
// trigger doesn't make sense here.
for {
c.Lock()
_, registered := c.triggers[timerID]
c.Unlock()
if registered {
break
}
time.Sleep(time.Microsecond)
}
c.Unregister(timerID)
timer2 := c.NewTimer(time.Second, timerID)
go c.Trigger(timerID)
<-timer2.Channel()
}
func TestContextCancel(t *testing.T) {
mt := NewManual()
ctx, cancelF := mt.WithTimeout(context.Background(), time.Minute, contextID)
select {
case <-ctx.Done():
t.Fatal("context done channel already closed")
default:
}
if ctx.Err() != nil {
t.Fatal("context error is not nil")
}
cancelF()
select {
case <-ctx.Done():
default:
t.Fatal("context done channel open when it should be closed")
}
if ctx.Err() == nil || !errors.Is(ctx.Err(), context.Canceled) {
t.Fatal("context error is not context.Canceled")
}
}
func TestContextTrigger(t *testing.T) {
mt := NewManual()
ctx, cancelF := mt.WithTimeout(context.Background(), time.Minute, contextID)
select {
case <-ctx.Done():
t.Fatal("context done channel already closed")
default:
}
if ctx.Err() != nil {
t.Fatal("context error is not nil")
}
mt.Trigger(contextID)
select {
case <-ctx.Done():
default:
t.Fatal("context done channel open when it should be closed")
}
if ctx.Err() == nil || !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Fatal("context error is not context.DeadlineExceeded")
}
cancelF()
if ctx.Err() == nil || !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Fatal("context error is not context.DeadlineExceeded")
}
}
func TestContextNestedCancel(t *testing.T) {
mt := NewManual()
parent, parentCancel := context.WithCancel(context.Background())
child, childCancel := mt.WithTimeout(parent, time.Minute, contextID)
select {
case <-child.Done():
t.Fatal("context done channel already closed")
default:
}
if child.Err() != nil {
t.Fatal("context error is not nil")
}
parentCancel()
select {
case <-child.Done():
case <-time.After(50 * time.Microsecond):
t.Fatal("context done channel open when it should be closed")
}
if child.Err() == nil || !errors.Is(child.Err(), context.Canceled) {
t.Fatal("context error is not context.Canceled")
}
childCancel()
}
func TestContextNestedTimeout(t *testing.T) {
mt := NewManual()
parent, parentCancel := mt.WithTimeout(context.Background(), time.Minute, contextID)
child, childCancel := mt.WithTimeout(parent, time.Minute, childContextID)
select {
case <-child.Done():
t.Fatal("context done channel already closed")
default:
}
if child.Err() != nil {
t.Fatal("context error is not nil")
}
mt.Trigger(contextID)
select {
case <-child.Done():
case <-time.After(50 * time.Microsecond):
t.Fatal("context done channel open when it should be closed")
}
if child.Err() == nil || !errors.Is(child.Err(), context.DeadlineExceeded) {
t.Fatal("context error is not context.DeadlineExceeded")
}
childCancel()
if child.Err() == nil || !errors.Is(child.Err(), context.DeadlineExceeded) {
t.Fatal("context error is not context.DeadlineExceeded")
}
parentCancel()
if child.Err() == nil || !errors.Is(child.Err(), context.DeadlineExceeded) {
t.Fatal("context error is not context.DeadlineExceeded")
}
}