lnd.xprv/clock/test_clock_test.go
Andras Banki-Horvath 7024f36a76 general: adding the Clock interface to aid testing
This commit adds Clock and DefaultClock and moves the private
invoices.testClock under the clock package while adding basic
unit tests for it.
Clock is an interface currently encapsulating Now() and TickAfter().
It can be added as an external dependency to any class. This way
tests can stub out time.Now() or time.After().

The DefaultClock class simply returns the real time.Now() and
time.After().
2019-12-13 16:52:22 +01:00

64 lines
1.2 KiB
Go

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)
}