Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 24-Mar-2022 | - | ||||
.editorconfig | H A D | 24-Mar-2022 | 174 | 13 | 10 | |
.gitignore | H A D | 24-Mar-2022 | 275 | 28 | 20 | |
LICENSE | H A D | 24-Mar-2022 | 11.1 KiB | 202 | 169 | |
README.md | H A D | 24-Mar-2022 | 2 KiB | 81 | 53 | |
clockwork.go | H A D | 24-Mar-2022 | 4.8 KiB | 196 | 137 | |
ticker.go | H A D | 24-Mar-2022 | 2 KiB | 73 | 50 |
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