Help us shape the future of Go! Take the 2026 Go Developer Survey

timemock

package module
v0.0.0-...-7c8c611 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 2 Imported by: 3

README

timemock

Build Status codecov Go Report Card

Inspired by Timecop Ruby Gem. It provides methods for Freezing time, Traveling in time, and scaling time for testing.

Motivation

Since in golang we can't override time package functions for mock purposes, we needed another solution to mock time.

Install

$ go get -u github.com/BorisBorshevsky/timemock

How to use

There are 2 ways (at least) to use this package.

  • Use your own clock (and pass it as needed)
  • Use the standard clock as a shared one between all the packages

both will have the same interface

type Clock interface {
	Now() time.Time
	Since(time.Time) time.Duration
	Freeze(time.Time)
	Travel(time.Time)
	Scale(float64)
	Return()
}

Im order to use the second method (shared clock), all the usages of time.Now and time.Since in your code (including non test code) should be replaced with timemock.Now and timemock.Since.

Examples

Global
timemock.Now() //time now

timemock.Freeze(timemock.Now()) //time is frozen

timemock.Return() //time is unfrozen

dummyTime := time.Unix(1522549800, 0) // Sunday, April 1, 2018 2:30:00 AM

timemock.Travel(dummyTime) //we traveled to 1st April

timemock.Scale(5) //time runs 5 times faster now

timemock.Return() // all is back to normal again

Travel
clock := timemock.New()

timeForTest := time.Unix(1522549800, 0)
clock.Travel(timeForTest) //April 1, 2018 02:30:00

//what ever code that runs 10 seconds...
// ...
time.Sleep(time.Second * 10)
// ...

clock.Now() //April 1, 2018 02:30:10
clock.Return() //time is untraveled and back to regular time
Freeze
clock := timemock.New()

timeForTest := time.Unix(1522549800, 0)
clock.Freeze(timeForTest) //April 1, 2018 02:30:00

//what ever code that runs 10 seconds...
// ...
time.Sleep(time.Second * 10)
// ...


clock.Now() //April 1, 2018 02:30:00, time is still frozen
clock.Return() //time is unfrozen and back to regular time
Scale
clock := timemock.New()

clock.Scale(6) 

//what ever code that runs 10 seconds...
// ...
time.Sleep(time.Second * 10)
// ...

//clock will think that 10 * 6 = 60 seconds passed
clock.Now() //April 1, 2018 02:31:00, time is still frozen

Travel can be used together with scale

Contact

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Freeze

func Freeze(t time.Time)

func Now

func Now() time.Time

func Return

func Return()

func Scale

func Scale(s float64)

func Since

func Since(t time.Time) time.Duration

func Travel

func Travel(t time.Time)

func Until

func Until(t time.Time) time.Duration

Types

type Clock

type Clock interface {
	Now() time.Time
	Since(time.Time) time.Duration
	Until(time.Time) time.Duration
	Freeze(time.Time)
	Travel(time.Time)
	Scale(float64)
	Return()
}

func New

func New() Clock

Directories

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

Jump to

Keyboard shortcuts

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