├── .travis.yml ├── LICENSE ├── README.md ├── clock.go ├── clock_test.go ├── mock.go ├── mock_test.go ├── real.go ├── real_test.go └── suite_test.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.5 4 | - 1.6 5 | - 1.7 6 | 7 | install: 8 | - go get -v -t ./... 9 | 10 | script: 11 | - go build 12 | - go test -v -covermode=count -coverprofile=profile.cov ./... 13 | 14 | after_success: 15 | - go get -u github.com/mattn/goveralls 16 | - ~/gopath/bin/goveralls -coverprofile=profile.cov -service=travis-ci 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 - 101 Loops UG 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | clock [![Build Status](https://secure.travis-ci.org/stephanos/clock.png)](https://travis-ci.org/stephanos/clock) [![Coverage Status](https://coveralls.io/repos/stephanos/clock/badge.png)](https://coveralls.io/r/stephanos/clock) [![GoDoc](https://camo.githubusercontent.com/6bae67c5189d085c05271a127da5a4bbb1e8eb2c/68747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f736d61727479737472656574732f676f636f6e7665793f7374617475732e706e67)](http://godoc.org/github.com/stephanos/clock) 2 | ====== 3 | 4 | This Go package provides a `Clock` that returns the time; and can be mocked. 5 | 6 | 7 | ## Install 8 | ```bash 9 | go get github.com/stephanos/clock 10 | ``` 11 | 12 | ## Documentation 13 | [godoc.org](http://godoc.org/github.com/stephanos/clock) 14 | 15 | ## License 16 | MIT (see LICENSE). 17 | -------------------------------------------------------------------------------- /clock.go: -------------------------------------------------------------------------------- 1 | package clock 2 | 3 | import "time" 4 | 5 | // Work mirrors the behaviour of Go's time package. 6 | var Work Clock 7 | 8 | func init() { 9 | Work = New() 10 | } 11 | 12 | // Now returns the current local time. 13 | func Now() time.Time { 14 | return Work.Now() 15 | } 16 | 17 | // Sleep pauses the current goroutine for at least the duration d. 18 | // A negative or zero duration causes Sleep to return immediately. 19 | func Sleep(d time.Duration) { 20 | Work.Sleep(d) 21 | } 22 | 23 | // After waits for the duration to elapse and then sends the current time 24 | // on the returned channel. It is equivalent to NewTimer(d).C. 25 | func After(d time.Duration) <-chan time.Time { 26 | return Work.After(d) 27 | } 28 | 29 | // Tick is a convenience wrapper for NewTicker providing access to the 30 | // ticking channel only. 31 | func Tick(d time.Duration) <-chan time.Time { 32 | return Work.Tick(d) 33 | } 34 | 35 | // Ticker returns a new Ticker containing a channel that will send the 36 | // time with a period specified by the duration argument. It adjusts 37 | // the intervals or drops ticks to make up for slow receivers. 38 | // The duration d must be greater than zero; if not, Ticker will panic. 39 | func Ticker(d time.Duration) *time.Ticker { 40 | return Work.Ticker(d) 41 | } 42 | 43 | // Clock provides the functions from the time package. 44 | type Clock interface { 45 | 46 | // Now returns the current local time. 47 | Now() time.Time 48 | 49 | // Sleep pauses the current goroutine for at least the duration d. 50 | // A negative or zero duration causes Sleep to return immediately. 51 | Sleep(d time.Duration) 52 | 53 | // After waits for the duration to elapse and then sends the current time 54 | // on the returned channel. It is equivalent to NewTimer(d).C. 55 | After(d time.Duration) <-chan time.Time 56 | 57 | // Tick is a convenience wrapper for NewTicker providing access to the 58 | // ticking channel only. 59 | Tick(d time.Duration) <-chan time.Time 60 | 61 | // Ticker returns a new Ticker containing a channel that will send the 62 | // time with a period specified by the duration argument. It adjusts 63 | // the intervals or drops ticks to make up for slow receivers. 64 | // The duration d must be greater than zero; if not, Ticker will panic. 65 | Ticker(d time.Duration) *time.Ticker 66 | 67 | // TODO: At(t time.Time) <-chan time.Time 68 | } 69 | 70 | // Mock represents a manipulable Work. It is concurrent-friendly. 71 | type Mock interface { 72 | Clock 73 | 74 | // ==== manipulate Now() 75 | 76 | // Set applies the passed-in time to the Clock's time. 77 | Set(t time.Time) Mock 78 | 79 | // Add changes the Clock's time by the passed-in duration. 80 | Add(d time.Duration) Mock 81 | 82 | // Freeze stops the clock's time. 83 | Freeze() Mock 84 | 85 | // Freeze stops the clock's time at the passed-in moment. 86 | FreezeAt(t time.Time) Mock 87 | 88 | // IsFrozen is whether the clock's time is stopped. 89 | IsFrozen() bool 90 | 91 | // Unfreeze starts the clock's time again. 92 | Unfreeze() Mock 93 | 94 | // ==== manipulate Sleep() 95 | 96 | // SetSleep overrides the passed-in argument to the Sleep method. 97 | SetSleep(d time.Duration) Mock 98 | 99 | // NoSleep disables the Sleep method. 100 | NoSleep() Mock 101 | 102 | // ResetSleep re-enables the default Sleep behaviour. 103 | ResetSleep() Mock 104 | 105 | // ==== manipulate After() 106 | 107 | // SetAfter overrides the passed-in argument to the After method. 108 | // SetAfter(d time.Duration) Mock 109 | } 110 | -------------------------------------------------------------------------------- /clock_test.go: -------------------------------------------------------------------------------- 1 | package clock 2 | 3 | import . "github.com/101loops/bdd" 4 | 5 | var _ = Describe("Clock Work", func() { 6 | 7 | It("Now()", func() { 8 | Work = New() 9 | Check(Now().Sub(now()), IsLessThan, delay) 10 | }) 11 | 12 | It("Sleep()", func() { 13 | slept := durationOf(func() { Sleep(delay) }) 14 | Check(slept, IsRoughly, delay, threshold) 15 | }) 16 | 17 | It("After()", func() { 18 | elapsed := durationOf(func() { <-After(delay) }) 19 | Check(elapsed, IsRoughly, delay, threshold) 20 | }) 21 | 22 | It("Ticker()", func() { 23 | ticker := Ticker(delay).C 24 | elapsed := durationOf(func() { <-ticker; <-ticker }) 25 | Check(elapsed, IsRoughly, 2*delay, threshold) 26 | }) 27 | 28 | It("Tick()", func() { 29 | elapsed := durationOf(func() { <-Tick(delay); <-Tick(delay) }) 30 | Check(elapsed, IsRoughly, 2*delay, threshold) 31 | }) 32 | }) 33 | -------------------------------------------------------------------------------- /mock.go: -------------------------------------------------------------------------------- 1 | package clock 2 | 3 | import ( 4 | "sync" 5 | "time" 6 | ) 7 | 8 | type mock struct { 9 | base time.Time 10 | setAt time.Time 11 | frozen bool 12 | timeMutex sync.Mutex 13 | 14 | sleep time.Duration 15 | sleepMutex sync.Mutex 16 | } 17 | 18 | // NewMock returns a new manipulable Clock. 19 | func NewMock() Mock { 20 | return &mock{ 21 | base: time.Now(), 22 | setAt: time.Now(), 23 | sleep: -1, 24 | } 25 | } 26 | 27 | func (c *mock) Now() time.Time { 28 | defer c.timeMutex.Unlock() 29 | c.timeMutex.Lock() 30 | 31 | if c.frozen { 32 | return c.setAt 33 | } 34 | 35 | return c.base.Add(c.elapsed()) 36 | } 37 | 38 | func (c *mock) Set(t time.Time) Mock { 39 | defer c.timeMutex.Unlock() 40 | c.timeMutex.Lock() 41 | 42 | c.base = t 43 | c.setAt = time.Now() 44 | return c 45 | } 46 | 47 | func (c *mock) Add(d time.Duration) Mock { 48 | defer c.timeMutex.Unlock() 49 | c.timeMutex.Lock() 50 | 51 | c.base = c.base.Add(d) 52 | if c.frozen { 53 | c.setAt = c.setAt.Add(d) 54 | } else { 55 | c.setAt = time.Now() 56 | } 57 | return c 58 | } 59 | 60 | func (c *mock) Freeze() Mock { 61 | return c.FreezeAt(c.Now()) 62 | } 63 | 64 | func (c *mock) IsFrozen() bool { 65 | defer c.timeMutex.Unlock() 66 | c.timeMutex.Lock() 67 | 68 | return c.frozen 69 | } 70 | 71 | func (c *mock) FreezeAt(t time.Time) Mock { 72 | defer c.timeMutex.Unlock() 73 | c.timeMutex.Lock() 74 | 75 | c.frozen = true 76 | c.setAt = t 77 | return c 78 | } 79 | 80 | func (c *mock) Unfreeze() Mock { 81 | defer c.timeMutex.Unlock() 82 | c.timeMutex.Lock() 83 | 84 | c.setAt = c.setAt.Add(c.elapsed()) 85 | c.frozen = false 86 | return c 87 | } 88 | 89 | func (c *mock) SetSleep(d time.Duration) Mock { 90 | defer c.sleepMutex.Unlock() 91 | c.sleepMutex.Lock() 92 | 93 | c.sleep = d 94 | return c 95 | } 96 | 97 | func (c *mock) NoSleep() Mock { 98 | c.SetSleep(0) 99 | return c 100 | } 101 | 102 | func (c *mock) ResetSleep() Mock { 103 | c.SetSleep(-1) 104 | return c 105 | } 106 | 107 | func (c *mock) Sleep(d time.Duration) { 108 | c.sleepMutex.Lock() 109 | override := c.sleep 110 | c.sleepMutex.Unlock() 111 | 112 | if override == -1 { 113 | time.Sleep(d) 114 | } else { 115 | time.Sleep(override) 116 | } 117 | } 118 | 119 | // elapsed returns the Duration between the date the time was set and now. 120 | func (c *mock) elapsed() time.Duration { 121 | return time.Now().Sub(c.setAt) 122 | } 123 | 124 | func (*mock) Tick(d time.Duration) <-chan time.Time { 125 | // TODO: make mockable 126 | return time.Tick(d) 127 | } 128 | 129 | func (*mock) Ticker(d time.Duration) *time.Ticker { 130 | // TODO: make mockable 131 | return time.NewTicker(d) 132 | } 133 | 134 | func (c *mock) After(d time.Duration) <-chan time.Time { 135 | // TODO: make mockable 136 | return time.After(d) 137 | } 138 | -------------------------------------------------------------------------------- /mock_test.go: -------------------------------------------------------------------------------- 1 | package clock 2 | 3 | import ( 4 | . "github.com/101loops/bdd" 5 | "time" 6 | ) 7 | 8 | var ( 9 | fixedTime = time.Unix(1415926535, 0) 10 | ) 11 | 12 | var _ = Describe("Mock Clock", func() { 13 | 14 | It("returns the time", func() { 15 | clock := NewMock() 16 | Check(clock.IsFrozen(), IsFalse) 17 | Check(timeDiff(clock), IsLessThan, 1*time.Millisecond) 18 | }) 19 | 20 | It("sets time", func() { 21 | clock := NewMock().Set(fixedTime) 22 | Check(clock.Now().Sub(fixedTime), IsLessThan, 1*time.Millisecond) 23 | 24 | time.Sleep(delay) 25 | Check(clock.Now().Sub(fixedTime), IsRoughly, delay, threshold) 26 | }) 27 | 28 | It("adds time when not frozen", func() { 29 | clock := NewMock().Add(1 * time.Hour) 30 | Check(timeDiff(clock), IsRoughly, -1*time.Hour, threshold) 31 | }) 32 | 33 | It("adds time when frozen", func() { 34 | clock := NewMock().Freeze() 35 | clock.Add(1 * time.Hour) 36 | Check(timeDiff(clock), IsRoughly, -1*time.Hour, threshold) 37 | }) 38 | 39 | It("freezes", func() { 40 | clock := NewMock().Add(1 * time.Hour).Freeze() 41 | Check(clock.IsFrozen(), IsTrue) 42 | clockNow := clock.Now() 43 | 44 | time.Sleep(delay) 45 | 46 | Check(clock.Now(), IsSameTimeAs, clockNow) 47 | }) 48 | 49 | It("freezes at passed-in time", func() { 50 | clock := NewMock().FreezeAt(fixedTime) 51 | Check(clock.IsFrozen(), IsTrue) 52 | 53 | time.Sleep(delay) 54 | 55 | Check(clock.Now(), IsSameTimeAs, fixedTime) 56 | }) 57 | 58 | It("unfreezes", func() { 59 | clock := NewMock().Add(1 * time.Hour).Freeze() 60 | Check(clock.IsFrozen(), IsTrue) 61 | 62 | time.Sleep(delay) 63 | 64 | clock.Unfreeze() 65 | Check(clock.IsFrozen(), IsFalse) 66 | Check(timeDiff(clock), IsRoughly, -1*time.Hour+delay, threshold) 67 | }) 68 | 69 | It("can sleep", func() { 70 | clock := NewMock() 71 | 72 | slept := durationOf(func() { clock.Sleep(delay) }) 73 | Check(slept, IsRoughly, delay, threshold) 74 | }) 75 | 76 | It("disables sleep", func() { 77 | clock := NewMock().NoSleep() 78 | 79 | slept := durationOf(func() { clock.Sleep(delay) }) 80 | Check(slept, IsLessThan, delay) 81 | }) 82 | 83 | It("overwrites sleep argument", func() { 84 | clock := NewMock().SetSleep(delay) 85 | 86 | slept := durationOf(func() { clock.Sleep(2 * time.Second) }) 87 | Check(slept, IsRoughly, delay, threshold) 88 | }) 89 | 90 | It("resets sleep override", func() { 91 | clock := NewMock().SetSleep(2 * time.Second) 92 | clock.ResetSleep() 93 | 94 | slept := durationOf(func() { clock.Sleep(delay) }) 95 | Check(slept, IsRoughly, delay, threshold) 96 | }) 97 | }) 98 | -------------------------------------------------------------------------------- /real.go: -------------------------------------------------------------------------------- 1 | package clock 2 | 3 | import "time" 4 | 5 | type realClock struct{} 6 | 7 | // New returns a new Clock that mirrors the behaviour of the time package. 8 | func New() Clock { 9 | return &realClock{} 10 | } 11 | 12 | func (*realClock) Now() time.Time { 13 | return time.Now() 14 | } 15 | 16 | func (*realClock) Sleep(d time.Duration) { 17 | time.Sleep(d) 18 | } 19 | 20 | func (*realClock) Tick(d time.Duration) <-chan time.Time { 21 | return time.Tick(d) 22 | } 23 | 24 | func (*realClock) Ticker(d time.Duration) *time.Ticker { 25 | return time.NewTicker(d) 26 | } 27 | 28 | func (*realClock) After(d time.Duration) <-chan time.Time { 29 | return time.After(d) 30 | } 31 | -------------------------------------------------------------------------------- /real_test.go: -------------------------------------------------------------------------------- 1 | package clock 2 | 3 | import . "github.com/101loops/bdd" 4 | 5 | var _ = Describe("Real Clock", func() { 6 | 7 | var clock = New() 8 | 9 | It("Now()", func() { 10 | clockNow := clock.Now() 11 | Check(clockNow.Sub(now()), IsLessThan, delay) 12 | }) 13 | 14 | It("Sleep()", func() { 15 | slept := durationOf(func() { clock.Sleep(delay) }) 16 | Check(slept, IsRoughly, delay, threshold) 17 | }) 18 | 19 | It("After()", func() { 20 | elapsed := durationOf(func() { <-clock.After(delay) }) 21 | Check(elapsed, IsRoughly, delay, threshold) 22 | }) 23 | 24 | It("Ticker()", func() { 25 | ticker := clock.Ticker(delay).C 26 | elapsed := durationOf(func() { <-ticker; <-ticker }) 27 | Check(elapsed, IsRoughly, 2*delay, threshold) 28 | }) 29 | 30 | It("Tick()", func() { 31 | elapsed := durationOf(func() { <-clock.Tick(delay); <-clock.Tick(delay) }) 32 | Check(elapsed, IsRoughly, 2*delay, threshold) 33 | }) 34 | }) 35 | -------------------------------------------------------------------------------- /suite_test.go: -------------------------------------------------------------------------------- 1 | package clock 2 | 3 | import ( 4 | . "github.com/101loops/bdd" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | var ( 10 | delay = 200 * time.Millisecond 11 | threshold = 100 * time.Millisecond 12 | ) 13 | 14 | func TestSuite(t *testing.T) { 15 | RunSpecs(t, "Clock Suite") 16 | } 17 | 18 | func now() time.Time { 19 | return time.Now() 20 | } 21 | 22 | func timeDiff(c Clock) time.Duration { 23 | return time.Now().Sub(c.Now()) 24 | } 25 | 26 | func durationOf(fn func()) time.Duration { 27 | beforeSleep := now() 28 | fn() 29 | return now().Sub(beforeSleep) 30 | } 31 | --------------------------------------------------------------------------------