You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

  1. clock [![Build Status](https://drone.io/github.com/benbjohnson/clock/status.png)](https://drone.io/github.com/benbjohnson/clock/latest) [![Coverage Status](https://coveralls.io/repos/benbjohnson/clock/badge.png?branch=master)](https://coveralls.io/r/benbjohnson/clock?branch=master) [![GoDoc](https://godoc.org/github.com/benbjohnson/clock?status.png)](https://godoc.org/github.com/benbjohnson/clock) ![Project status](http://img.shields.io/status/experimental.png?color=red)
  2. =====
  3. Clock is a small library for mocking time in Go. It provides an interface
  4. around the standard library's [`time`][time] package so that the application
  5. can use the realtime clock while tests can use the mock clock.
  6. [time]: http://golang.org/pkg/time/
  7. ## Usage
  8. ### Realtime Clock
  9. Your application can maintain a `Clock` variable that will allow realtime and
  10. mock clocks to be interchangable. For example, if you had an `Application` type:
  11. ```go
  12. import "github.com/benbjohnson/clock"
  13. type Application struct {
  14. Clock clock.Clock
  15. }
  16. ```
  17. You could initialize it to use the realtime clock like this:
  18. ```go
  19. var app Application
  20. app.Clock = clock.New()
  21. ...
  22. ```
  23. Then all timers and time-related functionality should be performed from the
  24. `Clock` variable.
  25. ### Mocking time
  26. In your tests, you will want to use a `Mock` clock:
  27. ```go
  28. import (
  29. "testing"
  30. "github.com/benbjohnson/clock"
  31. )
  32. func TestApplication_DoSomething(t *testing.T) {
  33. mock := clock.NewMock()
  34. app := Application{Clock: mock}
  35. ...
  36. }
  37. ```
  38. Now that you've initialized your application to use the mock clock, you can
  39. adjust the time programmatically. The mock clock always starts from the Unix
  40. epoch (midnight, Jan 1, 1970 UTC).
  41. ### Controlling time
  42. The mock clock provides the same functions that the standard library's `time`
  43. package provides. For example, to find the current time, you use the `Now()`
  44. function:
  45. ```go
  46. mock := clock.NewMock()
  47. // Find the current time.
  48. mock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC
  49. // Move the clock forward.
  50. mock.Add(2 * time.Hour)
  51. // Check the time again. It's 2 hours later!
  52. mock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC
  53. ```
  54. Timers and Tickers are also controlled by this same mock clock. They will only
  55. execute when the clock is moved forward:
  56. ```
  57. mock := clock.NewMock()
  58. count := 0
  59. // Kick off a timer to increment every 1 mock second.
  60. go func() {
  61. ticker := clock.Ticker(1 * time.Second)
  62. for {
  63. <-ticker.C
  64. count++
  65. }
  66. }()
  67. runtime.Gosched()
  68. // Move the clock forward 10 second.
  69. mock.Add(10 * time.Second)
  70. // This prints 10.
  71. fmt.Println(count)
  72. ```