From 9cd4835b8171af24df395bd02e41b8375341cfe3 Mon Sep 17 00:00:00 2001 From: frrist Date: Thu, 6 Jun 2019 09:00:22 -0700 Subject: [PATCH] add MockBlockClock --- go.mod | 1 + go.sum | 2 ++ plumbing/clock/testing.go | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 plumbing/clock/testing.go diff --git a/go.mod b/go.mod index 27a4b98739..56522482ef 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 2f28eadb38..67f6494bea 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/plumbing/clock/testing.go b/plumbing/clock/testing.go new file mode 100644 index 0000000000..8427639ea2 --- /dev/null +++ b/plumbing/clock/testing.go @@ -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) +}