clock

package module
v0.0.0-...-204199a Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 4 Imported by: 0

README

clock Coverage Status GoDoc Project status

NOTE: This README has not yet been updated to reflect the fixes/changes. Please refer to the source code in the meantime.

Clock is a small library for mocking time in Go. It provides an interface around the standard library's time package so that the application can use the realtime clock while tests can use the mock clock.

Usage

Realtime Clock

Your application can maintain a Clock variable that will allow realtime and mock clocks to be interchangable. For example, if you had an Application type:

import "github.com/aristanetworks/clock"

type Application struct {
	Clock clock.Clock
}

You could initialize it to use the realtime clock like this:

var app Application
app.Clock = clock.New()
...

Then all timers and time-related functionality should be performed from the Clock variable.

Mocking time

In your tests, you will want to use a Mock clock:

import (
	"testing"

	"github.com/aristanetworks/clock/mock"
)

func TestApplication_DoSomething(t *testing.T) {
	mock := mock.NewMockClock(ctrl)
	app := Application{Clock: mock}
	...
}

Now that you've initialized your application to use the mock clock, you can use the standard gomock methods to mock any call for any method.

Examples
fake Sleep
mock := clock.NewMockClock(ctrl)

mock.EXPECT().Sleep(gomock.Any()).AnyTimes()
Return a fake time
mock := clock.NewMockClock(ctrl)

now := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
mock.EXPECT().Now().Return(now).AnyTimes()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithDeadline

func ContextWithDeadline(parent context.Context, clock Clock, deadline time.Time) (context.Context, context.CancelFunc)

func ContextWithDeadlineCause

func ContextWithDeadlineCause(parent context.Context, clock Clock, deadline time.Time, cause error) (context.Context, context.CancelFunc)

func ContextWithTimeout

func ContextWithTimeout(parent context.Context, clock Clock, timeout time.Duration) (context.Context, context.CancelFunc)

func ContextWithTimeoutCause

func ContextWithTimeoutCause(parent context.Context, clock Clock, timeout time.Duration, cause error) (context.Context, context.CancelFunc)

Types

type Clock

type Clock interface {
	After(d time.Duration) <-chan time.Time
	AfterFunc(d time.Duration, f func()) Timer
	Now() time.Time
	Since(t time.Time) time.Duration
	Sleep(d time.Duration)
	Tick(d time.Duration) <-chan time.Time
	Ticker(d time.Duration) Ticker
	Timer(d time.Duration) Timer
}

Clock represents an interface to the functions in the standard library time package. Two implementations are available in the clock package. The first is a real-time clock which simply wraps the time package's functions. The second is a mock clock which will only make forward progress when programmatically adjusted.

func New

func New() Clock

New returns an instance of a real-time clock.

type Ticker

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

A Ticker delivers `ticks' of a clock at intervals.

type Timer

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

The Timer type represents a single event. When the Timer expires, the current time will be sent on C(), unless the Timer was created by AfterFunc. A Timer must be created with clock.Timer or clock.AfterFunc.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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