clock

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 0

README

Clock

A drop in (almost) replacement for the system time package. It provides a way to make scheduled calls, timers and tickers deterministic in tests. By default it forwards all calls to the system time package. In test, however, it is possible to enable the frozen clock mode, and advance time manually to make scheduled even trigger at certain moments.

Usage

package foo

import (
    "testing"

    "github.com/vulcand/oxy/internal/holsterv4/clock"
	"github.com/stretchr/testify/assert"
)

func TestSleep(t *testing.T) {
    // Freeze switches the clock package to the frozen clock mode. You need to
    // advance time manually from now on. Note that all scheduled events, timers
    // and ticker created before this call keep operating in real time.
    //
    // The initial time is set to now here, but you can set any datetime.
    clock.Freeze(clock.Now())
    // Do not forget to revert the effect of Freeze at the end of the test.
    defer clock.Unfreeze()

    var fired bool

    clock.AfterFunc(100*clock.Millisecond, func() {
        fired = true
    })
    clock.Advance(93*clock.Millisecond)

    // Advance will make all fire all events, timers, tickers that are
    // scheduled for the passed period of time. Note that scheduled functions
    // are called from within Advanced unlike system time package that calls
    // them in their own goroutine.
    assert.Equal(t, 97*clock.Millisecond, clock.Advance(6*clock.Millisecond))
    assert.True(t, fired)
    assert.Equal(t, 100*clock.Millisecond, clock.Advance(1*clock.Millisecond))
    assert.True(t, fired)
}

Documentation

Overview

Package clock provides the same functions as the system package time. In production it forwards all calls to the system time package, but in tests the time can be frozen by calling Freeze function and from that point it has to be advanced manually with Advance function making all scheduled calls deterministic.

The functions provided by the package have the same parameters and return values as their system counterparts with a few exceptions. Where either *time.Timer or *time.Ticker is returned by a system function, the clock package counterpart returns clock.Timer or clock.Ticker interface respectively. The interfaces provide API as respective structs except C is not a channel, but a function that returns <-chan time.Time.

Index

Constants

View Source
const (
	Nanosecond  = time.Nanosecond
	Microsecond = time.Microsecond
	Millisecond = time.Millisecond
	Second      = time.Second
	Minute      = time.Minute
	Hour        = time.Hour

	Sunday    = time.Sunday
	Monday    = time.Monday
	Tuesday   = time.Tuesday
	Wednesday = time.Wednesday
	Thursday  = time.Thursday
	Friday    = time.Friday
	Saturday  = time.Saturday

	January   = time.January
	February  = time.February
	March     = time.March
	April     = time.April
	May       = time.May
	June      = time.June
	July      = time.July
	August    = time.August
	September = time.September
	October   = time.October
	November  = time.November
	December  = time.December

	ANSIC       = time.ANSIC
	UnixDate    = time.UnixDate
	RubyDate    = time.RubyDate
	RFC822      = time.RFC822
	RFC822Z     = time.RFC822Z
	RFC850      = time.RFC850
	RFC1123     = time.RFC1123
	RFC1123Z    = time.RFC1123Z
	RFC3339     = time.RFC3339
	RFC3339Nano = time.RFC3339Nano
	Kitchen     = time.Kitchen
	Stamp       = time.Stamp
	StampMilli  = time.StampMilli
	StampMicro  = time.StampMicro
	StampNano   = time.StampNano
)

Variables

View Source
var (
	UTC   = time.UTC
	Local = time.Local
)

Functions

func Advance

func Advance(d time.Duration) time.Duration

Makes the deterministic time move forward by the specified duration, firing timers along the way in the natural order. It returns how much time has passed since it was frozen. So you can assert on the return value in tests to make it explicit where you stand on the deterministic time scale.

func After

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

After see time.After.

func Now

func Now() time.Time

Now see time.Now.

func Sleep

func Sleep(d time.Duration)

Sleep see time.Sleep.

func Tick

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

Tick see time.Tick.

func Unfreeze

func Unfreeze()

Unfreeze reverses effect of Freeze.

func Wait4Scheduled

func Wait4Scheduled(count int, timeout time.Duration) bool

Wait4Scheduled blocks until either there are n or more scheduled events, or the timeout elapses. It returns true if the wait condition has been met before the timeout expired, false otherwise.

Types

type Clock

type Clock interface {
	Now() time.Time
	Sleep(d time.Duration)
	After(d time.Duration) <-chan time.Time
	NewTimer(d time.Duration) Timer
	AfterFunc(d time.Duration, f func()) Timer
	NewTicker(d time.Duration) Ticker
	Tick(d time.Duration) <-chan time.Time
	Wait4Scheduled(n int, timeout time.Duration) bool
}

Clock is an interface that mimics the one of the SDK time package.

func Realtime

func Realtime() Clock

Realtime returns a clock provider wrapping the SDK's time package. It is supposed to be used in tests when time is frozen to schedule test timeouts.

type Duration

type Duration = time.Duration

func ParseDuration

func ParseDuration(s string) (Duration, error)

func Since

func Since(t Time) Duration

func Until

func Until(t Time) Duration

type DurationJSON

type DurationJSON struct {
	Duration Duration
}

func NewDurationJSON

func NewDurationJSON(v interface{}) (DurationJSON, error)

func NewDurationJSONOrPanic

func NewDurationJSONOrPanic(v interface{}) DurationJSON

func (DurationJSON) MarshalJSON

func (d DurationJSON) MarshalJSON() ([]byte, error)

func (DurationJSON) String

func (d DurationJSON) String() string

func (*DurationJSON) UnmarshalJSON

func (d *DurationJSON) UnmarshalJSON(b []byte) error

type Location

type Location = time.Location

func FixedZone

func FixedZone(name string, offset int) *Location

func LoadLocation

func LoadLocation(name string) (*Location, error)

type Month

type Month = time.Month

type ParseError

type ParseError = time.ParseError

type RFC822Time

type RFC822Time struct {
	Time
}

Allows seamless JSON encoding/decoding of rfc822 formatted timestamps. https://www.ietf.org/rfc/rfc822.txt section 5.

func NewRFC822Time

func NewRFC822Time(t Time) RFC822Time

NewRFC822Time creates RFC822Time from a standard Time. The created value is truncated down to second precision because RFC822 does not allow for better.

func NewRFC822TimeFromUnix

func NewRFC822TimeFromUnix(timestamp int64) RFC822Time

NewRFC822Time creates RFC822Time from a Unix timestamp (seconds from Epoch).

func (RFC822Time) MarshalJSON

func (t RFC822Time) MarshalJSON() ([]byte, error)

func (RFC822Time) String

func (t RFC822Time) String() string

func (RFC822Time) StringWithOffset

func (t RFC822Time) StringWithOffset() string

func (*RFC822Time) UnmarshalJSON

func (t *RFC822Time) UnmarshalJSON(s []byte) error

type Ticker

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

Ticker see time.Ticker.

func NewTicker

func NewTicker(d time.Duration) Ticker

NewTicker see time.Ticker.

type Time

type Time = time.Time

func Date

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time

func Parse

func Parse(layout, value string) (Time, error)

func ParseInLocation

func ParseInLocation(layout, value string, loc *Location) (Time, error)

func ParseRFC822Time

func ParseRFC822Time(s string) (Time, error)

ParseRFC822Time parses an RFC822 time string.

func Unix

func Unix(sec int64, nsec int64) Time

type Timer

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

Timer see time.Timer.

func AfterFunc

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

AfterFunc see time.AfterFunc.

func NewStoppedTimer

func NewStoppedTimer() Timer

NewStoppedTimer returns a stopped timer. Call Reset to get it ticking.

func NewTimer

func NewTimer(d time.Duration) Timer

NewTimer see time.NewTimer.

type Unfreezer

type Unfreezer struct{}

func Freeze

func Freeze(now time.Time) Unfreezer

Freeze after this function is called all time related functions start generate deterministic timers that are triggered by Advance function. It is supposed to be used in tests only. Returns an Unfreezer so it can be a one-liner in tests: defer clock.Freeze(clock.Now()).Unfreeze()

func (Unfreezer) Unfreeze

func (u Unfreezer) Unfreeze()

type Weekday

type Weekday = time.Weekday

Jump to

Keyboard shortcuts

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