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
16 changed files
with
1,147 additions
and
239 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,24 @@ | ||
package clock | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// DefaultClock implements Clock interface by simply calling the appropriate | ||
// time functions. | ||
type DefaultClock struct{} | ||
|
||
// NewDefaultClock constructs a new DefaultClock. | ||
func NewDefaultClock() Clock { | ||
return &DefaultClock{} | ||
} | ||
|
||
// Now simply returns time.Now(). | ||
func (DefaultClock) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
// TickAfter simply wraps time.After(). | ||
func (DefaultClock) TickAfter(duration time.Duration) <-chan time.Time { | ||
return time.After(duration) | ||
} |
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,16 @@ | ||
package clock | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Clock is an interface that provides a time functions for LND packages. | ||
// This is useful during testing when a concrete time reference is needed. | ||
type Clock interface { | ||
// Now returns the current local time (as defined by the Clock). | ||
Now() time.Time | ||
|
||
// TickAfter returns a channel that will receive a tick after the specified | ||
// duration has passed. | ||
TickAfter(duration time.Duration) <-chan time.Time | ||
} |
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,63 @@ | ||
package clock | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
var ( | ||
testTime = time.Date(2009, time.January, 3, 12, 0, 0, 0, time.UTC) | ||
) | ||
|
||
func TestNow(t *testing.T) { | ||
c := NewTestClock(testTime) | ||
now := c.Now() | ||
|
||
if now != testTime { | ||
t.Fatalf("expected: %v, got: %v", testTime, now) | ||
} | ||
|
||
now = now.Add(time.Hour) | ||
c.SetTime(now) | ||
if c.Now() != now { | ||
t.Fatalf("epected: %v, got: %v", now, c.Now()) | ||
} | ||
} | ||
|
||
func TestTickAfter(t *testing.T) { | ||
c := NewTestClock(testTime) | ||
|
||
// Should be ticking immediately. | ||
ticker0 := c.TickAfter(0) | ||
|
||
// Both should be ticking after SetTime | ||
ticker1 := c.TickAfter(time.Hour) | ||
ticker2 := c.TickAfter(time.Hour) | ||
|
||
// We don't expect this one to tick. | ||
ticker3 := c.TickAfter(2 * time.Hour) | ||
|
||
tickOrTimeOut := func(ticker <-chan time.Time, expectTick bool) { | ||
tick := false | ||
select { | ||
case <-ticker: | ||
tick = true | ||
case <-time.After(time.Millisecond): | ||
} | ||
|
||
if tick != expectTick { | ||
t.Fatalf("expected tick: %v, ticked: %v", expectTick, tick) | ||
} | ||
} | ||
|
||
tickOrTimeOut(ticker0, true) | ||
tickOrTimeOut(ticker1, false) | ||
tickOrTimeOut(ticker2, false) | ||
tickOrTimeOut(ticker3, false) | ||
|
||
c.SetTime(c.Now().Add(time.Hour)) | ||
|
||
tickOrTimeOut(ticker1, true) | ||
tickOrTimeOut(ticker2, true) | ||
tickOrTimeOut(ticker3, false) | ||
} |
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
Oops, something went wrong.