-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.go
243 lines (181 loc) · 5.65 KB
/
simulator.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
package chrono
import (
"context"
"sync"
"time"
)
// Simulator simulates time. Allows to have a control over what an when is executed.
// Upon start, initial events must be placed into queue using AfterFunc, UntilFunc or EveryFunc methods.
// Then, the time can be advanced using Advance or ProcessAll methods.
// Tasks are ran in their chronological order. They can generate additional tasks.
// Simulation happens in a single thread, but tasks can be scheduled from different threads.
func NewSimulator(now time.Time) *Simulator {
return NewSimulatorWithOpts(now, nil)
}
// Usage lock is lock used to push and pop tasks to/from the task queue,
// and also to check current time. If nil - it is regulat sync.RWMutex.
// Pass NoLock to disable locking if you are sure that all calls are made from the same goroutine.
// There is no simulator lock for the sake of simplicity.
func NewSimulatorWithOpts(now time.Time, usageLock RWLocker) *Simulator {
if usageLock == nil {
usageLock = &sync.RWMutex{}
}
return &Simulator{
now: now,
taskQueue: newTaskQueue(),
usageLock: usageLock,
}
}
type Simulator struct {
usageLock RWLocker
now time.Time
taskQueue *taskQueue
}
var _ Clock = &Simulator{}
func (s *Simulator) Now() time.Time {
s.usageLock.RLock()
defer s.usageLock.RUnlock()
return s.now
}
func (s *Simulator) SetNow(now time.Time) (time.Time, time.Duration) {
s.usageLock.RLock()
defer s.usageLock.RUnlock()
return s.setNow(now)
}
func (s *Simulator) setNow(newNow time.Time) (time.Time, time.Duration) {
leap := newNow.Sub(s.now)
if leap > 0 {
s.now = newNow
}
return s.now, leap
}
// Sets the current time to the next task deadlin, but does not run the task.
func (s *Simulator) Approach() (newNow time.Time, leap time.Duration, hasTasks bool) {
s.usageLock.Lock()
defer s.usageLock.Unlock()
if !s.taskQueue.HasTasks() {
return s.now, 0, false
}
nextTask := s.taskQueue.PeekTask()
newNow, leap = s.setNow(nextTask.Deadline)
return newNow, leap, true
}
// Advances the current time to the next task deadline and runs the task.
func (s *Simulator) Advance() (newNow time.Time, leap time.Duration, hadTasks bool) {
return s.AdvanceIfBefore(time.Time{})
}
// Advances the current time to the next task deadline and runs the task if it is before the specified time.
// If there are no tasks or its deadline comes not specified time, the current time is NOT changed.
func (s *Simulator) AdvanceIfBefore(before time.Time) (newNow time.Time, leap time.Duration, hadExpiredTasks bool) {
s.usageLock.Lock()
if !s.taskQueue.HasTasks() {
s.usageLock.Unlock()
return s.now, 0, false
}
if !before.IsZero() {
nextTask := s.taskQueue.PeekTask()
if !nextTask.Deadline.Before(before) {
s.usageLock.Unlock()
return s.now, 0, false
}
}
newNow, leap = s.processNextTask(false)
return newNow, leap, true
}
// Processes all tasks.
// WARNING: If you have periodic tasks, this method will run until you explicitly stop them.
func (s *Simulator) ProcessAll(ctx context.Context) (int, error) {
return s.ProcessAllUntil(ctx, time.Time{})
}
// Processes all tasks, which are set to fire before the specified time (not including).
func (s *Simulator) ProcessAllUntil(ctx context.Context, until time.Time) (int, error) {
tasksProcessed := 0
for ctx.Err() == nil {
_, _, hadExpiredTasks := s.AdvanceIfBefore(until)
if !hadExpiredTasks {
return tasksProcessed, nil
}
tasksProcessed++
}
return tasksProcessed, ctx.Err()
}
func (s *Simulator) HasExpiredTasks(before time.Time) bool {
s.usageLock.RLock()
defer s.usageLock.RUnlock()
return s.taskQueue.HasExpiredTasks(before)
}
// Returns all the pending tasks, and clears the task queue.
func (s *Simulator) PopAllTasks() []*Task {
s.usageLock.Lock()
defer s.usageLock.Unlock()
tasks := s.taskQueue
s.taskQueue = newTaskQueue()
return []*Task(*tasks)
}
func (s *Simulator) processNextTask(keepLock bool) (time.Time, time.Duration) {
nextTask := s.taskQueue.PopTask()
now, leap := s.setNow(nextTask.Deadline)
s.usageLock.Unlock()
followingTask := nextTask.Run(now)
if followingTask != nil {
s.usageLock.Lock()
s.taskQueue.PushTask(followingTask)
if !keepLock {
s.usageLock.Unlock()
}
} else if keepLock {
s.usageLock.Lock()
}
return now, leap
}
func (t *Simulator) removeTask(task *Task) (taskWasActive bool) {
t.usageLock.Lock()
defer t.usageLock.Unlock()
if !task.IsPending() {
return false
}
t.taskQueue.RemoveTask(task)
return true
}
func (t *Simulator) resetTask(task *Task, d time.Duration) (wasPending bool) {
t.usageLock.Lock()
defer t.usageLock.Unlock()
isPending := task.IsPending()
if isPending {
t.taskQueue.RemoveTask(task)
}
task.Deadline = t.now.Add(d)
t.taskQueue.PushTask(task)
return isPending
}
func (s *Simulator) Since(t time.Time) time.Duration {
s.usageLock.RLock()
defer s.usageLock.RUnlock()
return s.now.Sub(t)
}
func (s *Simulator) Until(t time.Time) time.Duration {
s.usageLock.RLock()
defer s.usageLock.RUnlock()
return t.Sub(s.now)
}
func (s *Simulator) AfterFunc(d time.Duration, f func(now time.Time)) Timer {
s.usageLock.Lock()
defer s.usageLock.Unlock()
timer, timerTask := newSimTimer(s, s.now.Add(d), f)
s.taskQueue.PushTask(timerTask)
return timer
}
func (s *Simulator) UntilFunc(t time.Time, f func(now time.Time)) Timer {
s.usageLock.Lock()
defer s.usageLock.Unlock()
timer, fireTask := newSimTimer(s, t, f)
s.taskQueue.PushTask(fireTask)
return timer
}
func (s *Simulator) EveryFunc(interval time.Duration, f func(now time.Time) bool) Ticker {
s.usageLock.Lock()
defer s.usageLock.Unlock()
ticker, startTask := newSimTicker(s, s.now.Add(interval), interval, f)
s.taskQueue.PushTask(startTask)
return ticker
}