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
|
// Copyright 2022 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testclock
import (
"sync"
"sync/atomic"
"time"
"github.com/juju/clock"
)
// NewDilatedWallClock returns a clock that can be sped up or slowed down.
// realSecondDuration is the real duration of a second.
func NewDilatedWallClock(realSecondDuration time.Duration) AdvanceableClock {
dc := &dilationClock{
epoch: time.Now(),
realSecondDuration: realSecondDuration,
offsetChanged: make(chan any),
}
dc.offsetChangedCond = sync.NewCond(dc.offsetChangedMutex.RLocker())
return dc
}
type dilationClock struct {
// offsetAtomic is the current dilated offset to allow for time jumps/advances.
// Must be the first item in the struct to ensure proper 64-bit alignment as
// documented at https://pkg.go.dev/sync/atomic#pkg-note-BUG
offsetAtomic int64
epoch time.Time
realSecondDuration time.Duration
// offsetChanged is a channel that is closed when timers need to be signaled
// that there is a offset change coming.
offsetChanged chan any
// offsetChangedMutex is a mutex protecting the offsetChanged and is used by
// the offsetChangedCond.
offsetChangedMutex sync.RWMutex
// offsetChangedCond is used to signal timers that they may try to pull the new
// offset.
offsetChangedCond *sync.Cond
}
// Now is part of the Clock interface.
func (dc *dilationClock) Now() time.Time {
dt, _ := dc.nowWithOffset()
return dt
}
func (dc *dilationClock) nowWithOffset() (time.Time, time.Duration) {
offset := time.Duration(atomic.LoadInt64(&dc.offsetAtomic))
realNow := time.Now()
dt := dilateTime(dc.epoch, realNow, dc.realSecondDuration, offset)
return dt, offset
}
// After implements Clock.After
func (dc *dilationClock) After(d time.Duration) <-chan time.Time {
t := newDilatedWallTimer(dc, d, nil)
return t.c
}
// AfterFunc implements Clock.AfterFunc
func (dc *dilationClock) AfterFunc(d time.Duration, f func()) clock.Timer {
return newDilatedWallTimer(dc, d, f)
}
// NewTimer implements Clock.NewTimer
func (dc *dilationClock) NewTimer(d time.Duration) clock.Timer {
return newDilatedWallTimer(dc, d, nil)
}
// Advance implements AdvanceableClock.Advance
func (dc *dilationClock) Advance(d time.Duration) {
close(dc.offsetChanged)
dc.offsetChangedMutex.Lock()
dc.offsetChanged = make(chan any)
atomic.AddInt64(&dc.offsetAtomic, int64(d))
dc.offsetChangedCond.Broadcast()
dc.offsetChangedMutex.Unlock()
}
// dilatedWallTimer implements the Timer interface.
type dilatedWallTimer struct {
timer *time.Timer
dc *dilationClock
c chan time.Time
target time.Time
offset time.Duration
after func()
done chan any
resetChan chan resetReq
resetMutex sync.Mutex
stopChan chan chan bool
}
type resetReq struct {
d time.Duration
r chan bool
}
func newDilatedWallTimer(dc *dilationClock, d time.Duration, after func()) *dilatedWallTimer {
t := &dilatedWallTimer{
dc: dc,
c: make(chan time.Time),
resetChan: make(chan resetReq),
stopChan: make(chan chan bool),
}
t.start(d, after)
return t
}
func (t *dilatedWallTimer) start(d time.Duration, after func()) {
t.dc.offsetChangedMutex.RLock()
dialatedNow, offset := t.dc.nowWithOffset()
realDuration := time.Duration(float64(d) * t.dc.realSecondDuration.Seconds())
t.target = dialatedNow.Add(d)
t.timer = time.NewTimer(realDuration)
t.offset = offset
t.after = after
t.done = make(chan any)
go t.run()
}
func (t *dilatedWallTimer) run() {
defer t.dc.offsetChangedMutex.RUnlock()
defer close(t.done)
var sendChan chan time.Time
var sendTime time.Time
for {
select {
case reset := <-t.resetChan:
realNow := time.Now()
dialatedNow := dilateTime(t.dc.epoch, realNow, t.dc.realSecondDuration, t.offset)
realDuration := time.Duration(float64(reset.d) * t.dc.realSecondDuration.Seconds())
t.target = dialatedNow.Add(reset.d)
sendChan = nil
sendTime = time.Time{}
reset.r <- t.timer.Reset(realDuration)
case stop := <-t.stopChan:
stop <- t.timer.Stop()
return
case tt := <-t.timer.C:
if t.after != nil {
t.after()
return
}
if sendChan != nil {
panic("reset should have been called")
}
sendChan = t.c
sendTime = dilateTime(t.dc.epoch, tt, t.dc.realSecondDuration, t.offset)
case sendChan <- sendTime:
sendChan = nil
sendTime = time.Time{}
return
case <-t.dc.offsetChanged:
t.dc.offsetChangedCond.Wait()
newOffset := time.Duration(atomic.LoadInt64(&t.dc.offsetAtomic))
if newOffset == t.offset {
continue
}
t.offset = newOffset
stopped := t.timer.Stop()
if !stopped {
continue
}
realNow := time.Now()
dialatedNow := dilateTime(t.dc.epoch, realNow, t.dc.realSecondDuration, t.offset)
dialatedDuration := t.target.Sub(dialatedNow)
if dialatedDuration <= 0 {
sendChan = t.c
sendTime = dialatedNow
continue
}
realDuration := time.Duration(float64(dialatedDuration) * t.dc.realSecondDuration.Seconds())
t.timer.Reset(realDuration)
}
}
}
// Chan implements Timer.Chan
func (t *dilatedWallTimer) Chan() <-chan time.Time {
return t.c
}
// Chan implements Timer.Reset
func (t *dilatedWallTimer) Reset(d time.Duration) bool {
t.resetMutex.Lock()
defer t.resetMutex.Unlock()
reset := resetReq{
d: d,
r: make(chan bool),
}
select {
case <-t.done:
t.start(d, nil)
return true
case t.resetChan <- reset:
return <-reset.r
}
}
// Chan implements Timer.Stop
func (t *dilatedWallTimer) Stop() bool {
stop := make(chan bool)
select {
case <-t.done:
return false
case t.stopChan <- stop:
return <-stop
}
}
func dilateTime(epoch, realNow time.Time,
realSecondDuration, dilatedOffset time.Duration) time.Time {
delta := realNow.Sub(epoch)
if delta < 0 {
delta = time.Duration(0)
}
return epoch.Add(dilatedOffset).Add(time.Duration(float64(delta) / realSecondDuration.Seconds()))
}
|