package clock

Import Path
	go.pact.im/x/clock (on go.dev)

Dependency Relation
	imports 2 packages, and imported by 5 packages

Involved Source Files Package clock defines basic interfaces to a clock. A clock can be provided by the host operating system but also by other packages. A clock provides a capability to schedule an arbitrary event to run after at least the given duration. On expiry, the event calls a function in its own goroutine with the current time argument. All other functions are implemented in terms of scheduled events or using an optimized Scheduler implementations. A time.Now function signature should be accepted instead of Clock instance if a limited capability is required that allows getting the current time but not scheduling new events. It is possible to implement Schedule, Timer and Ticker in terms of each other interchangeably and hence there is no option to limit the scope for these operations. now.go runtime.go ticker.go timer.go
Package-Level Type Names (total 13, in which 8 are exported)
/* sort exporteds by: | */
A Clock provides a functionality for measuring and displaying time. Scheduler Scheduler Now returns the current local time. Unless the underlying Scheduler implements Timer, Now calls Timer with zero duration and uses C on the returned timer to wait for the current time. Schedule implements the Scheduler interface. It uses the clock provided by the host operating system if c is nil or the underlying Scheduler is not set. Ticker returns a new Ticker containing a channel that will send the current time on the channel after each tick. The period of the ticks is specified by the duration argument. The ticker will adjust the time interval or drop ticks to make up for slow receivers. The duration d must be greater than zero; if not, Ticker will panic. Stop the ticker to release associated resources. Unless the underlying Scheduler implements TickerScheduler, Ticker calls Timer and resets the returned timer on each tick. Timer creates a new Timer that will send the current time on its channel after at least duration d. Unless the underlying Scheduler implements TimerScheduler, Timer calls Schedule and sends the current time on the channel when the Event expires. *Clock : NowScheduler *Clock : Scheduler *Clock : TickerScheduler *Clock : TimerScheduler func NewClock(s Scheduler) *Clock func System() *Clock func go.pact.im/x/clock/observeclock.NewClock(c *Clock) *observeclock.Clock func go.pact.im/x/flaky.(*DebounceExecutor).WithClock(c *Clock) *flaky.DebounceExecutor func go.pact.im/x/flaky.(*RetryExecutor).WithClock(c *Clock) *flaky.RetryExecutor func go.pact.im/x/flaky.(*ScheduleExecutor).WithClock(c *Clock) *flaky.ScheduleExecutor func go.pact.im/x/flaky.(*WatchdogExecutor).WithClock(c *Clock) *flaky.WatchdogExecutor
The Event type represents a single event. When the Event expires, the function is called in its own goroutine. Reset changes the timer to expire after duration d. It returns true if the timer had been active, false if the timer had expired or been stopped. It either reschedules when f will run, in which case Reset returns true, or schedules f to run again, in which case it returns false. Note that Reset does not guarantee that the subsequent goroutine running the function does not run concurrently with the prior one. Stop prevents the Event from firing. It returns true if the call stops the timer, false if the timer has been stopped or already expired and the function has been started in its own goroutine. Stop does not wait for function to complete before returning. *time.Timer func (*Clock).Schedule(d time.Duration, f func(now time.Time)) Event func NowScheduler.Schedule(d time.Duration, f func(now time.Time)) Event func Scheduler.Schedule(d time.Duration, f func(now time.Time)) Event func TickerScheduler.Schedule(d time.Duration, f func(now time.Time)) Event func TimerScheduler.Schedule(d time.Duration, f func(now time.Time)) Event func go.pact.im/x/clock/fakeclock.(*Clock).Schedule(d time.Duration, f func(now time.Time)) Event func go.pact.im/x/clock/mockclock.Clock.Schedule(d time.Duration, f func(now time.Time)) Event func go.pact.im/x/clock/mockclock.(*MockClock).Schedule(d time.Duration, f func(time.Time)) Event func go.pact.im/x/clock/observeclock.(*Clock).Schedule(d time.Duration, f func(time.Time)) Event
NowScheduler is the interface implemented by a clock that provides an optimized implementation of Now. Now returns the current local time. Schedule schedules an event that calls f in its own goroutine when the given duration elapses. It returns an Event that can be canceled using the Stop method. *Clock *go.pact.im/x/clock/fakeclock.Clock go.pact.im/x/clock/mockclock.Clock (interface) *go.pact.im/x/clock/mockclock.MockClock *go.pact.im/x/clock/observeclock.Clock NowScheduler : Scheduler
A Scheduler allows scheduling events to run after duration elapses. The Scheduler interface is the minimum implementation required of the clock. A clock may implement additional interfaces, such as TimerScheduler, to provide additional or optimized functionality. It is a low-level interface provided by clock implementations. Users should accept Clock instance instead of Scheduler. Schedule schedules an event that calls f in its own goroutine when the given duration elapses. It returns an Event that can be canceled using the Stop method. *Clock NowScheduler (interface) TickerScheduler (interface) TimerScheduler (interface) *go.pact.im/x/clock/fakeclock.Clock go.pact.im/x/clock/mockclock.Clock (interface) *go.pact.im/x/clock/mockclock.MockClock *go.pact.im/x/clock/observeclock.Clock func NewClock(s Scheduler) *Clock func go.pact.im/x/clock/observeclock.New(s Scheduler) *observeclock.Clock
A Ticker holds a channel that delivers “ticks” of a clock at intervals. C returns the channel on which the ticks are delivered. Reset stops a ticker and resets its period to the specified duration. The next tick will arrive after the new period elapses. The duration d must be greater than zero; if not, Reset will panic. Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does not close the channel, to prevent a concurrent goroutine reading from the channel from seeing an erroneous “tick”. *go.pact.im/x/clock/mockclock.MockTicker func (*Clock).Ticker(d time.Duration) Ticker func TickerScheduler.Ticker(d time.Duration) Ticker func go.pact.im/x/clock/fakeclock.(*Clock).Ticker(d time.Duration) Ticker func go.pact.im/x/clock/mockclock.Clock.Ticker(d time.Duration) Ticker func go.pact.im/x/clock/mockclock.(*MockClock).Ticker(d time.Duration) Ticker func go.pact.im/x/clock/observeclock.(*Clock).Ticker(d time.Duration) Ticker
TickerScheduler is the interface implemented by a clock that provides an optimized implementation of Ticker. Schedule schedules an event that calls f in its own goroutine when the given duration elapses. It returns an Event that can be canceled using the Stop method. Ticker returns a new Ticker containing a channel that will send the time on the channel after each tick. The period of the ticks is specified by the duration argument. The ticker will adjust the time interval or drop ticks to make up for slow receivers. The duration d must be greater than zero; if not, Ticker will panic. Stop the ticker to release associated resources. *Clock *go.pact.im/x/clock/fakeclock.Clock go.pact.im/x/clock/mockclock.Clock (interface) *go.pact.im/x/clock/mockclock.MockClock *go.pact.im/x/clock/observeclock.Clock TickerScheduler : Scheduler
The Timer type represents a single event. When the Timer expires, the current time will be sent on C. C returns the channel on which the current time is sent when the timer expires. Reset changes the timer to expire after duration d. Reset should be invoked only on stopped or expired timers with drained channels. If a program has already received a value from t.C, the timer is known to have expired and the channel drained, so t.Reset can be used directly. If a program has not yet received a value from t.C, however, the timer must be stopped and—if Stop reports that the timer expired before being stopped—the channel explicitly drained: select { case <-t.C(): // Timer expired. case <-done: if !t.Stop() { <-t.C() } } t.Reset(d) Reset should always be invoked on stopped or expired channels, as described above. This should not be done concurrent to other receives from the Timer’s channel. Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly. To ensure the channel is empty after a call to Stop, check the return value and drain the channel. For example, assuming the program has not received from t.C already: if !t.Stop() { <-t.C() } This cannot be done concurrent to other receives from the Timer’s channel or other calls to the Timer’s Stop method. *go.pact.im/x/clock/mockclock.MockTimer func (*Clock).Timer(d time.Duration) Timer func TimerScheduler.Timer(d time.Duration) Timer func go.pact.im/x/clock/fakeclock.(*Clock).Timer(d time.Duration) Timer func go.pact.im/x/clock/mockclock.Clock.Timer(d time.Duration) Timer func go.pact.im/x/clock/mockclock.(*MockClock).Timer(d time.Duration) Timer func go.pact.im/x/clock/observeclock.(*Clock).Timer(d time.Duration) Timer
TimerScheduler is the interface implemented by a clock that provides an optimized implementation of ScheduleTimer. Schedule schedules an event that calls f in its own goroutine when the given duration elapses. It returns an Event that can be canceled using the Stop method. Timer creates a new Timer that will send the current time on its channel after at least duration d. *Clock *go.pact.im/x/clock/fakeclock.Clock go.pact.im/x/clock/mockclock.Clock (interface) *go.pact.im/x/clock/mockclock.MockClock *go.pact.im/x/clock/observeclock.Clock TimerScheduler : Scheduler
Package-Level Functions (total 4, in which 2 are exported)
NewClock returns a new Clock that uses the given Scheduler to schedule events and measure time.
System returns a clock provided by the host operating system.
Package-Level Variables (only one, which is unexported)