| 1 |
package clock |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"sync" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
type frozenTime struct { |
| 10 |
mu sync.Mutex |
| 11 |
now time.Time |
| 12 |
timers []*frozenTimer |
| 13 |
waiter *waiter |
| 14 |
} |
| 15 |
|
| 16 |
type waiter struct { |
| 17 |
count int |
| 18 |
signalCh chan struct{} |
| 19 |
} |
| 20 |
|
| 21 |
func (ft *frozenTime) Now() time.Time { |
| 22 |
ft.mu.Lock() |
| 23 |
defer ft.mu.Unlock() |
| 24 |
return ft.now |
| 25 |
} |
| 26 |
|
| 27 |
func (ft *frozenTime) Sleep(d time.Duration) { |
| 28 |
<-ft.NewTimer(d).C() |
| 29 |
} |
| 30 |
|
| 31 |
func (ft *frozenTime) After(d time.Duration) <-chan time.Time { |
| 32 |
return ft.NewTimer(d).C() |
| 33 |
} |
| 34 |
|
| 35 |
func (ft *frozenTime) NewTimer(d time.Duration) Timer { |
| 36 |
return ft.AfterFunc(d, nil) |
| 37 |
} |
| 38 |
|
| 39 |
func (ft *frozenTime) AfterFunc(d time.Duration, f func()) Timer { |
| 40 |
t := &frozenTimer{ |
| 41 |
ft: ft, |
| 42 |
when: ft.Now().Add(d), |
| 43 |
f: f, |
| 44 |
} |
| 45 |
if f == nil { |
| 46 |
t.c = make(chan time.Time, 1) |
| 47 |
} |
| 48 |
ft.startTimer(t) |
| 49 |
return t |
| 50 |
} |
| 51 |
|
| 52 |
func (ft *frozenTime) advance(d time.Duration) { |
| 53 |
ft.mu.Lock() |
| 54 |
defer ft.mu.Unlock() |
| 55 |
|
| 56 |
ft.now = ft.now.Add(d) |
| 57 |
for t := ft.nextExpired(); t != nil; t = ft.nextExpired() { |
| 58 |
// Send the timer expiration time to the timer channel if it is |
| 59 |
// defined. But make sure not to block on the send if the channel is |
| 60 |
// full. This behavior will make a ticker skip beats if it readers are |
| 61 |
// not fast enough. |
| 62 |
if t.c != nil { |
| 63 |
select { |
| 64 |
case t.c <- t.when: |
| 65 |
default: |
| 66 |
} |
| 67 |
} |
| 68 |
// If it is a ticking timer then schedule next tick, otherwise mark it |
| 69 |
// as stopped. |
| 70 |
if t.interval != 0 { |
| 71 |
t.when = t.when.Add(t.interval) |
| 72 |
t.stopped = false |
| 73 |
ft.unlockedStartTimer(t) |
| 74 |
} |
| 75 |
// If a function is associated with the timer then call it, but make |
| 76 |
// sure to release the lock for the time of call it is necessary |
| 77 |
// because the lock is not re-entrant but the function may need to |
| 78 |
// start another timer or ticker. |
| 79 |
if t.f != nil { |
| 80 |
func() { |
| 81 |
ft.mu.Unlock() |
| 82 |
defer ft.mu.Lock() |
| 83 |
t.f() |
| 84 |
}() |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func (ft *frozenTime) stopTimer(t *frozenTimer) bool { |
| 90 |
ft.mu.Lock() |
| 91 |
defer ft.mu.Unlock() |
| 92 |
|
| 93 |
if t.stopped { |
| 94 |
return false |
| 95 |
} |
| 96 |
for i, curr := range ft.timers { |
| 97 |
if curr == t { |
| 98 |
t.stopped = true |
| 99 |
copy(ft.timers[i:], ft.timers[i+1:]) |
| 100 |
lastIdx := len(ft.timers) - 1 |
| 101 |
ft.timers[lastIdx] = nil |
| 102 |
ft.timers = ft.timers[:lastIdx] |
| 103 |
return true |
| 104 |
} |
| 105 |
} |
| 106 |
return false |
| 107 |
} |
| 108 |
|
| 109 |
func (ft *frozenTime) nextExpired() *frozenTimer { |
| 110 |
if len(ft.timers) == 0 { |
| 111 |
return nil |
| 112 |
} |
| 113 |
t := ft.timers[0] |
| 114 |
if ft.now.Before(t.when) { |
| 115 |
return nil |
| 116 |
} |
| 117 |
copy(ft.timers, ft.timers[1:]) |
| 118 |
lastIdx := len(ft.timers) - 1 |
| 119 |
ft.timers[lastIdx] = nil |
| 120 |
ft.timers = ft.timers[:lastIdx] |
| 121 |
t.stopped = true |
| 122 |
return t |
| 123 |
} |
| 124 |
|
| 125 |
func (ft *frozenTime) startTimer(t *frozenTimer) { |
| 126 |
ft.mu.Lock() |
| 127 |
defer ft.mu.Unlock() |
| 128 |
|
| 129 |
ft.unlockedStartTimer(t) |
| 130 |
|
| 131 |
if ft.waiter == nil { |
| 132 |
return |
| 133 |
} |
| 134 |
if len(ft.timers) >= ft.waiter.count { |
| 135 |
close(ft.waiter.signalCh) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func (ft *frozenTime) unlockedStartTimer(t *frozenTimer) { |
| 140 |
pos := 0 |
| 141 |
for _, curr := range ft.timers { |
| 142 |
if t.when.Before(curr.when) { |
| 143 |
break |
| 144 |
} |
| 145 |
pos++ |
| 146 |
} |
| 147 |
ft.timers = append(ft.timers, nil) |
| 148 |
copy(ft.timers[pos+1:], ft.timers[pos:]) |
| 149 |
ft.timers[pos] = t |
| 150 |
} |
| 151 |
|
| 152 |
type frozenTimer struct { |
| 153 |
ft *frozenTime |
| 154 |
when time.Time |
| 155 |
interval time.Duration |
| 156 |
stopped bool |
| 157 |
c chan time.Time |
| 158 |
f func() |
| 159 |
} |
| 160 |
|
| 161 |
func (t *frozenTimer) C() <-chan time.Time { |
| 162 |
return t.c |
| 163 |
} |
| 164 |
|
| 165 |
func (t *frozenTimer) Stop() bool { |
| 166 |
return t.ft.stopTimer(t) |
| 167 |
} |
| 168 |
|
| 169 |
func (t *frozenTimer) Reset(d time.Duration) bool { |
| 170 |
active := t.ft.stopTimer(t) |
| 171 |
t.when = t.ft.Now().Add(d) |
| 172 |
t.ft.startTimer(t) |
| 173 |
return active |
| 174 |
} |
| 175 |
|
| 176 |
type frozenTicker struct { |
| 177 |
t *frozenTimer |
| 178 |
} |
| 179 |
|
| 180 |
func (t *frozenTicker) C() <-chan time.Time { |
| 181 |
return t.t.C() |
| 182 |
} |
| 183 |
|
| 184 |
func (t *frozenTicker) Stop() { |
| 185 |
t.t.Stop() |
| 186 |
} |
| 187 |
|
| 188 |
func (ft *frozenTime) NewTicker(d time.Duration) Ticker { |
| 189 |
if d <= 0 { |
| 190 |
panic(errors.New("non-positive interval for NewTicker")) |
| 191 |
} |
| 192 |
t := &frozenTimer{ |
| 193 |
ft: ft, |
| 194 |
when: ft.Now().Add(d), |
| 195 |
interval: d, |
| 196 |
c: make(chan time.Time, 1), |
| 197 |
} |
| 198 |
ft.startTimer(t) |
| 199 |
return &frozenTicker{t} |
| 200 |
} |
| 201 |
|
| 202 |
func (ft *frozenTime) Tick(d time.Duration) <-chan time.Time { |
| 203 |
if d <= 0 { |
| 204 |
return nil |
| 205 |
} |
| 206 |
return ft.NewTicker(d).C() |
| 207 |
} |
| 208 |
|
| 209 |
func (ft *frozenTime) Wait4Scheduled(count int, timeout time.Duration) bool { |
| 210 |
ft.mu.Lock() |
| 211 |
if len(ft.timers) >= count { |
| 212 |
ft.mu.Unlock() |
| 213 |
return true |
| 214 |
} |
| 215 |
if ft.waiter != nil { |
| 216 |
panic("Concurrent call") |
| 217 |
} |
| 218 |
ft.waiter = &waiter{count, make(chan struct{})} |
| 219 |
ft.mu.Unlock() |
| 220 |
|
| 221 |
success := false |
| 222 |
select { |
| 223 |
case <-ft.waiter.signalCh: |
| 224 |
success = true |
| 225 |
case <-time.After(timeout): |
| 226 |
} |
| 227 |
ft.mu.Lock() |
| 228 |
ft.waiter = nil |
| 229 |
ft.mu.Unlock() |
| 230 |
return success |
| 231 |
} |