• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..24-Mar-2022-

.editorconfigH A D24-Mar-2022174 1310

.gitignoreH A D24-Mar-2022275 2820

LICENSEH A D24-Mar-202211.1 KiB202169

README.mdH A D24-Mar-20222 KiB8153

clockwork.goH A D24-Mar-20224.8 KiB196137

ticker.goH A D24-Mar-20222 KiB7350

README.md

1# clockwork
2
3[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/avelino/awesome-go#utilities)
4
5[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/jonboulle/clockwork/CI?style=flat-square)](https://github.com/jonboulle/clockwork/actions?query=workflow%3ACI)
6[![Go Report Card](https://goreportcard.com/badge/github.com/jonboulle/clockwork?style=flat-square)](https://goreportcard.com/report/github.com/jonboulle/clockwork)
7![Go Version](https://img.shields.io/badge/go%20version-%3E=1.11-61CFDD.svg?style=flat-square)
8[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/mod/github.com/jonboulle/clockwork)
9
10**A simple fake clock for Go.**
11
12
13## Usage
14
15Replace uses of the `time` package with the `clockwork.Clock` interface instead.
16
17For example, instead of using `time.Sleep` directly:
18
19```go
20func myFunc() {
21	time.Sleep(3 * time.Second)
22	doSomething()
23}
24```
25
26Inject a clock and use its `Sleep` method instead:
27
28```go
29func myFunc(clock clockwork.Clock) {
30	clock.Sleep(3 * time.Second)
31	doSomething()
32}
33```
34
35Now you can easily test `myFunc` with a `FakeClock`:
36
37```go
38func TestMyFunc(t *testing.T) {
39	c := clockwork.NewFakeClock()
40
41	// Start our sleepy function
42	var wg sync.WaitGroup
43	wg.Add(1)
44	go func() {
45		myFunc(c)
46		wg.Done()
47	}()
48
49	// Ensure we wait until myFunc is sleeping
50	c.BlockUntil(1)
51
52	assertState()
53
54	// Advance the FakeClock forward in time
55	c.Advance(3 * time.Second)
56
57	// Wait until the function completes
58	wg.Wait()
59
60	assertState()
61}
62```
63
64and in production builds, simply inject the real clock instead:
65
66```go
67myFunc(clockwork.NewRealClock())
68```
69
70See [example_test.go](example_test.go) for a full example.
71
72
73# Credits
74
75clockwork is inspired by @wickman's [threaded fake clock](https://gist.github.com/wickman/3840816), and the [Golang playground](https://blog.golang.org/playground#TOC_3.1.)
76
77
78## License
79
80Apache License, Version 2.0. Please see [License File](LICENSE) for more information.
81