Documentation
¶
Overview ¶
Package syncclock provides a near drop-in replacement for clockwork.FakeClock that is backed by Go 1.25's testing/synctest package instead of maintaining its own fake time.
Rather than tracking waiters and manually expiring timers, SyncClock delegates to the real time package and uses synctest.Wait to synchronise goroutines inside a synctest bubble. This means all standard time functions (After, Sleep, NewTimer, …) work as expected and time only advances when SyncClock.Advance is called.
Migration from clockwork ¶
In most cases migrating is a two-step process:
- Wrap your test function with synctest.Test.
- Replace clockwork.NewFakeClock with NewFakeClock (or clockwork.NewFakeClockAt with NewFakeClockAt).
Production code can keep using clockwork.NewRealClock unchanged because SyncClock implements clockwork.Clock.
Not implemented ¶
The following clockwork.FakeClock methods have no equivalent in SyncClock because synctest's cooperative scheduling model makes them unnecessary:
- BlockUntil – use synctest.Wait instead.
- BlockUntilContext – use synctest.Wait instead.
Limitations ¶
A SyncClock must be created inside a synctest.Test bubble.
Because synctest does not consider blocking I/O operations (e.g. network reads) as durable blocks, HTTP servers and similar code must be mocked using net.Pipe or equivalent. See the synctest documentation for details.
Index ¶
- type FakeClock
- type SyncClock
- func (s *SyncClock) Advance(d time.Duration)
- func (s *SyncClock) After(d time.Duration) <-chan time.Time
- func (s *SyncClock) AfterFunc(d time.Duration, f func()) clockwork.Timer
- func (s *SyncClock) NewTicker(d time.Duration) clockwork.Ticker
- func (s *SyncClock) NewTimer(d time.Duration) clockwork.Timer
- func (s *SyncClock) Now() time.Time
- func (s *SyncClock) Since(t time.Time) time.Duration
- func (s *SyncClock) Sleep(d time.Duration)
- func (s *SyncClock) Until(t time.Time) time.Duration
- type SyncTicker
- type SyncTimer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FakeClock ¶
FakeClock is a minimal interface satisfied by both clockwork.FakeClock and SyncClock. It allows code that only needs the Advance method to accept either implementation without a type assertion.
type SyncClock ¶
type SyncClock struct{}
SyncClock implements clockwork.Clock on top of testing/synctest. All time operations delegate to the standard time package; advancing the clock is achieved by sleeping inside the synctest bubble and then calling synctest.Wait to let other goroutines observe the new time.
A SyncClock must only be used inside a synctest.Test bubble.
func NewFakeClock ¶
func NewFakeClock() *SyncClock
NewFakeClock creates a SyncClock whose initial time is time.Now inside the synctest bubble. It must be called from within a synctest.Test bubble.
func NewFakeClockAt ¶
NewFakeClockAt creates a SyncClock whose initial time is t. It must be called from within a synctest.Test bubble.
It panics if t is before midnight UTC 2000-01-01 because synctest does not support rewinding time before that point.
func (*SyncClock) Advance ¶
Advance moves the fake clock forward by d and waits for all goroutines in the synctest bubble to reach a durable blocking state. This is the equivalent of clockwork.FakeClock.Advance.
func (*SyncClock) After ¶
After mimics time.After; it waits for d to elapse on the fake clock, then sends the current time on the returned channel.
func (*SyncClock) AfterFunc ¶
AfterFunc returns a clockwork.Timer that invokes f after d has elapsed.
func (*SyncClock) NewTicker ¶
NewTicker returns a clockwork.Ticker that ticks every d.
func (*SyncClock) NewTimer ¶
NewTimer returns a clockwork.Timer that fires after d.
type SyncTicker ¶
SyncTicker wraps a time.Ticker and implements clockwork.Ticker.
func (*SyncTicker) Chan ¶
func (st *SyncTicker) Chan() <-chan time.Time
Chan returns the channel on which ticks are delivered.