FakeClock does not behave like RealClock on Go >= 1.23
Ever since Go 1.23, the fake and real clocks behave differently:
func Test_RealTimer(t *testing.T) {
c := clockwork.NewRealClock()
test_drainTimer(t, c, func() {
c.Sleep(time.Second)
})
}
func Test_FakeTimer(t *testing.T) {
c := clockwork.NewFakeClock()
test_drainTimer(t, c, func() {
c.Advance(time.Second)
})
}
func test_drainTimer(t *testing.T, c clockwork.Clock, advanceTime func()) {
timer := c.NewTimer(time.Second)
advanceTime()
timer.Reset(time.Second)
if len(timer.Chan()) > 0 {
t.Fatal("timer didn't reset, but fired immediately")
}
}
On Go 1.22 Fake and Real give an identical and correct result (the timer should be drained)
main_test.go:111: timer didn't reset, but fired immediately
main_test.go:111: timer didn't reset, but fired immediately
However on Go 1.23, Fake and Real behave differently:
main_test.go:111: timer didn't reset, but fired immediately
See also https://github.com/jonboulle/clockwork/issues/84
@DPJacques Have you considered a way to correct this behavior on newer Go versions?
Unfortunately, some non-open-source issues have had me considerably distracted. I need to come back into context on this.
In the test code, what would advanceTime() do for a real clock? I assume nothing and reset is called immediately?
The change from Go 1.23 was difficult to navigate and reason about. I have not had much time to come back to this.