forked from jonboulle/clockwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker_test.go
89 lines (74 loc) · 1.76 KB
/
ticker_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
package clockwork
import (
"testing"
"time"
)
func TestFakeTickerStop(t *testing.T) {
fc := &fakeClock{}
ft := fc.NewTicker(1)
ft.Stop()
select {
case <-ft.Chan():
t.Errorf("received unexpected tick!")
default:
}
}
func TestFakeTickerTick(t *testing.T) {
fc := &fakeClock{}
now := fc.Now()
// The tick at now.Add(2) should not get through since we advance time by
// two units below and the channel can hold at most one tick until it's
// consumed.
first := now.Add(1)
second := now.Add(3)
// We wrap the Advance() calls with blockers to make sure that the ticker
// can go to sleep and produce ticks without time passing in parallel.
ft := fc.NewTicker(1)
fc.BlockUntil(1)
fc.Advance(2)
fc.BlockUntil(1)
select {
case tick := <-ft.Chan():
if tick != first {
t.Errorf("wrong tick time, got: %v, want: %v", tick, first)
}
case <-time.After(time.Millisecond):
t.Errorf("expected tick!")
}
// Advance by one more unit, we should get another tick now.
fc.Advance(1)
fc.BlockUntil(1)
select {
case tick := <-ft.Chan():
if tick != second {
t.Errorf("wrong tick time, got: %v, want: %v", tick, second)
}
case <-time.After(time.Millisecond):
t.Errorf("expected tick!")
}
ft.Stop()
}
func TestFakeTicker_Race(t *testing.T) {
fc := NewFakeClock()
tickTime := 1 * time.Millisecond
ticker := fc.NewTicker(tickTime)
defer ticker.Stop()
fc.Advance(tickTime)
timeout := time.NewTimer(500 * time.Millisecond)
defer timeout.Stop()
select {
case <-ticker.Chan():
// Pass
case <-timeout.C:
t.Fatalf("Ticker didn't detect the clock advance!")
}
}
func TestFakeTicker_Race2(t *testing.T) {
fc := NewFakeClock()
ft := fc.NewTicker(5 * time.Second)
for i := 0; i < 100; i++ {
fc.Advance(5 * time.Second)
<-ft.Chan()
}
ft.Stop()
}