clock

package
v0.0.0-...-6fe5fd8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 1 Imported by: 1,110

README

Clock

This package provides an interface for time-based operations. It allows mocking time for testing.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	PassiveClock
	// After returns the channel of a new Timer.
	// This method does not allow to free/GC the backing timer before it fires. Use
	// NewTimer instead.
	After(d time.Duration) <-chan time.Time
	// NewTimer returns a new Timer.
	NewTimer(d time.Duration) Timer
	// Sleep sleeps for the provided duration d.
	// Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
	Sleep(d time.Duration)
	// Tick returns the channel of a new Ticker.
	// This method does not allow to free/GC the backing ticker. Use
	// NewTicker from WithTicker instead.
	Tick(d time.Duration) <-chan time.Time
}

Clock allows for injecting fake or real clocks into code that needs to do arbitrary things based on time.

type PassiveClock

type PassiveClock interface {
	Now() time.Time
	Since(time.Time) time.Duration
}

PassiveClock allows for injecting fake or real clocks into code that needs to read the current time but does not support scheduling activity in the future.

type RealClock

type RealClock struct{}

RealClock really calls time.Now()

func (RealClock) After

func (RealClock) After(d time.Duration) <-chan time.Time

After is the same as time.After(d). This method does not allow to free/GC the backing timer before it fires. Use NewTimer instead.

func (RealClock) AfterFunc

func (RealClock) AfterFunc(d time.Duration, f func()) Timer

AfterFunc is the same as time.AfterFunc(d, f).

func (RealClock) NewTicker

func (RealClock) NewTicker(d time.Duration) Ticker

NewTicker returns a new Ticker.

func (RealClock) NewTimer

func (RealClock) NewTimer(d time.Duration) Timer

NewTimer is the same as time.NewTimer(d)

func (RealClock) Now

func (RealClock) Now() time.Time

Now returns the current time.

func (RealClock) Since

func (RealClock) Since(ts time.Time) time.Duration

Since returns time since the specified timestamp.

func (RealClock) Sleep

func (RealClock) Sleep(d time.Duration)

Sleep is the same as time.Sleep(d) Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.

func (RealClock) Tick

func (RealClock) Tick(d time.Duration) <-chan time.Time

Tick is the same as time.Tick(d) This method does not allow to free/GC the backing ticker. Use NewTicker instead.

type Ticker

type Ticker interface {
	C() <-chan time.Time
	Stop()
}

Ticker defines the Ticker interface.

type Timer

type Timer interface {
	C() <-chan time.Time
	Stop() bool
	Reset(d time.Duration) bool
}

Timer allows for injecting fake or real timers into code that needs to do arbitrary things based on time.

type WithDelayedExecution

type WithDelayedExecution interface {
	Clock
	// AfterFunc executes f in its own goroutine after waiting
	// for d duration and returns a Timer whose channel can be
	// closed by calling Stop() on the Timer.
	AfterFunc(d time.Duration, f func()) Timer
}

WithDelayedExecution allows for injecting fake or real clocks into code that needs to make use of AfterFunc functionality.

type WithTicker

type WithTicker interface {
	Clock
	// NewTicker returns a new Ticker.
	NewTicker(time.Duration) Ticker
}

WithTicker allows for injecting fake or real clocks into code that needs to do arbitrary things based on time.

type WithTickerAndDelayedExecution

type WithTickerAndDelayedExecution interface {
	WithTicker
	// AfterFunc executes f in its own goroutine after waiting
	// for d duration and returns a Timer whose channel can be
	// closed by calling Stop() on the Timer.
	AfterFunc(d time.Duration, f func()) Timer
}

WithTickerAndDelayedExecution allows for injecting fake or real clocks into code that needs Ticker and AfterFunc functionality

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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