/
fake.go
217 lines (186 loc) · 5.38 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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package clock
// Forked from github.com/andres-erbsen/clock to isolate a missing nap.
import (
"container/heap"
"runtime"
"sync"
"time"
)
// FakeClock represents a fake clock that only moves forward programmically.
// It can be preferable to a real-time clock when testing time-based functionality.
type FakeClock struct {
sync.Mutex
now time.Time
timers timers
}
var _ Clock = (*FakeClock)(nil)
// NewFake returns an instance of a fake clock.
// The current time of the fake clock on initialization is the Unix epoch.
func NewFake() *FakeClock {
// Note: Unix(0, 0) is not the zero value for time, and we need the zero
// value to distinguish rate limiters that have not started.
return &FakeClock{now: time.Unix(0, 0)}
}
// Add moves the current time of the fake clock forward by the duration.
// This should only be called from a single goroutine at a time.
func (fc *FakeClock) Add(d time.Duration) {
fc.Lock()
// Calculate the final time.
end := fc.now.Add(d)
fc.flush(end)
if fc.now.Before(end) {
fc.now = end
}
fc.Unlock()
nap()
}
// Set advances the current time of the fake clock to the given absolute time.
func (fc *FakeClock) Set(end time.Time) {
fc.Lock()
fc.flush(end)
if fc.now.Before(end) {
fc.now = end
}
fc.Unlock()
nap()
}
// flush runs all timers before the given end time and is used to run newly
// added timers as well as expired timers in Add().
func (fc *FakeClock) flush(end time.Time) {
for len(fc.timers) > 0 && !fc.timers[0].time.After(end) {
t := fc.timers[0]
heap.Pop(&fc.timers)
if fc.now.Before(t.time) {
fc.now = t.time
}
fc.Unlock()
t.tick()
fc.Lock()
}
}
// FakeTimer produces a timer that will emit a time some duration after now,
// exposing the fake timer internals and type.
func (fc *FakeClock) FakeTimer(d time.Duration) *FakeTimer {
fc.Lock()
defer fc.Unlock()
c := make(chan time.Time, 1)
t := &FakeTimer{
c: c,
clock: fc,
time: fc.now.Add(d),
}
fc.addTimer(t)
return t
}
// Timer produces a timer that will emit a time some duration after now.
func (fc *FakeClock) Timer(d time.Duration) Timer {
return fc.FakeTimer(d)
}
func (fc *FakeClock) addTimer(t *FakeTimer) {
heap.Push(&fc.timers, t)
fc.flush(fc.now)
}
// After produces a channel that will emit the time after a duration passes.
func (fc *FakeClock) After(d time.Duration) <-chan time.Time {
return fc.Timer(d).C()
}
// FakeAfterFunc waits for the duration to elapse and then executes a function.
// A Timer is returned that can be stopped.
func (fc *FakeClock) FakeAfterFunc(d time.Duration, f func()) *FakeTimer {
t := fc.FakeTimer(d)
go func() {
<-t.c
f()
}()
nap()
return t
}
// AfterFunc waits for the duration to elapse and then executes a function.
// A Timer is returned that can be stopped.
func (fc *FakeClock) AfterFunc(d time.Duration, f func()) Timer {
return fc.FakeAfterFunc(d, f)
}
// Now returns the current time on the fake clock.
func (fc *FakeClock) Now() time.Time {
fc.Lock()
defer fc.Unlock()
return fc.now
}
// Sleep pauses the goroutine for the given duration on the fake clock.
// The clock must be moved forward in a separate goroutine.
func (fc *FakeClock) Sleep(d time.Duration) {
<-fc.After(d)
}
// FakeTimer represents a single event.
type FakeTimer struct {
c chan time.Time
time time.Time
clock *FakeClock
index int
}
// C returns a channel that will send the time when it fires.
func (t *FakeTimer) C() <-chan time.Time {
return t.c
}
// tick advances the clock to this timer.
func (t *FakeTimer) tick() {
select {
case t.c <- t.time:
default:
}
nap()
}
// Reset adjusts the timer's scheduled time forward from now, unless it has
// already fired.
func (t *FakeTimer) Reset(d time.Duration) bool {
t.time = t.clock.now.Add(d)
// Empty the channel if already filled.
select {
case <-t.c:
default:
}
if t.index >= 0 {
heap.Fix(&t.clock.timers, t.index)
return true
}
heap.Push(&t.clock.timers, t)
return false
}
// Stop removes a timer from the scheduled timers.
func (t *FakeTimer) Stop() bool {
if t.index < 0 {
return false
}
// Empty the channel if already filled.
select {
case <-t.c:
default:
}
t.clock.timers.Swap(t.index, len(t.clock.timers)-1)
t.clock.timers.Pop()
heap.Fix(&t.clock.timers, t.index)
return true
}
func nap() {
// time.Sleep(time.Millisecond)
runtime.Gosched()
}