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

..19-Nov-2020-

.travis.ymlH A D19-Nov-202038 64

LICENSEH A D19-Nov-202011.1 KiB202169

README.mdH A D19-Nov-20201.3 KiB6242

clockwork.goH A D19-Nov-20204.2 KiB170116

README.md

1clockwork
2=========
3
4[![Build Status](https://travis-ci.org/jonboulle/clockwork.png?branch=master)](https://travis-ci.org/jonboulle/clockwork)
5[![godoc](https://godoc.org/github.com/jonboulle/clockwork?status.svg)](http://godoc.org/github.com/jonboulle/clockwork)
6
7a simple fake clock for golang
8
9# Usage
10
11Replace uses of the `time` package with the `clockwork.Clock` interface instead.
12
13For example, instead of using `time.Sleep` directly:
14
15```
16func my_func() {
17	time.Sleep(3 * time.Second)
18	do_something()
19}
20```
21
22inject a clock and use its `Sleep` method instead:
23
24```
25func my_func(clock clockwork.Clock) {
26	clock.Sleep(3 * time.Second)
27	do_something()
28}
29```
30
31Now you can easily test `my_func` with a `FakeClock`:
32
33```
34func TestMyFunc(t *testing.T) {
35	c := clockwork.NewFakeClock()
36
37	// Start our sleepy function
38	my_func(c)
39
40	// Ensure we wait until my_func is sleeping
41	c.BlockUntil(1)
42
43	assert_state()
44
45	// Advance the FakeClock forward in time
46	c.Advance(3)
47
48	assert_state()
49}
50```
51
52and in production builds, simply inject the real clock instead:
53```
54my_func(clockwork.NewRealClock())
55```
56
57See [example_test.go](example_test.go) for a full example.
58
59# Credits
60
61clockwork is inspired by @wickman's [threaded fake clock](https://gist.github.com/wickman/3840816), and the [Golang playground](http://blog.golang.org/playground#Faking time)
62