NewTicker returns a time.Ticker that ticks at the specified frequency. As with [time.NewTicker], the ticker will drop ticks if the receiver is slow, and the channel is never closed. Calling Stop on the returned ticker is a no-op. The ticker only runs when the clock is advanced.
(d time.Duration)
| 66 | // Calling Stop on the returned ticker is a no-op. |
| 67 | // The ticker only runs when the clock is advanced. |
| 68 | func (c *MockClock) NewTicker(d time.Duration) *time.Ticker { |
| 69 | ch := make(chan time.Time, 1) |
| 70 | |
| 71 | var tick func(time.Time) |
| 72 | tick = func(now time.Time) { |
| 73 | next := now.Add(d) |
| 74 | c.runAt(next, func() { |
| 75 | defer tick(next) |
| 76 | |
| 77 | select { |
| 78 | case ch <- next: |
| 79 | // ok |
| 80 | default: |
| 81 | // The receiver is slow. |
| 82 | // Drop the tick and continue. |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | tick(c.Now()) |
| 87 | |
| 88 | return &time.Ticker{C: ch} |
| 89 | } |
| 90 | |
| 91 | // runAt schedules the given function to be run at the given time. |
| 92 | // The function runs without a lock held, so it may schedule more work. |