| 1 |
package sturdyc |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"sync/atomic" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
// Clock is an abstraction for time.Time package that allows for testing. |
| 10 |
type Clock interface { |
| 11 |
Now() time.Time |
| 12 |
NewTicker(d time.Duration) (<-chan time.Time, func()) |
| 13 |
NewTimer(d time.Duration) (<-chan time.Time, func() bool) |
| 14 |
Since(t time.Time) time.Duration |
| 15 |
} |
| 16 |
|
| 17 |
// RealClock provides functions that wraps the real time.Time package. |
| 18 |
type RealClock struct{} |
| 19 |
|
| 20 |
// NewClock returns a new RealClock. |
| 21 |
func NewClock() *RealClock { |
| 22 |
return &RealClock{} |
| 23 |
} |
| 24 |
|
| 25 |
// Now wraps time.Now() from the standard library. |
| 26 |
func (c *RealClock) Now() time.Time { |
| 27 |
return time.Now() |
| 28 |
} |
| 29 |
|
| 30 |
// NewTicker returns the channel and stop function from the ticker from the standard library. |
| 31 |
func (c *RealClock) NewTicker(d time.Duration) (<-chan time.Time, func()) { |
| 32 |
t := time.NewTicker(d) |
| 33 |
return t.C, t.Stop |
| 34 |
} |
| 35 |
|
| 36 |
// NewTimer returns the channel and stop function from the timer from the standard library. |
| 37 |
func (c *RealClock) NewTimer(d time.Duration) (<-chan time.Time, func() bool) { |
| 38 |
t := time.NewTimer(d) |
| 39 |
return t.C, t.Stop |
| 40 |
} |
| 41 |
|
| 42 |
// Since wraps time.Since() from the standard library. |
| 43 |
func (c *RealClock) Since(t time.Time) time.Duration { |
| 44 |
return time.Since(t) |
| 45 |
} |
| 46 |
|
| 47 |
type testTimer struct { |
| 48 |
deadline time.Time |
| 49 |
ch chan time.Time |
| 50 |
stopped *atomic.Bool |
| 51 |
} |
| 52 |
|
| 53 |
type testTicker struct { |
| 54 |
nextTick time.Time |
| 55 |
interval time.Duration |
| 56 |
ch chan time.Time |
| 57 |
stopped *atomic.Bool |
| 58 |
} |
| 59 |
|
| 60 |
// TestClock is a clock that satisfies the Clock interface. It should only be used for testing. |
| 61 |
type TestClock struct { |
| 62 |
mu sync.Mutex |
| 63 |
time time.Time |
| 64 |
timers []*testTimer |
| 65 |
tickers []*testTicker |
| 66 |
} |
| 67 |
|
| 68 |
// NewTestClock returns a new TestClock with the specified time. |
| 69 |
func NewTestClock(time time.Time) *TestClock { |
| 70 |
var c TestClock |
| 71 |
c.time = time |
| 72 |
c.timers = make([]*testTimer, 0) |
| 73 |
c.tickers = make([]*testTicker, 0) |
| 74 |
return &c |
| 75 |
} |
| 76 |
|
| 77 |
// Set sets the internal time of the test clock and triggers any timers or tickers that should fire. |
| 78 |
func (c *TestClock) Set(t time.Time) { |
| 79 |
c.mu.Lock() |
| 80 |
defer c.mu.Unlock() |
| 81 |
|
| 82 |
if t.Before(c.time) { |
| 83 |
panic("can't go back in time") |
| 84 |
} |
| 85 |
|
| 86 |
c.time = t |
| 87 |
for _, ticker := range c.tickers { |
| 88 |
if !ticker.stopped.Load() && !ticker.nextTick.Add(ticker.interval).After(c.time) { |
| 89 |
//nolint: durationcheck // This is a test clock, we don't care about overflows. |
| 90 |
nextTick := (c.time.Sub(ticker.nextTick) / ticker.interval) * ticker.interval |
| 91 |
ticker.nextTick = ticker.nextTick.Add(nextTick) |
| 92 |
select { |
| 93 |
case ticker.ch <- c.time: |
| 94 |
default: |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
unfiredTimers := make([]*testTimer, 0) |
| 100 |
for i, timer := range c.timers { |
| 101 |
if timer.deadline.After(c.time) && !timer.stopped.Load() { |
| 102 |
unfiredTimers = append(unfiredTimers, c.timers[i]) |
| 103 |
continue |
| 104 |
} |
| 105 |
timer.stopped.Store(true) |
| 106 |
timer.ch <- c.time |
| 107 |
} |
| 108 |
c.timers = unfiredTimers |
| 109 |
} |
| 110 |
|
| 111 |
// Add adds the duration to the internal time of the test clock |
| 112 |
// and triggers any timers or tickers that should fire. |
| 113 |
func (c *TestClock) Add(d time.Duration) { |
| 114 |
c.Set(c.time.Add(d)) |
| 115 |
} |
| 116 |
|
| 117 |
// Now returns the internal time of the test clock. |
| 118 |
func (c *TestClock) Now() time.Time { |
| 119 |
c.mu.Lock() |
| 120 |
defer c.mu.Unlock() |
| 121 |
return c.time |
| 122 |
} |
| 123 |
|
| 124 |
// NewTicker creates a new ticker that will fire every time |
| 125 |
// the internal clock advances by the specified duration. |
| 126 |
func (c *TestClock) NewTicker(d time.Duration) (<-chan time.Time, func()) { |
| 127 |
c.mu.Lock() |
| 128 |
defer c.mu.Unlock() |
| 129 |
|
| 130 |
ch := make(chan time.Time, 1) |
| 131 |
stopped := &atomic.Bool{} |
| 132 |
ticker := &testTicker{nextTick: c.time, interval: d, ch: ch, stopped: stopped} |
| 133 |
c.tickers = append(c.tickers, ticker) |
| 134 |
stop := func() { |
| 135 |
stopped.Store(true) |
| 136 |
} |
| 137 |
|
| 138 |
return ch, stop |
| 139 |
} |
| 140 |
|
| 141 |
// NewTimer creates a new timer that will fire once the internal time |
| 142 |
// of the clock has been advanced passed the specified duration. |
| 143 |
func (c *TestClock) NewTimer(d time.Duration) (<-chan time.Time, func() bool) { |
| 144 |
c.mu.Lock() |
| 145 |
defer c.mu.Unlock() |
| 146 |
|
| 147 |
ch := make(chan time.Time, 1) |
| 148 |
stopped := &atomic.Bool{} |
| 149 |
|
| 150 |
// Fire the timer straight away if the duration is less than zero. |
| 151 |
if d <= 0 { |
| 152 |
ch <- c.time |
| 153 |
return ch, func() bool { return false } |
| 154 |
} |
| 155 |
|
| 156 |
timer := &testTimer{deadline: c.time.Add(d), ch: ch, stopped: stopped} |
| 157 |
c.timers = append(c.timers, timer) |
| 158 |
stop := func() bool { |
| 159 |
return stopped.CompareAndSwap(false, true) |
| 160 |
} |
| 161 |
|
| 162 |
return ch, stop |
| 163 |
} |
| 164 |
|
| 165 |
// Since returns the duration between the internal time of the clock and the specified time. |
| 166 |
func (c *TestClock) Since(t time.Time) time.Duration { |
| 167 |
return c.Now().Sub(t) |
| 168 |
} |