forked from juju/testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
279 lines (242 loc) · 7.06 KB
/
clock.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2015-2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package testing
import (
"sort"
"sync"
"time"
"github.com/juju/errors"
"github.com/juju/utils/clock"
)
// timer implements a mock clock.Timer for testing purposes.
type timer struct {
deadline time.Time
clock *Clock
c chan time.Time
// trigger is called when the timer expires. It is
// called with the clock mutex held and will not block.
trigger func()
}
// Reset is part of the clock.Timer interface.
func (t *timer) Reset(d time.Duration) bool {
return t.clock.reset(t, d)
}
// Stop is part of the clock.Timer interface.
func (t *timer) Stop() bool {
return t.clock.stop(t)
}
// Chan is part of the clock.Timer interface.
func (t *timer) Chan() <-chan time.Time {
return t.c
}
// Clock implements a mock clock.Clock for testing purposes.
type Clock struct {
mu sync.Mutex
now time.Time
waiting []*timer // timers waiting to fire, sorted by deadline.
notifyAlarms chan struct{}
}
// NewClock returns a new clock set to the supplied time. If your SUT needs to
// call After, AfterFunc, NewTimer or Timer.Reset more than 10000 times: (1)
// you have probably written a bad test; and (2) you'll need to read from the
// Alarms chan to keep the buffer clear.
func NewClock(now time.Time) *Clock {
return &Clock{
now: now,
notifyAlarms: make(chan struct{}, 10000),
}
}
// Now is part of the clock.Clock interface.
func (clock *Clock) Now() time.Time {
clock.mu.Lock()
defer clock.mu.Unlock()
return clock.now
}
// After is part of the clock.Clock interface.
func (clock *Clock) After(d time.Duration) <-chan time.Time {
return clock.NewTimer(d).Chan()
}
func (clock *Clock) NewTimer(d time.Duration) clock.Timer {
c := make(chan time.Time, 1)
return clock.addAlarm(d, c, func() {
c <- clock.now
})
}
// AfterFunc is part of the clock.Clock interface.
func (clock *Clock) AfterFunc(d time.Duration, f func()) clock.Timer {
return clock.addAlarm(d, nil, func() {
go f()
})
}
func (clock *Clock) addAlarm(d time.Duration, c chan time.Time, trigger func()) *timer {
defer clock.notifyAlarm()
clock.mu.Lock()
defer clock.mu.Unlock()
t := &timer{
c: c,
deadline: clock.now.Add(d),
clock: clock,
trigger: trigger,
}
clock.addTimer(t)
clock.triggerAll()
return t
}
// Advance advances the result of Now by the supplied duration, and sends
// the "current" time on all alarms which are no longer "in the future".
func (clock *Clock) Advance(d time.Duration) {
clock.mu.Lock()
defer clock.mu.Unlock()
clock.now = clock.now.Add(d)
if len(clock.waiting) == 0 {
logger.Debugf("advancing a clock that has nothing waiting: cf. https://github.com/juju/juju/wiki/Intermittent-failures")
}
clock.triggerAll()
}
// WaitAdvance functions the same as Advance, but only if there is n timers in
// clock.waiting. This came about while fixing lp:1607044 intermittent
// failures. It turns out that testing.Clock.Advance might advance the time
// and trigger notifications before triggers are set. So we wait a limited time
// 'w' for 'n' timers to show up in clock.waiting, and if they do we advance
// 'd'.
func (clock *Clock) WaitAdvance(d, w time.Duration, n int) error {
if w == 0 {
w = time.Second
}
pause := w / 10
for i := 0; i < 10; i++ {
if clock.hasNWaiters(n) {
clock.Advance(d)
return nil
}
time.Sleep(pause)
}
clock.mu.Lock()
got := len(clock.waiting)
clock.mu.Unlock()
return errors.Errorf(
"got %d timers added after waiting %s: wanted %d", got, w.String(), n)
}
// hasNWaiters checks if the clock currently has 'n' timers waiting to fire.
func (clock *Clock) hasNWaiters(n int) bool {
clock.mu.Lock()
hasWaiters := len(clock.waiting) == n
clock.mu.Unlock()
return hasWaiters
}
// Alarms returns a channel on which you can read one value for every call to
// After and AfterFunc; and for every successful Timer.Reset backed by this
// Clock. It might not be elegant but it's necessary when testing time logic
// that runs on a goroutine other than that of the test.
func (clock *Clock) Alarms() <-chan struct{} {
return clock.notifyAlarms
}
// triggerAll triggers any alarms that are currently due and removes them
// from clock.waiting.
func (clock *Clock) triggerAll() {
triggered := 0
for _, t := range clock.waiting {
if clock.now.Before(t.deadline) {
break
}
t.trigger()
triggered++
}
clock.waiting = clock.waiting[triggered:]
}
// reset is the underlying implementation of clock.Timer.Reset, which may be
// called by any Timer backed by this Clock.
func (clock *Clock) reset(t *timer, d time.Duration) bool {
defer clock.notifyAlarm()
clock.mu.Lock()
defer clock.mu.Unlock()
found := false
for _, wt := range clock.waiting {
if wt == t {
found = true
}
}
if !found {
clock.waiting = append(clock.waiting, t)
}
t.deadline = clock.now.Add(d)
sort.Sort(byDeadline(clock.waiting))
return found
}
// stop is the underlying implementation of clock.Timer.Reset, which may be
// called by any Timer backed by this Clock.
func (clock *Clock) stop(t *timer) bool {
clock.mu.Lock()
defer clock.mu.Unlock()
for i, wt := range clock.waiting {
if wt == t {
clock.waiting = removeFromSlice(clock.waiting, i)
return true
}
}
return false
}
// addTimer adds an alarm at time t.
func (clock *Clock) addTimer(t *timer) {
clock.waiting = append(clock.waiting, t)
sort.Sort(byDeadline(clock.waiting))
}
// notifyAlarm sends a value on the channel exposed by Alarms().
func (clock *Clock) notifyAlarm() {
select {
case clock.notifyAlarms <- struct{}{}:
default:
panic("alarm notification buffer full")
}
}
// byDeadline is used to sort alarms by time.
type byDeadline []*timer
func (a byDeadline) Len() int { return len(a) }
func (a byDeadline) Less(i, j int) bool { return a[i].deadline.Before(a[j].deadline) }
func (a byDeadline) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// removeFromSlice removes item at the specified index from the slice.
func removeFromSlice(sl []*timer, index int) []*timer {
return append(sl[:index], sl[index+1:]...)
}
type StubClock struct {
*Stub
ReturnNow time.Time
ReturnAfter <-chan time.Time
ReturnAfterFunc clock.Timer
}
func NewStubClock(stub *Stub) *StubClock {
return &StubClock{
Stub: stub,
}
}
func (s *StubClock) Now() time.Time {
s.AddCall("Now")
s.NextErr() // pop one off
return s.ReturnNow
}
func (s *StubClock) After(d time.Duration) <-chan time.Time {
s.AddCall("After", d)
s.NextErr() // pop one off
return s.ReturnAfter
}
func (s *StubClock) AfterFunc(d time.Duration, f func()) clock.Timer {
s.AddCall("AfterFunc", d, f)
s.NextErr() // pop one off
return s.ReturnAfterFunc
}
// AutoAdvancingClock wraps a clock.Clock, calling the Advance
// function whenever After or AfterFunc are called.
type AutoAdvancingClock struct {
clock.Clock
Advance func(time.Duration)
}
func (c *AutoAdvancingClock) After(d time.Duration) <-chan time.Time {
ch := c.Clock.After(d)
c.Advance(d)
return ch
}
func (c *AutoAdvancingClock) AfterFunc(d time.Duration, f func()) clock.Timer {
t := c.Clock.AfterFunc(d, f)
c.Advance(d)
return t
}