

GitHub - benbjohnson/clock: Clock is a small library for mocking time in Go.
source link: https://github.com/benbjohnson/clock
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

clock
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.
Usage
Realtime Clock
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.
Mocking time
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).
Controlling time
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)
Recommend
-
145
mocking library for Kotlin Hello from Joao Pessoa, Brazil. 11 MockK users reside there. Next...Participate a sma...
-
50
mocking library for Kotlin. Provides concise DSL to mock object behaviour.
-
54
README.md genesis Genesis is a utility for embedding static file assets into Go files to be included in static compilation. Genesis includes support fo...
-
59
You can do good thing by donating for a stem cell therapy for my kid
-
49
README.md El mock
-
91
README.md Immutable
-
29
Litestream
-
6
Postlite Postlite is a network proxy to allow access to remote SQLite databases over the Postgres wire protocol. This allows GUI tools to be used on remote SQLite databases which can make administration easier. The proxy work...
-
9
Remove streaming replication implementation #411 Merged ...
-
4
WTF Dial This project provides a real-time dashboard for teams to view how f-cked up they currently are. Each team member provides input to specify the level at which they feel the team is currently messed up. These values range from 0...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK