timing

package
v0.0.0-...-5395fea Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: MIT Imports: 2 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 {
	// Now returns the current time
	Now() time.Time

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

	// Sleep pauses the current goroutine for at least the duration d
	Sleep(d time.Duration)

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

	// AfterFunc waits for the duration to elapse and then calls f in its own goroutine
	AfterFunc(d time.Duration, f func()) Timer

	// NewTicker returns a new Ticker containing a channel that will send the current time on the channel after each tick
	NewTicker(d time.Duration) Ticker
}

Clock represents an interface to time-related functionality. It wraps benbjohnson/clock.Clock to provide mockable time operations.

func New

func New() Clock

New returns a new Clock that uses the system clock

type MockClock

type MockClock interface {
	Clock

	// Add moves the mock clock forward by the specified duration
	Add(d time.Duration)

	// Set sets the mock clock to a specific time
	Set(t time.Time)
}

MockClock extends Clock with methods to control time in tests

func NewMock

func NewMock() MockClock

NewMock returns a new MockClock that can be moved forward in time programmatically

type Ticker

type Ticker interface {
	// C returns the channel on which ticks are delivered
	C() <-chan time.Time

	// Stop turns off a ticker
	Stop()
}

Ticker represents a periodic event

type Timer

type Timer interface {
	// C returns the channel on which the time will be sent
	C() <-chan time.Time

	// Stop prevents the Timer from firing
	Stop() bool

	// Reset changes the timer to expire after duration d
	Reset(d time.Duration) bool
}

Timer represents a single event

Jump to

Keyboard shortcuts

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