README

clock

An interface definition for a fully defined clock.

An WallClock implementation of that interface using the time package.

A testing clock.

Expand ▾ Collapse ▴

Documentation

Index

Constants

This section is empty.

Variables

var WallClock wallClock

WallClock exposes wall-clock time via the Clock interface.


Functions

func Alarm

func Alarm(c Clock, t time.Time) <-chan time.Time

Alarm returns a channel that will have the time sent on it at some point after the supplied time occurs.

This is short for c.After(t.Sub(c.Now())).

Types

type Clock

type Clock interface {
	// Now returns the current clock time.
	Now() time.Time

	// After waits for the duration to elapse and then sends the
	// current time on the returned channel.
	After(time.Duration) <-chan time.Time

	// AfterFunc waits for the duration to elapse and then calls f in its own goroutine.
	// It returns a Timer that can be used to cancel the call using its Stop method.
	AfterFunc(d time.Duration, f func()) Timer

	// NewTimer creates a new Timer that will send the current time
	// on its channel after at least duration d.
	NewTimer(d time.Duration) Timer
}

Clock provides an interface for dealing with clocks.

type Timer

type Timer interface {
	// When the Timer expires, the current time will be sent on the
	// channel returned from Chan, unless the Timer was created by
	// AfterFunc.
	Chan() <-chan time.Time

	// 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.
	Reset(time.Duration) bool

	// 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.
	Stop() bool
}

The Timer type represents a single event. A Timer must be created with AfterFunc. This interface follows time.Timer's methods but provides easier mocking.

Source Files

Directories

Path Synopsis
monotonic
testclock