Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
time: mockable time support #8869
With https://github.com/jmhodges/clock and the broader https://github.com/benbjohnson/clock API, there's a desire for the timing systems in Go to be fakeable. Using a fake clock instead of time.Now directly is useful when testing code that stores timestamps, caches data, etc. The benbjohnson clock package attempts to make Ticker and Timer calls (as well as their related AfterFunc and Sleep methods) work against a fake time that can be set and updated in tests. However, it relies on some micro-sleeps and runtime.Gosched calls that are obviously going to be flaky. But there is a desire to able to test code that uses Tickers and Timers, not by adjusting the durations they work for (which can induce flaky testing), but by adjusting when they think they need to wake up. To do that, we seem to need more runtime magic to help developers out. In fact, it might be best if a clock package like these lived in the stdlib so that it could be tied more carefully and thoughtfully to the scheduler.  or "mockable", whatever language you prefer. The summary of this issue is to distinguish it from issue #5356.
I'm not entirely sure this is a good idea. I'm also not sure what it would look like. If the goal is simply to provide Ticker and Timer calls for testing, I think that could be done entirely independently of the time package, along the lines of the old playground code. The testing package would keep its own queue of events, and step forward to the next event without actually waiting.
Labels changed: added repo-main, release-none.