timejump

package module
v0.0.0-...-50c5a7a Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2017 License: MIT Imports: 2 Imported by: 1

README

timejump: Time jumper for testing in Go

Build Status GoDoc

If you want to mock time.Now for testing, use timejump.

go get github.com/agatan/timejump

Documentation

Overview

Package timejump allows you to mock `time.Now`.

Example
package main

import (
	"fmt"
	"time"

	"github.com/agatan/timejump"
)

func main() {
	timejump.Activate()
	defer timejump.Deactivate()

	future := time.Date(1985, 10, 26, 0, 0, 0, 0, time.UTC)
	timejump.Stop()
	timejump.Jump(future)

	fmt.Println(timejump.Now())

}
Output:

1985-10-26 00:00:00 +0000 UTC

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Activate

func Activate()

Activate enables timejump's mocking functionality. Call this at the top of test functions.

func Deactivate

func Deactivate()

Deactivate disables timejump's mocking functionality. Call this at the bottom of test functions (or use `defer` at the top).

func Jump

func Jump(t time.Time)

Jump jumps to the time t.

func Move

func Move(loc *time.Location)

Move sets the location of the time generated by `Now`.

Example
package main

import (
	"fmt"
	"time"

	"github.com/agatan/timejump"
)

func main() {
	timejump.Activate()
	defer timejump.Deactivate()

	timejump.Move(time.FixedZone("test/zone", 123))

	zone, offset := timejump.Now().Zone()

	fmt.Printf("zone: %v, offset: %v\n", zone, offset)

}
Output:

zone: test/zone, offset: 123

func Now

func Now() time.Time

Now returns the current time. If timejump is activated, it returns fake time calculated based on fake values set by `Jump`, `Move`, and `Scale`.

func Scale

func Scale(n int)

Scale sets the scale of the world speed.

Example
package main

import (
	"time"

	"github.com/agatan/timejump"
)

func main() {
	timejump.Activate()
	defer timejump.Deactivate()

	now := timejump.Now()
	// Time passes 100 times faster.
	timejump.Scale(1000)
	// Sleep just a millisecond.
	time.Sleep(time.Millisecond)

	sub := timejump.Now().Sub(now)

	if sub.Seconds() < 1 {
		panic("1 second have passed while sleeping 1 millisecond in this world.")
	}
}
Output:

func Stop

func Stop()

Stop stops the world (equal to `Scale(0)`).

Types

This section is empty.

Jump to

Keyboard shortcuts

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