Clock is an interface for getting the current time and creating tickers. By abstracting time operations behind this interface, code can be written that works with both the system clock in production and controlled time in tests, enabling deterministic testing of time-dependent logic. This approach
| 10 | // This approach helps avoid flaky tests caused by timing dependencies and |
| 11 | // allows for simulating time-based scenarios without waiting for real time to pass. |
| 12 | type Clock interface { |
| 13 | // Now returns the current time. |
| 14 | // In production implementations, this returns the system time. |
| 15 | // In test implementations, this returns a controlled time that |
| 16 | // can be manipulated for testing purposes. |
| 17 | Now() time.Time |
| 18 | |
| 19 | // NewTicker returns a Ticker that fires every d. Production |
| 20 | // implementations delegate to time.NewTicker. Test implementations |
| 21 | // fire when simulated time advances past the ticker period, so a |
| 22 | // background goroutine driven by NewTicker observes the same notion |
| 23 | // of time as the rest of the test instead of running on real time. |
| 24 | NewTicker(d time.Duration) Ticker |
| 25 | } |
| 26 | |
| 27 | // Ticker delivers ticks on a channel at a regular cadence. It mirrors |
| 28 | // the surface of [time.Ticker] needed by callers that work against the |
no outgoing calls
no test coverage detected