This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
mock_test.go
168 lines (147 loc) · 5.27 KB
/
mock_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
package clock
import (
"testing"
"time"
)
const timeout = 10 * time.Millisecond
func assertGets(t *testing.T, c <-chan time.Time, fail string) {
select {
case <-c:
case <-time.After(timeout):
t.Error(fail)
}
}
func assertDoesntGet(t *testing.T, c <-chan time.Time, fail string) {
select {
case now := <-c:
t.Errorf("Failed at time %d: "+fail, now.UnixNano())
case <-time.After(timeout):
}
}
func assertBool(t *testing.T, expected, actual bool, fail string) {
if expected != actual {
t.Errorf("Expected %t to be %t:"+fail, actual, expected)
}
}
func TestNewMockClockWithManyArguments(t *testing.T) {
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected code to have panicked!")
}
}()
NewMockClock(time.Now(), time.Now().Add(time.Second*5))
}()
}
func TestNewMockClockWithOneArguments(t *testing.T) {
date, _ := time.Parse(time.UnixDate, "Sat Mar 7 11:12:39 PST 2015")
c := NewMockClock(date)
if c.Now() != date {
t.Errorf("expected date to be %v, but got %v", date, c.Now())
}
}
func TestNewMockClockWithNoArguments(t *testing.T) {
c := NewMockClock()
dt := time.Now().Sub(c.Now())
if dt > time.Millisecond {
t.Errorf("expected diference in time to be less than 1ms")
}
}
func TestAfterGetsNegative(t *testing.T) {
c := NewMockClock()
assertGets(t, c.After(-time.Millisecond), "expected negative time to elapse immediately")
}
func TestAfterGetsExact(t *testing.T) {
c := NewMockClock()
ch := c.After(time.Millisecond * 2)
assertDoesntGet(t, ch, "expected after not to get immediately")
c.AddTime(time.Millisecond)
assertDoesntGet(t, ch, "expected after not to get after 1 ms")
c.AddTime(time.Millisecond)
assertGets(t, ch, "expected after to get after 1 ms")
}
func TestAfterGetsOvershoot(t *testing.T) {
c := NewMockClock()
ch := c.After(time.Millisecond * 2)
c.AddTime(time.Millisecond * 100)
assertGets(t, ch, "expected after to get overshot")
}
func TestSleepWorks(t *testing.T) {
// sleep just wraps .After internally, so just make sure it looks like it works
c := NewMockClock()
ch := make(chan time.Time)
go func() {
c.Sleep(10 * time.Millisecond)
ch <- c.Now()
}()
assertDoesntGet(t, ch, "expected sleep to not get immediately")
c.AddTime(time.Millisecond * 10)
assertGets(t, ch, "expected sleep to return eventually")
}
func TestTickerWorks(t *testing.T) {
c := NewMockClock()
tk := c.NewTicker(5 * time.Millisecond)
assertDoesntGet(t, tk.Chan(), "expected ticker to not get immediately")
c.AddTime(4 * time.Millisecond)
assertDoesntGet(t, tk.Chan(), "expected ticker to not get after 4ms")
c.AddTime(5 * time.Millisecond)
assertGets(t, tk.Chan(), "expected ticker to get after interval")
c.AddTime(5 * time.Millisecond)
assertGets(t, tk.Chan(), "expected ticker to keep getting")
tk.Stop()
c.AddTime(10 * time.Millisecond)
assertDoesntGet(t, tk.Chan(), "expected ticker not to get after stopped")
}
func TestTickerCatchesUp(t *testing.T) {
c := NewMockClock()
tk := c.NewTicker(5 * time.Millisecond)
c.AddTime(20 * time.Millisecond)
assertGets(t, tk.Chan(), "ticker sends catchup 1")
assertGets(t, tk.Chan(), "ticker sends catchup 2")
assertGets(t, tk.Chan(), "ticker sends catchup 3")
assertGets(t, tk.Chan(), "ticker sends catchup 4")
assertDoesntGet(t, tk.Chan(), "ticker catchup doesn't overshoot")
}
func TestTimerWorks(t *testing.T) {
c := NewMockClock()
tm := c.NewTimer(5 * time.Millisecond)
assertDoesntGet(t, tm.Chan(), "expected timer to not get immediately")
c.AddTime(4 * time.Millisecond)
assertDoesntGet(t, tm.Chan(), "expected timer to not get after 4ms")
c.AddTime(5 * time.Millisecond)
assertGets(t, tm.Chan(), "expected timer to get after interval")
c.AddTime(5 * time.Millisecond)
assertDoesntGet(t, tm.Chan(), "expected timer not to keep getting")
assertBool(t, false, tm.Stop(), "expected reset to return false after timer finishes")
}
func TestTimerResets(t *testing.T) {
c := NewMockClock()
tm := c.NewTimer(5 * time.Millisecond)
c.AddTime(4 * time.Millisecond)
assertDoesntGet(t, tm.Chan(), "expected timer to not get after 4ms")
assertBool(t, true, tm.Reset(time.Millisecond*3), "expected reset to return true while timer running")
c.AddTime(2 * time.Millisecond)
assertDoesntGet(t, tm.Chan(), "expected timer not to get after reset before interval is up")
c.AddTime(time.Millisecond)
assertGets(t, tm.Chan(), "expected timer to get after reset when interval is up")
assertBool(t, false, tm.Stop(), "expected stop to return false after timer finishes")
c.AddTime(100 * time.Millisecond)
assertDoesntGet(t, tm.Chan(), "expected timer to not get after finished")
assertBool(t, false, tm.Reset(time.Millisecond*3), "expected reset to return false after timer finishes")
c.AddTime(3 * time.Millisecond)
assertGets(t, tm.Chan(), "expected timer to get after reset when interval is up after restarted")
}
func TestTickerDeadlock(t *testing.T) { // fixes #6
c := NewMockClock()
tk := c.NewTicker(5 * time.Millisecond)
c.AddTime(6 * time.Millisecond)
time.Sleep(1 * time.Millisecond)
tk.Stop()
}
func TestSince(t *testing.T) {
c := NewMockClock()
before := c.Now()
assertBool(t, true, c.Since(before) == 0, "expected no time to pass without AddTime called")
c.AddTime(4 * time.Millisecond)
assertBool(t, true, c.Since(before) == 4*time.Millisecond, "expected Since to return time added with AddTime")
}