clock

package
v4.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// NewTicker returns a new Ticker containing a channel that will send the
	// time with a period specified by the duration argument. It adjusts the
	// intervals or drops ticks to make up for slow receivers. The duration d
	// must be greater than zero; if not, NewTicker will panic. Stop the ticker
	// to release associated resources.
	NewTicker(time.Duration) Ticker
	// NewTimer creates a new Timer that will send the current time on its
	// channel after at least duration d.
	NewTimer(time.Duration) Timer
	// Now returns the current local time.
	Now() time.Time
	// Since returns Now().Sub(ts)
	Since(ts time.Time) time.Duration
}

Clock is the interface for methods from time package which can be easily mocked.

func New

func New() Clock

New returns a new instance of unmocked Clock.

type Mock

type Mock struct {
	// contains filtered or unexported fields
}

Mock exists to allow easier mocking of Clock interface.

func NewMock

func NewMock() *Mock

NewMock returns a mocked instance of a Clock.

func (*Mock) Add

func (m *Mock) Add(d time.Duration) time.Time

Add adds the d to current time and sets the time.

func (*Mock) NewTicker

func (m *Mock) NewTicker(d time.Duration) Ticker

NewTicker implements the Clock interface.

func (*Mock) NewTimer

func (m *Mock) NewTimer(d time.Duration) Timer

NewTimer implements the Clock interface.

func (*Mock) Now

func (m *Mock) Now() time.Time

Now implements the Clock interface.

func (*Mock) Set

func (m *Mock) Set(now time.Time)

Set adjusts the current time.

func (*Mock) Since added in v4.1.5

func (m *Mock) Since(ts time.Time) time.Duration

Since implements the Clock interface.

type Ticker

type Ticker interface {
	// C returns a channel on which ticks are delivered.
	C() <-chan time.Time
	// 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".
	Stop()
	// Reset stops a ticker and resets its period to the specified duration. The
	// next tick will arrive after the new period elapses.
	Reset(time.Duration)
}

A Ticker holds a channel that delivers `ticks' of a clock at intervals. This interface exists so it can be easily mocked.

type Timer

type Timer interface {
	// C returns a channel on which ticks are delivered.
	C() <-chan time.Time
	// 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.
	//
	// For a timer created with AfterFunc(d, f), if t.Stop returns false, then
	// the timer has already expired and the function f has been started in its
	// own goroutine; Stop does not wait for f to complete before returning. If
	// the caller needs to know whether f is completed, it must coordinate with f
	// explicitly.
	Stop() bool
	// 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 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:
	//
	// 	if !t.Stop() {
	// 		<-t.C
	// 	}
	// 	t.Reset(d)
	//
	// This should not be done concurrent to other receives from the Timer's
	// channel.
	//
	// Note that it is not possible to use Reset's return value correctly, as
	// there is a race condition between draining the channel and the new timer
	// expiring. Reset should always be invoked on stopped or expired channels,
	// as described above. The return value exists to preserve compatibility with
	// existing programs.
	Reset(time.Duration)
}

The Timer type represents a single event. When the Timer expires, the current time will be sent on C. This interface exist so it can be easily mocked.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL