This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtime.go
212 lines (181 loc) · 4.51 KB
/
time.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
package neo
import (
"sort"
"sync"
"time"
)
// Timer abstracts a single event.
type Timer interface {
C() <-chan time.Time
Stop() bool
Reset(d time.Duration)
}
// Ticker abstracts a channel that delivers ``ticks'' of a clock at intervals.
type Ticker interface {
C() <-chan time.Time
Stop()
Reset(d time.Duration)
}
// NewTime returns new temporal simulator.
func NewTime(now time.Time) *Time {
return &Time{
now: now,
moments: map[int]moment{},
}
}
// Time simulates temporal interactions.
//
// All methods are goroutine-safe.
type Time struct {
// mux guards internal state. Note that all methods without Unlocked
// suffix acquire mux.
mux sync.Mutex
now time.Time
momentID int
moments map[int]moment
observers []chan struct{}
}
func (t *Time) Timer(d time.Duration) Timer {
tt := &timer{
time: t,
ch: make(chan time.Time, 1),
}
tt.id = t.plan(t.When(d), tt.do)
return tt
}
func (t *Time) Ticker(d time.Duration) Ticker {
tt := &ticker{
time: t,
ch: make(chan time.Time, 1),
dur: d,
}
tt.id = t.plan(t.When(d), tt.do)
return tt
}
func (t *Time) planUnlocked(when time.Time, do func(now time.Time)) int {
id := t.momentID
t.momentID++
t.moments[id] = moment{
when: when,
do: do,
}
t.observeUnlocked()
return id
}
func (t *Time) plan(when time.Time, do func(now time.Time)) int {
t.mux.Lock()
defer t.mux.Unlock()
return t.planUnlocked(when, do)
}
// stop removes the moment with the given ID from the list of scheduled moments.
// It returns true if a moment existed for the given ID, otherwise it is no-op.
func (t *Time) stop(id int) bool {
t.mux.Lock()
defer t.mux.Unlock()
_, ok := t.moments[id]
delete(t.moments, id)
return ok
}
// reset adjusts the moment with the given ID to run after the d duration. It
// creates a new moment if the moment does not already exist. If durp pointer
// is not nil, it is updated with d value while reset is holding Time’s lock.
func (t *Time) reset(d time.Duration, id int, do func(now time.Time), durp *time.Duration) {
t.mux.Lock()
defer t.mux.Unlock()
t.resetUnlocked(d, id, do, durp)
}
// resetUnlocked is like reset but does not acquire the Time’s lock.
func (t *Time) resetUnlocked(d time.Duration, id int, do func(now time.Time), durp *time.Duration) {
if durp != nil {
*durp = d
}
m, ok := t.moments[id]
if !ok {
m = moment{do: do}
}
m.when = t.now.Add(d)
t.moments[id] = m
}
// tickUnlocked applies all scheduled temporal effects.
func (t *Time) tickUnlocked() moments {
var past moments
for id, m := range t.moments {
if m.when.After(t.now) {
continue
}
delete(t.moments, id)
past = append(past, m)
}
sort.Sort(past)
return past
}
// Now returns the current time.
func (t *Time) Now() time.Time {
t.mux.Lock()
defer t.mux.Unlock()
return t.now
}
// Set travels to specified time.
//
// Also triggers temporal effects.
func (t *Time) Set(now time.Time) {
t.mux.Lock()
defer t.mux.Unlock()
t.setUnlocked(now)
}
// Travel adds duration to current time and returns result.
//
// Also triggers temporal effects.
func (t *Time) Travel(d time.Duration) time.Time {
t.mux.Lock()
defer t.mux.Unlock()
now := t.now.Add(d)
t.setUnlocked(now)
return now
}
// TravelDate applies AddDate to current time and returns result.
//
// Also triggers temporal effects.
func (t *Time) TravelDate(years, months, days int) time.Time {
t.mux.Lock()
defer t.mux.Unlock()
now := t.now.AddDate(years, months, days)
t.setUnlocked(now)
return now
}
// setUnlocked sets the current time to the given now time and triggers temporal
// effects.
func (t *Time) setUnlocked(now time.Time) {
t.now = now
t.tickUnlocked().do(now)
}
// Sleep blocks until duration is elapsed.
func (t *Time) Sleep(d time.Duration) { <-t.After(d) }
// When returns relative time point.
func (t *Time) When(d time.Duration) time.Time {
return t.Now().Add(d)
}
// After returns new channel that will receive time.Time value with current tme after
// specified duration.
func (t *Time) After(d time.Duration) <-chan time.Time {
done := make(chan time.Time, 1)
t.plan(t.When(d), func(now time.Time) {
done <- now
})
return done
}
// Observe return channel that closes on clock calls. The current implementation
// also closes the channel on Ticker’s ticks.
func (t *Time) Observe() <-chan struct{} {
observer := make(chan struct{})
t.mux.Lock()
t.observers = append(t.observers, observer)
t.mux.Unlock()
return observer
}
func (t *Time) observeUnlocked() {
for _, observer := range t.observers {
close(observer)
}
t.observers = t.observers[:0]
}