-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |