timejumper

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2019 License: GPL-3.0 Imports: 3 Imported by: 2

README

TimeJumper

Documentation Go Report Card

Time as a dependency, very useful for testing. Unlike other similar libraries that use package globals, this gives you Clock interface that you can use in structs and functions as a dependency. RealClock{} delegates to the time standard library package.

Quick Start

package main

import (
    "time"
    timejumper "github.com/ghostsquad/go-timejumper"
)

clock := timejumper.New()
t := clock.Now()
fmt.Printf("current: %v\n", t)

fmt.Println("Dive into the future!")
clock.Jump(t.AddDate(1, 0, 0))
fmt.Printf("future: %v\n", clock.Now())

fmt.Println("Sleep, but not really")
clock.Sleep(10 * time.Hour)
fmt.Printf("future: %v\n", clock.Now())

fmt.Println("Back to the present")
clock.Back()
fmt.Printf("current: %v\n", clock.Now())

fmt.Println("Back to the past")
clock.Freeze(t)
fmt.Printf("frozen current: %v\n", clock.Now())
time.Sleep(5 * time.Seconds)
fmt.Printf("frozen current: %v\n", clock.Now())

Development

Testing
make install
make test
make test-bench
make test-race
CI Testing
make test-ci
make test-ci-bench
make test-ci-race

Output files are contained in reports/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
	Since(time.Time) time.Duration
	Sleep(time.Duration)
}

Clock is an interface that allows you to a create different implementions for testing purposes

type JumperClock

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

JumperClock is a Go implementation of but does not use package globals https://github.com/travisjeffery/timecop Instantiate with New()

func New

func New() *JumperClock

New returns a new instance of a TimeCopClock

func (*JumperClock) Back

func (c *JumperClock) Back()

Back returns you to the present (future? since you started) Does not change the TimeMachine

func (*JumperClock) Freeze

func (c *JumperClock) Freeze(t time.Time)

Freeze ensures time doesn't progress forward You can still move time by freezing again with a new time or by using Travel

func (*JumperClock) Jump

func (c *JumperClock) Jump(t time.Time)

Jump sets the clock to the desired time Compatible with Freeze (will remain frozen if previously frozen)

func (*JumperClock) NewDefaultTimeMachine

func (c *JumperClock) NewDefaultTimeMachine() TimeMachine

NewDefaultTimeMachine is what's used to enable scaling and freezing

func (*JumperClock) Now

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

Now activates the TimeMachine

func (*JumperClock) Scale

func (c *JumperClock) Scale(s int) error

Scale sets the time scale multiplier If set to 0, freezes the clock instead

func (*JumperClock) SetTimeMachine

func (c *JumperClock) SetTimeMachine(t TimeMachine)

SetTimeMachine sets TimeMachine, a function that controls how time works! It also resets the clock according to Back()

func (*JumperClock) Since

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

Since returns the time elapsed since t. It is shorthand for .Now().Sub(t).

func (*JumperClock) Sleep

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

Sleep is similar to jump, except that it takes a duration

type RealClock

type RealClock struct{}

RealClock implements the standard lib only functions relevant for this codebase

func (RealClock) Now

func (RealClock) Now() time.Time

Now is a proxy to time.Now

func (RealClock) Since

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

Since is a proxy to time.Since

func (RealClock) Sleep

func (RealClock) Sleep(d time.Duration)

Sleep is a proxy to time.Sleep

type TimeMachine

type TimeMachine func() (newTime time.Time)

TimeMachine is a function that gets called each time Now() is called The default machine allows the usage of freeze, scale and jump features of Jumper. This allows you to completely switch out the functionality

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

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