forked from jonboulle/clockwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker_test.go
58 lines (49 loc) · 1.15 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
package clockwork
import (
"testing"
)
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)
}
default:
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)
}
default:
t.Errorf("expected tick!")
}
ft.Stop()
}