ttime

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2015 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 2 Imported by: 0

README

TTime - Testable Time

TTime is a small package meant to be used in place of the Go std "time" package. The driving goal of this package is to be testable by allowing a user to plug in their own 'time' struct.

If no 'time' struct is configured, ttime will behave normally.

A testing shim, ttime.TestTime, is also provided which allows time to be fast-forwarded and skipped.

This package does not provide a complete replacement to the 'time' package at this time, preferring to cover the most common use-cases that include "Sleep" and "Now".

Documentation

Overview

Package ttime implements a testable alternative to the Go "time" package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After added in v1.1.0

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

After calls the implementations After method

func Now

func Now() time.Time

Now returns the implementation's current time

func SetTime

func SetTime(t Time)

SetTime configures what 'Time' implementation to use for each of the package-level methods.

func Since

func Since(t time.Time) time.Duration

Since returns the time different from Now and the given time t

func Sleep

func Sleep(d time.Duration)

Sleep calls the implementation's Sleep method

Types

type DefaultTime

type DefaultTime struct{}

DefaultTime is a Time that behaves normally

func (*DefaultTime) After added in v1.1.0

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

After sleeps for the given duration and then writes to to the returned channel

func (*DefaultTime) Now

func (*DefaultTime) Now() time.Time

Now returns the current time

func (*DefaultTime) Sleep

func (*DefaultTime) Sleep(d time.Duration)

Sleep sleeps for the given duration

type TestTime

type TestTime struct {
	// IsLudicrousSpeed indicates whether sleeps will all succeed instantly
	IsLudicrousSpeed bool
	// contains filtered or unexported fields
}

TestTime implements a time that is able to be moved forward easily

func NewTestTime

func NewTestTime() *TestTime

NewTestTime returns a default TestTime. It will not be LudicrousSpeed and will behave like a normal time

func (*TestTime) After added in v1.1.0

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

After returns a channel which is written to after the given duration, taking into account time-warping

func (*TestTime) LudicrousSpeed

func (t *TestTime) LudicrousSpeed(b bool)

LudicrousSpeed can be called to toggle LudicrousSpeed (default off). When LudicrousSpeed is engaged, all calls to "sleep" will succeed instantly.

func (*TestTime) Now

func (t *TestTime) Now() time.Time

Now returns the current time, including any time-warping that has occured.

func (*TestTime) Sleep

func (t *TestTime) Sleep(d time.Duration)

Sleep sleeps the given duration in mock-time; that is to say that Warps will reduce the amount of time slept and LudicrousSpeed will cause instant success.

func (*TestTime) Warp

func (t *TestTime) Warp(d time.Duration)

Warp moves the mock time forwards by the given duration.

type Time

type Time interface {
	Now() time.Time
	Sleep(d time.Duration)
	After(d time.Duration) <-chan time.Time
}

Time represents an implementation for this package's methods

Jump to

Keyboard shortcuts

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