-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
342 lines (271 loc) · 5.7 KB
/
fake.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package clock
import (
"errors"
"sync"
"time"
)
type sleeper struct {
i int
until time.Time
woke bool
c chan time.Time
f func()
}
func (s *sleeper) wake() {
if s.woke {
return
}
s.woke = true
// if c is set, send the current time
if s.c != nil {
s.c <- s.until
}
// if f is set, call it in separate goroutine
if s.f != nil {
s.f()
}
}
type blocker struct {
n int
done chan struct{}
}
type fakeClock struct {
mutex sync.RWMutex
at time.Time
sleepers []*sleeper
blockers []blocker
}
func NewFakeClock() FakeClock {
return NewFakeClockAt(time.Unix(1, 0))
}
func NewFakeClockAt(at time.Time) FakeClock {
return &fakeClock{
at: at,
}
}
func (clock *fakeClock) Now() time.Time {
clock.mutex.RLock()
defer clock.mutex.RUnlock()
return clock.at
}
func (clock *fakeClock) Since(t time.Time) time.Duration {
return clock.Now().Sub(t)
}
func (clock *fakeClock) Sleep(d time.Duration) {
<-clock.After(d)
}
func (clock *fakeClock) After(d time.Duration) <-chan time.Time {
clock.mutex.Lock()
defer clock.mutex.Unlock()
if d < 0 {
d = 0
}
c := make(chan time.Time, 1)
clock.appendSleeper(&sleeper{
until: clock.at.Add(d),
c: c,
})
return c
}
func (clock *fakeClock) AfterFunc(d time.Duration, f func()) Timer {
clock.mutex.Lock()
defer clock.mutex.Unlock()
timer := &fakeTimer{
clock: clock,
sleeper: sleeper{
until: clock.at.Add(d),
f: func() { go f() },
},
}
clock.appendSleeper(&timer.sleeper)
return timer
}
type fakeTimer struct {
clock *fakeClock
stopped bool
sleeper sleeper
}
func (clock *fakeClock) NewTimer(d time.Duration) Timer {
return &fakeTimer{
clock: clock,
sleeper: sleeper{
i: -1,
until: clock.Now().Add(d),
c: make(chan time.Time, 1),
},
}
}
func (timer *fakeTimer) C() <-chan time.Time {
clock := timer.clock
clock.mutex.Lock()
defer clock.mutex.Unlock()
sleeper := &timer.sleeper
if sleeper.i < 0 {
clock.appendSleeper(sleeper)
}
return sleeper.c
}
func (timer *fakeTimer) Stop() bool {
clock := timer.clock
clock.mutex.Lock()
defer clock.mutex.Unlock()
defer func() { timer.stopped = true }()
if timer.stopped {
return false
}
sleeper := &timer.sleeper
if clock.removeSleeper(sleeper) {
return true
}
return sleeper.until.After(clock.at)
}
func (timer *fakeTimer) Reset(d time.Duration) bool {
clock := timer.clock
clock.mutex.Lock()
defer clock.mutex.Unlock()
sleeper := &timer.sleeper
if d < 0 {
d = 0
}
sleeper.until = timer.clock.at.Add(d)
sleeper.woke = false
sleeper.c = make(chan time.Time, 1)
defer func() {
if sleeper.f != nil {
clock.appendSleeper(&timer.sleeper)
}
}()
return clock.removeSleeper(sleeper)
}
type fakeTicker struct {
clock *fakeClock
interval time.Duration
next time.Time
stopped bool
sleeper *sleeper
}
var errNonPositiveInterval = errors.New("non-positive interval for NewTicker")
func (clock *fakeClock) NewTicker(d time.Duration) Ticker {
if d <= 0 {
panic(errNonPositiveInterval)
}
return &fakeTicker{
clock: clock,
interval: d,
next: clock.Now().Add(d),
sleeper: &sleeper{
i: -1,
},
}
}
func (ticker *fakeTicker) C() <-chan time.Time {
clock := ticker.clock
clock.mutex.Lock()
defer clock.mutex.Unlock()
c := make(chan time.Time, 1)
if ticker.stopped {
return c
}
ticker.sleeper = &sleeper{
until: ticker.next,
c: c,
}
clock.appendSleeper(ticker.sleeper)
ticker.next = ticker.next.Add(ticker.interval)
return c
}
func (ticker *fakeTicker) Stop() {
clock := ticker.clock
clock.mutex.Lock()
defer clock.mutex.Unlock()
ticker.stopped = true
clock.removeSleeper(ticker.sleeper)
}
func (ticker *fakeTicker) Reset(d time.Duration) {
ticker.Stop()
ticker.stopped = false
ticker.interval = d
ticker.sleeper.until = ticker.clock.Now().Add(d)
}
func (clock *fakeClock) Tick(d time.Duration) func() <-chan time.Time {
if d <= 0 {
return func() <-chan time.Time { return nil }
}
return clock.NewTicker(d).C
}
func (clock *fakeClock) Advance(d time.Duration) {
clock.mutex.Lock()
defer clock.mutex.Unlock()
// time travel is not allowed
if d <= 0 {
return
}
clock.at = clock.at.Add(d)
clock.checkSleepers()
}
func (clock *fakeClock) Until(n int) <-chan struct{} {
clock.mutex.Lock()
defer clock.mutex.Unlock()
done := make(chan struct{})
if len(clock.sleepers) >= n {
close(done)
return done
}
clock.appendBlocker(blocker{
n: n,
done: done,
})
return done
}
func (clock *fakeClock) BlockUntil(n int) {
<-clock.Until(n)
}
func (clock *fakeClock) appendSleeper(s *sleeper) {
if !clock.at.Before(s.until) {
s.i = -1
s.wake()
return
}
s.i = len(clock.sleepers)
clock.sleepers = append(clock.sleepers, s)
clock.checkBlockers()
}
func (clock *fakeClock) removeSleeper(s *sleeper) bool {
i := s.i
if i < 0 {
return false
}
// Replace the sleeper with the last sleeper
clock.sleepers[i] = clock.sleepers[len(clock.sleepers)-1]
// Update the replacing sleeper's i
clock.sleepers[i].i = i
// nil out the last reference
clock.sleepers[len(clock.sleepers)-1] = nil
// make the sleeper index negative
s.i = -1
// Shrink the sleeper slice
clock.sleepers = clock.sleepers[:len(clock.sleepers)-1]
return true
}
func (clock *fakeClock) checkSleepers() {
oldSleepers := clock.sleepers
clock.sleepers = clock.sleepers[:0]
for _, sleeper := range oldSleepers {
clock.appendSleeper(sleeper)
}
}
func (clock *fakeClock) appendBlocker(b blocker) {
clock.blockers = append(clock.blockers, b)
}
func (clock *fakeClock) checkBlockers() {
n := 0
for _, blocker := range clock.blockers {
if len(clock.sleepers) < blocker.n {
clock.blockers[n] = blocker
n++
} else {
close(blocker.done)
}
}
clock.blockers = clock.blockers[:n]
}