-
Notifications
You must be signed in to change notification settings - Fork 272
/
mock.go
186 lines (151 loc) · 3.38 KB
/
mock.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
package clock
import (
"fmt"
"sync"
"time"
)
// Mock exists to allow easier mocking of Clock interface.
type Mock struct {
mu sync.RWMutex
time time.Time
tickers map[*mockTicker]struct{}
}
var _ Clock = &Mock{}
// NewMock returns a mocked instance of a Clock.
func NewMock() *Mock {
return &Mock{
mu: sync.RWMutex{},
time: time.Time{},
tickers: map[*mockTicker]struct{}{},
}
}
// Set adjusts the current time.
func (m *Mock) Set(now time.Time) {
m.mu.Lock()
m.set(now)
m.mu.Unlock()
}
func (m *Mock) set(now time.Time) {
start := m.time
m.time = now
diff := now.Sub(start)
if diff < 0 {
panic(fmt.Sprintf("diff cannot be less than zero: %d", diff))
}
for ticker := range m.tickers {
for ts := ticker.getStart().Add(ticker.d); !ts.After(now) && !ticker.isStopped(); ts = ts.Add(ticker.d) {
ticker.send(ts)
}
}
}
// Add adds the d to current time and sets the time.
func (m *Mock) Add(d time.Duration) time.Time {
m.mu.Lock()
ts := m.time.Add(d)
m.set(ts)
m.mu.Unlock()
return ts
}
// Now implements the Clock interface.
func (m *Mock) Now() time.Time {
m.mu.RLock()
ts := m.time
m.mu.RUnlock()
return ts
}
// Since implements the Clock interface.
func (m *Mock) Since(ts time.Time) time.Duration {
return m.Now().Sub(ts)
}
// NewTicker implements the Clock interface.
func (m *Mock) NewTicker(d time.Duration) Ticker {
return &tickerWrapper{
mockTicker: m.newTicker(d, false),
}
}
// NewTimer implements the Clock interface.
func (m *Mock) NewTimer(d time.Duration) Timer {
return m.newTicker(d, true)
}
// tickerWrapper just wraps a ticker into an interface that satisfies Ticker,
// because Stop method returns a boolean.
type tickerWrapper struct {
*mockTicker
}
// Stop calls Stop on the mockTicker but does not return anything.
func (t *tickerWrapper) Stop() {
t.mockTicker.Stop()
}
func (m *Mock) newTicker(d time.Duration, timer bool) *mockTicker {
m.mu.Lock()
ticker := &mockTicker{
timer: timer,
c: make(chan time.Time, 1),
d: d,
mock: m,
start: m.time,
stopped: false,
}
m.tickers[ticker] = struct{}{}
m.mu.Unlock()
return ticker
}
type mockTicker struct {
// timer is set to false when this mock represents a Ticker and true when
// it represents a ticker.
timer bool
// mock is used for getting the current time when resetting the timer.
mock *Mock
// mu protects d, start and stopped.
mu sync.Mutex
d time.Duration
start time.Time
stopped bool
c chan time.Time
}
// C implements the Ticker and Timer interfaces.
func (m *mockTicker) C() <-chan time.Time {
return m.c
}
func (m *mockTicker) getStart() time.Time {
m.mu.Lock()
start := m.start
m.mu.Unlock()
return start
}
func (m *mockTicker) send(ts time.Time) {
m.mu.Lock()
select {
case m.c <- ts:
default:
}
if m.timer {
// Timers get stopped after first use, but tickers don't.
m.stopped = true
}
m.start = ts
m.mu.Unlock()
}
// Stop implements the Ticker and Timer interfaces.
func (m *mockTicker) Stop() bool {
m.mu.Lock()
justStopped := !m.stopped
m.stopped = true
m.mu.Unlock()
return justStopped
}
func (m *mockTicker) isStopped() bool {
m.mu.Lock()
stopped := m.stopped
m.mu.Unlock()
return stopped
}
// Reset implements the Ticker and Timer interfaces.
func (m *mockTicker) Reset(d time.Duration) {
now := m.mock.Now()
m.mu.Lock()
m.start = now
m.d = d
m.stopped = false
m.mu.Unlock()
}