| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | package clockwork |
| 2 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 3 | import "time" |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 4 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 5 | // Ticker provides an interface which can be used instead of directly using |
| 6 | // [time.Ticker]. The real-time ticker t provides ticks through t.C which |
| 7 | // becomes t.Chan() to make this channel requirement definable in this |
| 8 | // interface. |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 9 | type Ticker interface { |
| 10 | Chan() <-chan time.Time |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 11 | Reset(d time.Duration) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 12 | Stop() |
| 13 | } |
| 14 | |
| 15 | type realTicker struct{ *time.Ticker } |
| 16 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 17 | func (r realTicker) Chan() <-chan time.Time { |
| 18 | return r.C |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | type fakeTicker struct { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 22 | // The channel associated with the firer, used to send expiration times. |
| 23 | c chan time.Time |
| 24 | |
| 25 | // The time when the ticker expires. Only meaningful if the ticker is currently |
| 26 | // one of a FakeClock's waiters. |
| 27 | exp time.Time |
| 28 | |
| 29 | // reset and stop provide the implementation of the respective exported |
| 30 | // functions. |
| 31 | reset func(d time.Duration) |
| 32 | stop func() |
| 33 | |
| 34 | // The duration of the ticker. |
| 35 | d time.Duration |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 38 | func newFakeTicker(fc *FakeClock, d time.Duration) *fakeTicker { |
| 39 | var ft *fakeTicker |
| 40 | ft = &fakeTicker{ |
| 41 | c: make(chan time.Time, 1), |
| 42 | d: d, |
| 43 | reset: func(d time.Duration) { |
| 44 | fc.l.Lock() |
| 45 | defer fc.l.Unlock() |
| 46 | ft.d = d |
| 47 | fc.setExpirer(ft, d) |
| 48 | }, |
| 49 | stop: func() { fc.stop(ft) }, |
| 50 | } |
| 51 | return ft |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 54 | func (f *fakeTicker) Chan() <-chan time.Time { return f.c } |
| 55 | |
| 56 | func (f *fakeTicker) Reset(d time.Duration) { f.reset(d) } |
| 57 | |
| 58 | func (f *fakeTicker) Stop() { f.stop() } |
| 59 | |
| 60 | func (f *fakeTicker) expire(now time.Time) *time.Duration { |
| 61 | // Never block on expiration. |
| 62 | select { |
| 63 | case f.c <- now: |
| 64 | default: |
| 65 | } |
| 66 | return &f.d |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 69 | func (f *fakeTicker) expiration() time.Time { return f.exp } |
| 70 | |
| 71 | func (f *fakeTicker) setExpiration(t time.Time) { f.exp = t } |