Skip to content

Commit

Permalink
add MockBlockClock
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist committed Jun 6, 2019
1 parent 9ee8457 commit 9cd4835
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
contrib.go.opencensus.io/exporter/prometheus v0.1.0
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/benbjohnson/clock v0.0.0-20161215174838-7dc76406b6d3
github.com/cskr/pubsub v1.0.2
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v0.7.3-0.20190315170154-87d593639c77
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/apache/thrift v0.12.0 h1:pODnxUFNcjP9UTLZGTdeh+j16A8lJbRvD3rOtrk/7bs=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/benbjohnson/clock v0.0.0-20161215174838-7dc76406b6d3 h1:wOysYcIdqv3WnvwqFFzrYCFALPED7qkUGaLXu359GSc=
github.com/benbjohnson/clock v0.0.0-20161215174838-7dc76406b6d3/go.mod h1:UMqtWQTnOe4byzwe7Zhwh8f8s+36uszN51sJrSIZlTE=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32 h1:qkOC5Gd33k54tobS36cXdAzJbeHaduLtnLQQwNoIi78=
Expand Down
52 changes: 52 additions & 0 deletions plumbing/clock/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package clock

import (
"time"

"github.com/benbjohnson/clock"
)

// MockBlockClock defines a testing structure used to mock a BlockClock.
type MockBlockClock struct {
blockClock *clock.Mock
blockTime time.Duration
}

// NewMockBlockClock returns a new MockBlockClock with the provided blockTime.
// Calls to MockBlockClock.EpochSeconds() will return 0 until a time is set using
// the method SetClock().
func NewMockBlockClock(blkTime time.Duration) *MockBlockClock {
return &MockBlockClock{
blockClock: clock.NewMock(),
blockTime: blkTime,
}
}

// BlockTime returns the block time of the MockBlockClock.
func (mbc *MockBlockClock) BlockTime() time.Duration {
return mbc.blockTime
}

// SetBlockTime sets the block time on the MockBlockClock.
func (mbc *MockBlockClock) SetBlockTime(t time.Duration) {
mbc.blockTime = t
}

// EpochSeconds returns the current wall time on the mock clock.
// All calls to EpochSeconds will return 0 until SetClock is called with the
// desired time.
func (mbc *MockBlockClock) EpochSeconds() int64 {
return mbc.blockClock.Now().Unix()
}

// SetClock sets the current time of the mock clock to a specific one.
// This should only be called from a single goroutine at a time.
func (mbc *MockBlockClock) SetClock(t time.Time) {
mbc.blockClock.Set(t)
}

// Add moves the current time of the mock clock forward by the duration.
// This should only be called from a single goroutine at a time.
func (mbc *MockBlockClock) Add(t time.Duration) {
mbc.blockClock.Add(t)
}

0 comments on commit 9cd4835

Please sign in to comment.