-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmock.go
185 lines (166 loc) · 4 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
package clock
import (
"context"
"sync"
"time"
)
type mockTimers interface {
start(t *mockTimer)
stop(t *mockTimer)
reset(t *mockTimer)
next() *mockTimer
len() int
}
// Mock implements a Clock that only moves with Add, AddNext and Set.
//
// The clock can be suspended with Lock and resumed with Unlock.
// While suspended, all attempts to use the API will block.
//
// To increase predictability, all Mock methods acquire
// and release the Mutex only once during their execution.
type Mock struct {
sync.Mutex
now time.Time
mockTimers
}
// NewMock returns a new Mock with current time set to now.
//
// Use Realtime to get the real-time Clock.
func NewMock(now time.Time) *Mock {
return &Mock{
now: now,
mockTimers: &timerHeap{},
}
}
// Add advances the current time by duration d and fires all expired timers.
//
// Returns the new current time.
// To increase predictability and speed, Tickers are ticked only once per call.
func (m *Mock) Add(d time.Duration) time.Time {
m.Lock()
defer m.Unlock()
now, _ := m.set(m.now.Add(d))
return now
}
// AddNext advances the current time to the next available timer deadline
// and fires all expired timers.
//
// Returns the new current time and the advanced duration.
func (m *Mock) AddNext() (time.Time, time.Duration) {
m.Lock()
defer m.Unlock()
t := m.next()
if t == nil {
return m.now, 0
}
return m.set(t.deadline)
}
// Set advances the current time to t and fires all expired timers.
//
// Returns the advanced duration.
// To increase predictability and speed, Tickers are ticked only once per call.
func (m *Mock) Set(t time.Time) time.Duration {
m.Lock()
defer m.Unlock()
_, d := m.set(t)
return d
}
func (m *Mock) set(now time.Time) (time.Time, time.Duration) {
cur := m.now
for {
t := m.next()
if t == nil || t.deadline.After(now) {
m.now = now
return m.now, m.now.Sub(cur)
}
m.now = t.deadline
if d := t.fire(); d == 0 {
// Timers are always stopped.
m.stop(t)
} else {
// Ticker's next deadline is set to the first tick after the new now.
dd := (now.Sub(m.now)/d + 1) * d
t.deadline = m.now.Add(dd)
m.reset(t)
}
}
}
// Len returns the number of active timers.
func (m *Mock) Len() int {
m.Lock()
defer m.Unlock()
return m.len()
}
// Now returns the current mocked time.
func (m *Mock) Now() time.Time {
m.Lock()
defer m.Unlock()
return m.now
}
// Since returns the time elapsed since t.
func (m *Mock) Since(t time.Time) time.Duration {
m.Lock()
defer m.Unlock()
return m.now.Sub(t)
}
// Until returns the duration until t.
func (m *Mock) Until(t time.Time) time.Duration {
m.Lock()
defer m.Unlock()
return t.Sub(m.now)
}
// DeadlineContext implements Clock.
func (m *Mock) DeadlineContext(parent context.Context, d time.Time) (context.Context, context.CancelFunc) {
m.Lock()
defer m.Unlock()
return m.deadlineContext(parent, d)
}
// TimeoutContext implements Clock.
func (m *Mock) TimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
m.Lock()
defer m.Unlock()
return m.deadlineContext(parent, m.now.Add(timeout))
}
func (m *Mock) deadlineContext(parent context.Context, deadline time.Time) (context.Context, context.CancelFunc) {
cancelCtx, cancel := context.WithCancel(Context(parent, m))
if pd, ok := parent.Deadline(); ok && !pd.After(deadline) {
return cancelCtx, cancel
}
ctx := &mockCtx{
Context: cancelCtx,
done: make(chan struct{}),
deadline: deadline,
}
t := m.newTimerFunc(deadline, nil)
go func() {
select {
case <-t.C:
ctx.err = context.DeadlineExceeded
case <-cancelCtx.Done():
ctx.err = cancelCtx.Err()
defer t.Stop()
}
close(ctx.done)
}()
return ctx, cancel
}
type mockCtx struct {
context.Context
deadline time.Time
done chan struct{}
err error
}
func (ctx *mockCtx) Deadline() (time.Time, bool) {
return ctx.deadline, true
}
func (ctx *mockCtx) Done() <-chan struct{} {
return ctx.done
}
func (ctx *mockCtx) Err() error {
select {
case <-ctx.done:
return ctx.err
default:
return nil
}
}