Source File
observeclock.go
Belonging Package
go.pact.im/x/clock/observeclock
// 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 observeclockimport ()var _ interface {clock.Schedulerclock.TimerSchedulerclock.TickerScheduler} = (*Clock)(nil)// Clock allows observing creation of new events on the underlying clock.Clock// instance.type Clock struct {*clock.Clockmu sync.Mutexxs []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[] = nilclose()}.xs = .xs[:0]}
The pages are generated with Golds v0.7.6. (GOOS=linux GOARCH=amd64)