Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Clock | 646 | 135 | 531 | 14 days ago | 15 | November 15, 2021 | 3 | mit | Go | |
Clock is a small library for mocking time in Go. | ||||||||||
Delorean | 464 | 794 | 50 | 3 years ago | 11 | December 06, 2012 | 7 | mit | Ruby | |
DISCONTINUED - Delorean lets you travel in time with Ruby by mocking Time.now | ||||||||||
Python Libfaketime | 59 | 2 years ago | 3 | gpl-2.0 | Python | |||||
A fast time mocking alternative to freezegun that wraps libfaketime. | ||||||||||
Timeshift Js | 55 | 2 | 8 | a year ago | 6 | June 01, 2022 | 3 | mit | JavaScript | |
Library for mocking JavaScript's Date object | ||||||||||
Abtime | 52 | a month ago | 8 | January 19, 2022 | mit | Go | ||||
A library for abstracting away from the literal Go time library, for testing and time control. | ||||||||||
Timex | 37 | 1 | 3 years ago | 3 | August 03, 2020 | other | Go | |||
A test-friendly replacement for golang's time package | ||||||||||
Time Travel | 33 | 3 years ago | 5 | September 04, 2018 | 8 | mit | Python | |||
python time libraries mocking | ||||||||||
Time Machine | 13 | 5 years ago | 1 | November 26, 2017 | bsd-3-clause | Haskell | ||||
A Haskell library to mock the current time. | ||||||||||
Clock | 8 | 2 years ago | mit | Go | ||||||
A Go (golang) library for mocking standard time, optionally also with context.Context | ||||||||||
Clock | 7 | 5 years ago | mit | Go | ||||||
Clock is a small library for mocking time in Go. |
Clock is a small library for mocking time in Go. It provides an interface
around the standard library's time
package so that the application
can use the realtime clock while tests can use the mock clock.
This module is no longer maintained.
Your application can maintain a Clock
variable that will allow realtime and
mock clocks to be interchangeable. For example, if you had an Application
type:
import "github.com/benbjohnson/clock"
type Application struct {
Clock clock.Clock
}
You could initialize it to use the realtime clock like this:
var app Application
app.Clock = clock.New()
...
Then all timers and time-related functionality should be performed from the
Clock
variable.
In your tests, you will want to use a Mock
clock:
import (
"testing"
"github.com/benbjohnson/clock"
)
func TestApplication_DoSomething(t *testing.T) {
mock := clock.NewMock()
app := Application{Clock: mock}
...
}
Now that you've initialized your application to use the mock clock, you can adjust the time programmatically. The mock clock always starts from the Unix epoch (midnight UTC on Jan 1, 1970).
The mock clock provides the same functions that the standard library's time
package provides. For example, to find the current time, you use the Now()
function:
mock := clock.NewMock()
// Find the current time.
mock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC
// Move the clock forward.
mock.Add(2 * time.Hour)
// Check the time again. It's 2 hours later!
mock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC
Timers and Tickers are also controlled by this same mock clock. They will only execute when the clock is moved forward:
mock := clock.NewMock()
count := 0
// Kick off a timer to increment every 1 mock second.
go func() {
ticker := mock.Ticker(1 * time.Second)
for {
<-ticker.C
count++
}
}()
runtime.Gosched()
// Move the clock forward 10 seconds.
mock.Add(10 * time.Second)
// This prints 10.
fmt.Println(count)