// Package observeclock provides a clock implementation that allows observing // Schedule, Timer and Ticker method calls. // // The package is intended as an alternative to mockclock for simple cases when // observing an event creation is enough to reach the desired state. //
package observeclock import ( ) var _ interface { clock.Scheduler clock.TimerScheduler clock.TickerScheduler } = (*Clock)(nil) // Clock allows observing creation of new events on the underlying clock.Clock // instance. type Clock struct { *clock.Clock mu sync.Mutex xs []chan struct{} } // NewClock returns a new Clock that observes the given clock.Clock. func ( *clock.Clock) *Clock { return &Clock{ Clock: , } } // New returns a new Clock that observes the given clock.Scheduler including // optional interface method calls. func ( clock.Scheduler) *Clock { return NewClock(clock.NewClock()) } // Schedule implements the clock.Scheduler interface. func ( *Clock) ( time.Duration, func(time.Time)) clock.Event { := .Clock.Schedule(, ) .event() return } // Timer implements the clock.TimerScheduler interface. func ( *Clock) ( time.Duration) clock.Timer { := .Clock.Timer() .event() return } // Ticker implements the clock.TickerScheduler interface. func ( *Clock) ( time.Duration) clock.Ticker { := .Clock.Ticker() .event() return } // Observe returns a channel that is closed on Schedule, Timer and Ticker calls. func ( *Clock) () <-chan struct{} { .mu.Lock() defer .mu.Unlock() := make(chan struct{}) .xs = append(.xs, ) return } // event triggers an observable event. func ( *Clock) () { .mu.Lock() defer .mu.Unlock() for , := range .xs { .xs[] = nil close() } .xs = .xs[:0] }