@@ -13,6 +13,7 @@ type Clock interface {
13
13
Now () time.Time
14
14
Since (t time.Time ) time.Duration
15
15
NewTicker (d time.Duration ) Ticker
16
+ NewTimer (d time.Duration ) Timer
16
17
}
17
18
18
19
// FakeClock provides an interface for a clock which can be
@@ -70,6 +71,10 @@ func (rc *realClock) NewTicker(d time.Duration) Ticker {
70
71
return & realTicker {time .NewTicker (d )}
71
72
}
72
73
74
+ func (rc * realClock ) NewTimer (d time.Duration ) Timer {
75
+ return & realTimer {time .NewTimer (d )}
76
+ }
77
+
73
78
type fakeClock struct {
74
79
sleepers []* sleeper
75
80
blockers []* blocker
@@ -132,7 +137,7 @@ func (fc *fakeClock) Sleep(d time.Duration) {
132
137
<- fc .After (d )
133
138
}
134
139
135
- // Time returns the current time of the fakeClock
140
+ // Now returns the current time of the fakeClock
136
141
func (fc * fakeClock ) Now () time.Time {
137
142
fc .l .RLock ()
138
143
t := fc .time
@@ -145,6 +150,8 @@ func (fc *fakeClock) Since(t time.Time) time.Duration {
145
150
return fc .Now ().Sub (t )
146
151
}
147
152
153
+ // NewTicker returns a ticker that will expire only after calls to fakeClock
154
+ // Advance have moved the clock passed the given duration
148
155
func (fc * fakeClock ) NewTicker (d time.Duration ) Ticker {
149
156
ft := & fakeTicker {
150
157
c : make (chan time.Time , 1 ),
@@ -156,6 +163,25 @@ func (fc *fakeClock) NewTicker(d time.Duration) Ticker {
156
163
return ft
157
164
}
158
165
166
+ // NewTimer returns a timer that will fire only after calls to fakeClock
167
+ // Advance have moved the clock passed the given duration
168
+ func (fc * fakeClock ) NewTimer (d time.Duration ) Timer {
169
+ stopped := uint32 (0 )
170
+ if d <= 0 {
171
+ stopped = 1
172
+ }
173
+ ft := & fakeTimer {
174
+ c : make (chan time.Time , 1 ),
175
+ stop : make (chan struct {}, 1 ),
176
+ reset : make (chan reset , 1 ),
177
+ clock : fc ,
178
+ stopped : stopped ,
179
+ }
180
+
181
+ ft .run (d )
182
+ return ft
183
+ }
184
+
159
185
// Advance advances fakeClock to a new point in time, ensuring channels from any
160
186
// previous invocations of After are notified appropriately before returning
161
187
func (fc * fakeClock ) Advance (d time.Duration ) {
0 commit comments