https://github.com/r3code/wallclock
Golang package to replace go time package allowing you to mock the time during tests
https://github.com/r3code/wallclock
golang mocking
Last synced: about 1 year ago
JSON representation
Golang package to replace go time package allowing you to mock the time during tests
- Host: GitHub
- URL: https://github.com/r3code/wallclock
- Owner: r3code
- License: mit
- Created: 2020-02-12T12:14:39.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-12T14:36:22.000Z (about 6 years ago)
- Last Synced: 2025-01-29T23:14:57.151Z (about 1 year ago)
- Topics: golang, mocking
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `wallclock`
[](https://travis-ci.org/r3code/wallclock)
[](https://coveralls.io/github/r3code/wallclock?branch=master)
[](https://godoc.org/github.com/r3code/wallclock)
`wallclock` Golang is a package to replace go `time` package allowing you to mock the time during test.
## Usage
Just replace your `time.Now()` by a `wallclock.Now()` call, etc.
## Mocking
Use `wallclock.FakeMoment(timeMoment)` or `wallclock.FakeFixedMoment()` to replace the current `wallclock.Now()` time reading function, and use the function it returns to restore the realtime.
## Notice
Package changing its internal global variable so you can't fake the time from several tests at the same time.
### Usage
```go
func TestWithFakeTime(t *testing.T) {
testMoment, _ := time.Parse(time.RFC3339, "1999-01-02T12:34:56.001Z")
t1 := wallclock.Now()
cancelFunc := wallclock.FakeMoment(testMoment)
defer cancelFunc()
fmt.Printf("%+v") // Outputs: 1999-01-02T12:34:56.001Z
}
```
## Drawbacks
### Performance
The indirection of the call makes `wallclock.Now` actually little slower.
It's not easy to get a stable result because the difference is small.
```
> go test -run=NONE -benchmem -benchtime=5s -bench=. .
goos: windows
goarch: 386
pkg: github.com/r3code/wallclock
BenchmarkTimeNow-4 234912168 25.5 ns/op 0 B/op
0 allocs/op
BenchmarkWallclockNow-4 142611286 42.1 ns/op 0 B/op
0 allocs/op
BenchmarkWallclockFakeMoment-4 447401670 13.4 ns/op 0 B/op
0 allocs/op
BenchmarkWallclockFakeFixedMoment-4 468723194 12.8 ns/op 0 B/op
0 allocs/op
PASS
ok github.com/r3code/wallclock 33.543s
```
## Faking time practice
Actually
> Changing the system time while making tests (or in general) is a bad idea.
> You don't know what depends on the system time while executing tests and you don't want to find out the hard way by spending days of debugging into that. Just don't do it."
by [nemo](https://stackoverflow.com/a/18970352/469898)
*But sometimes we need to do it*.