timeutil

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package timeutil provides a testable abstraction over time operations.

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

	// Since returns the duration since t.
	Since(t time.Time) time.Duration

	// Until returns the duration until t.
	Until(t time.Time) time.Duration

	// Sleep pauses for the specified duration.
	Sleep(d time.Duration)

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

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

	// NewTicker returns a new Ticker containing a channel that will
	// send the time with a period specified by the duration argument.
	NewTicker(d time.Duration) Ticker
}

Clock provides an abstraction over time operations for testability.

type MockClock

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

MockClock is a manually controlled clock for testing.

func NewMockClock

func NewMockClock(t time.Time) *MockClock

NewMockClock creates a new MockClock set to the given time.

func (*MockClock) Advance

func (c *MockClock) Advance(d time.Duration)

Advance moves the mock clock forward by the given duration and fires any expired timers/tickers.

func (*MockClock) After

func (c *MockClock) After(d time.Duration) <-chan time.Time

After returns a channel that receives the time after duration d.

func (*MockClock) NewTicker

func (c *MockClock) NewTicker(d time.Duration) Ticker

NewTicker creates a new MockTicker.

func (*MockClock) NewTimer

func (c *MockClock) NewTimer(d time.Duration) Timer

NewTimer creates a new MockTimer.

func (*MockClock) Now

func (c *MockClock) Now() time.Time

Now returns the mocked current time.

func (*MockClock) Set

func (c *MockClock) Set(t time.Time)

Set sets the mock clock to a specific time.

func (*MockClock) Since

func (c *MockClock) Since(t time.Time) time.Duration

Since returns the duration since t.

func (*MockClock) Sleep

func (c *MockClock) Sleep(d time.Duration)

Sleep records the sleep duration but returns immediately.

func (*MockClock) Sleeps

func (c *MockClock) Sleeps() []time.Duration

Sleeps returns all recorded sleep durations.

func (*MockClock) Until

func (c *MockClock) Until(t time.Time) time.Duration

Until returns the duration until t.

type MockTicker

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

MockTicker is a manually controlled ticker for testing.

func (*MockTicker) C

func (t *MockTicker) C() <-chan time.Time

C returns the ticker channel.

func (*MockTicker) Reset

func (t *MockTicker) Reset(d time.Duration)

Reset stops a ticker and resets its period to the specified duration.

func (*MockTicker) Stop

func (t *MockTicker) Stop()

Stop turns off the ticker.

func (*MockTicker) Trigger

func (t *MockTicker) Trigger(now time.Time)

Trigger manually sends a tick with the given time.

type MockTimer

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

MockTimer is a manually controlled timer for testing.

func (*MockTimer) C

func (t *MockTimer) C() <-chan time.Time

C returns the timer channel.

func (*MockTimer) Reset

func (t *MockTimer) Reset(d time.Duration) bool

Reset changes the timer to expire after duration d.

func (*MockTimer) Stop

func (t *MockTimer) Stop() bool

Stop prevents the timer from firing.

type RealClock

type RealClock struct{}

RealClock implements Clock using the standard time package.

func (RealClock) After

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

After waits for the duration to elapse and then sends the current time.

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 creates a new Timer.

func (RealClock) Now

func (RealClock) Now() time.Time

Now returns the current time.

func (RealClock) Since

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

Since returns the time elapsed since t.

func (RealClock) Sleep

func (RealClock) Sleep(d time.Duration)

Sleep pauses the current goroutine for at least the duration d.

func (RealClock) Until

func (RealClock) Until(t time.Time) time.Duration

Until returns the duration until t.

type Ticker

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

	// Stop turns off a ticker.
	Stop()

	// Reset stops a ticker and resets its period to the specified duration.
	Reset(d time.Duration)
}

Ticker holds a channel that delivers "ticks" of a clock at intervals.

type Timer

type Timer interface {
	// C returns the channel on which the time is delivered.
	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 timer.

Jump to

Keyboard shortcuts

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