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

..30-Jul-2020-

.gitignoreH A D30-Jul-2020266 2619

.travis.ymlH A D30-Jul-202038 64

LICENSEH A D30-Jul-202011.1 KiB202169

README.mdH A D30-Jul-20201.4 KiB7049

clockwork.goH A D30-Jul-20204.5 KiB180123

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	var wg sync.WaitGroup
39	wg.Add(1)
40	go func() {
41		my_func(c)
42		wg.Done()
43	}()
44
45	// Ensure we wait until my_func is sleeping
46	c.BlockUntil(1)
47
48	assert_state()
49
50	// Advance the FakeClock forward in time
51	c.Advance(3 * time.Second)
52
53	// Wait until the function completes
54	wg.Wait()
55
56	assert_state()
57}
58```
59
60and in production builds, simply inject the real clock instead:
61```
62my_func(clockwork.NewRealClock())
63```
64
65See [example_test.go](example_test.go) for a full example.
66
67# Credits
68
69clockwork 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)
70