Giter Club home page Giter Club logo

timemock's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.