@@ -45,6 +45,7 @@ type fakeClockWaiter struct {
45
45
fired bool
46
46
}
47
47
48
+ // NewFakeClock constructs a fake clock set to the provided time.
48
49
func NewFakeClock (t time.Time ) * FakeClock {
49
50
return & FakeClock {
50
51
time : t ,
@@ -65,7 +66,7 @@ func (f *FakeClock) Since(ts time.Time) time.Duration {
65
66
return f .time .Sub (ts )
66
67
}
67
68
68
- // Fake version of time.After(d).
69
+ // After is the fake version of time.After(d).
69
70
func (f * FakeClock ) After (d time.Duration ) <- chan time.Time {
70
71
f .lock .Lock ()
71
72
defer f .lock .Unlock ()
@@ -78,7 +79,7 @@ func (f *FakeClock) After(d time.Duration) <-chan time.Time {
78
79
return ch
79
80
}
80
81
81
- // Fake version of time.NewTimer(d).
82
+ // NewTimer constructs a fake timer, akin to time.NewTimer(d).
82
83
func (f * FakeClock ) NewTimer (d time.Duration ) clock.Timer {
83
84
f .lock .Lock ()
84
85
defer f .lock .Unlock ()
@@ -95,7 +96,11 @@ func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
95
96
return timer
96
97
}
97
98
99
+ // Tick constructs a fake ticker, akin to time.Tick
98
100
func (f * FakeClock ) Tick (d time.Duration ) <- chan time.Time {
101
+ if d <= 0 {
102
+ return nil
103
+ }
99
104
f .lock .Lock ()
100
105
defer f .lock .Unlock ()
101
106
tickTime := f .time .Add (d )
@@ -110,14 +115,15 @@ func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
110
115
return ch
111
116
}
112
117
113
- // Move clock by Duration, notify anyone that's called After, Tick, or NewTimer
118
+ // Step moves the clock by Duration and notifies anyone that's called After,
119
+ // Tick, or NewTimer.
114
120
func (f * FakeClock ) Step (d time.Duration ) {
115
121
f .lock .Lock ()
116
122
defer f .lock .Unlock ()
117
123
f .setTimeLocked (f .time .Add (d ))
118
124
}
119
125
120
- // Sets the time.
126
+ // SetTime sets the time.
121
127
func (f * FakeClock ) SetTime (t time.Time ) {
122
128
f .lock .Lock ()
123
129
defer f .lock .Unlock ()
@@ -157,14 +163,15 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
157
163
f .waiters = newWaiters
158
164
}
159
165
160
- // Returns true if After has been called on f but not yet satisfied (so you can
166
+ // HasWaiters returns true if After has been called on f but not yet satisfied (so you can
161
167
// write race-free tests).
162
168
func (f * FakeClock ) HasWaiters () bool {
163
169
f .lock .RLock ()
164
170
defer f .lock .RUnlock ()
165
171
return len (f .waiters ) > 0
166
172
}
167
173
174
+ // Sleep is akin to time.Sleep
168
175
func (f * FakeClock ) Sleep (d time.Duration ) {
169
176
f .Step (d )
170
177
}
@@ -186,24 +193,25 @@ func (i *IntervalClock) Since(ts time.Time) time.Duration {
186
193
return i .Time .Sub (ts )
187
194
}
188
195
189
- // Unimplemented , will panic.
196
+ // After is unimplemented , will panic.
190
197
// TODO: make interval clock use FakeClock so this can be implemented.
191
198
func (* IntervalClock ) After (d time.Duration ) <- chan time.Time {
192
199
panic ("IntervalClock doesn't implement After" )
193
200
}
194
201
195
- // Unimplemented , will panic.
202
+ // NewTimer is unimplemented , will panic.
196
203
// TODO: make interval clock use FakeClock so this can be implemented.
197
204
func (* IntervalClock ) NewTimer (d time.Duration ) clock.Timer {
198
205
panic ("IntervalClock doesn't implement NewTimer" )
199
206
}
200
207
201
- // Unimplemented , will panic.
208
+ // Tick is unimplemented , will panic.
202
209
// TODO: make interval clock use FakeClock so this can be implemented.
203
210
func (* IntervalClock ) Tick (d time.Duration ) <- chan time.Time {
204
211
panic ("IntervalClock doesn't implement Tick" )
205
212
}
206
213
214
+ // Sleep is unimplemented, will panic.
207
215
func (* IntervalClock ) Sleep (d time.Duration ) {
208
216
panic ("IntervalClock doesn't implement Sleep" )
209
217
}
@@ -250,5 +258,17 @@ func (f *fakeTimer) Reset(d time.Duration) bool {
250
258
f .waiter .fired = false
251
259
f .waiter .targetTime = f .fakeClock .time .Add (d )
252
260
261
+ var isWaiting bool
262
+ for i := range f .fakeClock .waiters {
263
+ w := f .fakeClock .waiters [i ]
264
+ if w == & f .waiter {
265
+ isWaiting = true
266
+ break
267
+ }
268
+ }
269
+ if ! isWaiting {
270
+ f .fakeClock .waiters = append (f .fakeClock .waiters , & f .waiter )
271
+ }
272
+
253
273
return active
254
274
}
0 commit comments