Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 30-Jul-2020 | - | ||||
.gitignore | H A D | 30-Jul-2020 | 266 | 26 | 19 | |
.travis.yml | H A D | 30-Jul-2020 | 38 | 6 | 4 | |
LICENSE | H A D | 30-Jul-2020 | 11.1 KiB | 202 | 169 | |
README.md | H A D | 30-Jul-2020 | 1.4 KiB | 70 | 49 | |
clockwork.go | H A D | 30-Jul-2020 | 4.5 KiB | 180 | 123 |
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