Skip to content

Commit

Permalink
Merge 72f3581 into eaea92e
Browse files Browse the repository at this point in the history
  • Loading branch information
wpaulino committed Feb 7, 2019
2 parents eaea92e + 72f3581 commit c6156bd
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 33 deletions.
6 changes: 6 additions & 0 deletions go.mod
Expand Up @@ -32,6 +32,8 @@ require (
github.com/kkdai/bstream v0.0.0-20181106074824-b3251f7901ec
github.com/lightninglabs/neutrino v0.0.0-20190115022559-351f5f06c6af
github.com/lightningnetwork/lightning-onion v0.0.0-20180605012408-ac4d9da8f1d6
github.com/lightningnetwork/lnd/queue v1.0.0
github.com/lightningnetwork/lnd/ticker v1.0.0
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796
github.com/miekg/dns v0.0.0-20171125082028-79bfde677fa8
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af // indirect
Expand All @@ -49,3 +51,7 @@ require (
gopkg.in/macaroon.v2 v2.0.0
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
)

replace github.com/lightningnetwork/lnd/ticker v1.0.0 => ./ticker

replace github.com/lightningnetwork/lnd/queue v1.0.0 => ./queue
4 changes: 4 additions & 0 deletions go.sum
Expand Up @@ -100,6 +100,10 @@ github.com/lightninglabs/neutrino v0.0.0-20190115022559-351f5f06c6af h1:JzoYbWqw
github.com/lightninglabs/neutrino v0.0.0-20190115022559-351f5f06c6af/go.mod h1:aR+E6cs+FTaIwIa/WLyvNsB8FZg8TiP3r0Led+4Q4gI=
github.com/lightningnetwork/lightning-onion v0.0.0-20180605012408-ac4d9da8f1d6 h1:ONLGrYJVQdbtP6CE/ff1KNWZtygRGEh12RzonTiCzPs=
github.com/lightningnetwork/lightning-onion v0.0.0-20180605012408-ac4d9da8f1d6/go.mod h1:8EgEt4a/NUOVQd+3kk6n9aZCJ1Ssj96Pb6lCrci+6oc=
github.com/lightningnetwork/lnd/queue v1.0.0 h1:dlYHA+4Z+GZhb6cJZhE8rVC1QJWWOxQHk8HpOh4q9RA=
github.com/lightningnetwork/lnd/queue v1.0.0/go.mod h1:JFLKmMmBT/YST+w7RwJnW0LXrpCOWDEseWTBg2DqSFA=
github.com/lightningnetwork/lnd/ticker v1.0.0 h1:Ih47JUL4sBCQknjbBsdblSjQpXj4w6AXhkD3HWf6sNE=
github.com/lightningnetwork/lnd/ticker v1.0.0/go.mod h1:iaLXJiVgI1sPANIF2qYYUJXjoksPNvGNYowB8aRbpX0=
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw=
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796/go.mod h1:3p7ZTf9V1sNPI5H8P3NkTFF4LuwMdPl2DodF60qAKqY=
github.com/ltcsuite/ltcutil v0.0.0-20181217130922-17f3b04680b6 h1:b/Op1jKdoE6tzGyjzFx8gc7ZyW3hVFs1jUCQfM/Z2Jo=
Expand Down
10 changes: 5 additions & 5 deletions htlcswitch/link_test.go
Expand Up @@ -1545,7 +1545,7 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (

// Instantiate with a long interval, so that we can precisely control
// the firing via force feeding.
bticker := ticker.MockNew(time.Hour)
bticker := ticker.NewForce(time.Hour)
aliceCfg := ChannelLinkConfig{
FwrdingPolicy: globalPolicy,
Peer: alicePeer,
Expand All @@ -1568,7 +1568,7 @@ func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) (
Registry: invoiceRegistry,
ChainEvents: &contractcourt.ChainEventSubscription{},
BatchTicker: bticker,
FwdPkgGCTicker: ticker.MockNew(15 * time.Second),
FwdPkgGCTicker: ticker.NewForce(15 * time.Second),
// Make the BatchSize and Min/MaxFeeUpdateTimeout large enough
// to not trigger commit updates automatically during tests.
BatchSize: 10000,
Expand Down Expand Up @@ -3506,7 +3506,7 @@ func TestChannelLinkShutdownDuringForward(t *testing.T) {
// unblocks after nothing has been pulled for two seconds.
waitForBobsSwitchToBlock := func() {
bobSwitch := n.firstBobChannelLink.cfg.Switch
ticker := bobSwitch.cfg.LogEventTicker.(*ticker.Mock)
ticker := bobSwitch.cfg.LogEventTicker.(*ticker.Force)
timeout := time.After(15 * time.Second)
for {
time.Sleep(50 * time.Millisecond)
Expand All @@ -3525,7 +3525,7 @@ func TestChannelLinkShutdownDuringForward(t *testing.T) {
// Define a helper method that strobes the link's batch ticker, and
// unblocks after nothing has been pulled for two seconds.
waitForBobsIncomingLinkToBlock := func() {
ticker := n.firstBobChannelLink.cfg.BatchTicker.(*ticker.Mock)
ticker := n.firstBobChannelLink.cfg.BatchTicker.(*ticker.Force)
timeout := time.After(15 * time.Second)
for {
time.Sleep(50 * time.Millisecond)
Expand Down Expand Up @@ -4060,7 +4060,7 @@ func restartLink(aliceChannel *lnwallet.LightningChannel, aliceSwitch *Switch,

// Instantiate with a long interval, so that we can precisely control
// the firing via force feeding.
bticker := ticker.MockNew(time.Hour)
bticker := ticker.NewForce(time.Hour)
aliceCfg := ChannelLinkConfig{
FwrdingPolicy: globalPolicy,
Peer: alicePeer,
Expand Down
4 changes: 2 additions & 2 deletions htlcswitch/mock.go
Expand Up @@ -163,8 +163,8 @@ func initSwitchWithDB(startingHeight uint32, db *channeldb.DB) (*Switch, error)
return nil, nil
},
Notifier: &mockNotifier{},
FwdEventTicker: ticker.MockNew(DefaultFwdEventInterval),
LogEventTicker: ticker.MockNew(DefaultLogInterval),
FwdEventTicker: ticker.NewForce(DefaultFwdEventInterval),
LogEventTicker: ticker.NewForce(DefaultLogInterval),
NotifyActiveChannel: func(wire.OutPoint) {},
NotifyInactiveChannel: func(wire.OutPoint) {},
}
Expand Down
2 changes: 1 addition & 1 deletion htlcswitch/switch_test.go
Expand Up @@ -1924,7 +1924,7 @@ func TestMultiHopPaymentForwardingEvents(t *testing.T) {

// After sending 5 of the payments, trigger the forwarding ticker, to
// make sure the events are properly flushed.
bobTicker, ok := n.bobServer.htlcSwitch.cfg.FwdEventTicker.(*ticker.Mock)
bobTicker, ok := n.bobServer.htlcSwitch.cfg.FwdEventTicker.(*ticker.Force)
if !ok {
t.Fatalf("mockTicker assertion failed")
}
Expand Down
4 changes: 2 additions & 2 deletions htlcswitch/test_utils.go
Expand Up @@ -1041,8 +1041,8 @@ func (h *hopNetwork) createChannelLink(server, peer *mockServer,
ChainEvents: &contractcourt.ChainEventSubscription{},
SyncStates: true,
BatchSize: 10,
BatchTicker: ticker.MockNew(batchTimeout),
FwdPkgGCTicker: ticker.MockNew(fwdPkgTimeout),
BatchTicker: ticker.NewForce(batchTimeout),
FwdPkgGCTicker: ticker.NewForce(fwdPkgTimeout),
MinFeeUpdateTimeout: minFeeUpdateTimeout,
MaxFeeUpdateTimeout: maxFeeUpdateTimeout,
OnChannelFailure: func(lnwire.ChannelID, lnwire.ShortChannelID, LinkFailureError) {},
Expand Down
3 changes: 3 additions & 0 deletions queue/go.mod
@@ -0,0 +1,3 @@
module github.com/lightningnetwork/lnd/queue

require github.com/lightningnetwork/lnd/ticker v1.0.0
2 changes: 2 additions & 0 deletions queue/go.sum
@@ -0,0 +1,2 @@
github.com/lightningnetwork/lnd/ticker v1.0.0 h1:Ih47JUL4sBCQknjbBsdblSjQpXj4w6AXhkD3HWf6sNE=
github.com/lightningnetwork/lnd/ticker v1.0.0/go.mod h1:iaLXJiVgI1sPANIF2qYYUJXjoksPNvGNYowB8aRbpX0=
23 changes: 13 additions & 10 deletions ticker/mock.go → ticker/force.go
Expand Up @@ -6,9 +6,9 @@ import (
"time"
)

// Mock implements the Ticker interface, and provides a method of
// force-feeding ticks, even while paused.
type Mock struct {
// Force implements the Ticker interface, and provides a method of force-feeding
// ticks, even while paused.
type Force struct {
isActive uint32 // used atomically

// Force is used to force-feed a ticks into the ticker. Useful for
Expand All @@ -22,10 +22,13 @@ type Mock struct {
quit chan struct{}
}

// MockNew returns a Mock Ticker, used for testing and debugging. It supports
// A compile-time constraint to ensure Force satisfies the Ticker interface.
var _ Ticker = (*Force)(nil)

// NewForce returns a Force ticker, used for testing and debugging. It supports
// the ability to force-feed events that get output by the
func MockNew(interval time.Duration) *Mock {
m := &Mock{
func NewForce(interval time.Duration) *Force {
m := &Force{
ticker: time.NewTicker(interval).C,
Force: make(chan time.Time),
skip: make(chan struct{}),
Expand Down Expand Up @@ -64,23 +67,23 @@ func MockNew(interval time.Duration) *Mock {
// time.
//
// NOTE: Part of the Ticker interface.
func (m *Mock) Ticks() <-chan time.Time {
func (m *Force) Ticks() <-chan time.Time {
return m.Force
}

// Resume starts underlying time.Ticker and causes the ticker to begin
// delivering scheduled events.
//
// NOTE: Part of the Ticker interface.
func (m *Mock) Resume() {
func (m *Force) Resume() {
atomic.StoreUint32(&m.isActive, 1)
}

// Pause suspends the underlying ticker, such that Ticks() stops signaling at
// regular intervals.
//
// NOTE: Part of the Ticker interface.
func (m *Mock) Pause() {
func (m *Force) Pause() {
atomic.StoreUint32(&m.isActive, 0)

// If the ticker fired and read isActive as true, it may still send the
Expand All @@ -95,7 +98,7 @@ func (m *Mock) Pause() {
// regular intervals, and permanently frees up any resources.
//
// NOTE: Part of the Ticker interface.
func (m *Mock) Stop() {
func (m *Force) Stop() {
m.Pause()
close(m.quit)
m.wg.Wait()
Expand Down
1 change: 1 addition & 0 deletions ticker/go.mod
@@ -0,0 +1 @@
module github.com/lightningnetwork/lnd/ticker
27 changes: 15 additions & 12 deletions ticker/ticker.go
Expand Up @@ -46,7 +46,7 @@ type Ticker interface {
// Pause suspends the underlying ticker, such that Ticks() stops
// signaling at regular intervals. After calling Pause, the ticker
// should not send any ticks scheduled with the chosen interval. Forced
// ticks are still permissible, as in the case of the Mock Ticker.
// ticks are still permissible, as in the case of the Force Ticker.
//
// NOTE: It MUST be safe to call Pause at any time, and more than once
// successively.
Expand All @@ -60,10 +60,10 @@ type Ticker interface {
Stop()
}

// ticker is the production implementation of the resumable Ticker interface.
// This allows various components to toggle their need for tick events, which
// may vary depending on system load.
type ticker struct {
// T is the production implementation of the resumable Ticker interface. This
// allows various components to toggle their need for tick events, which may
// vary depending on system load.
type T struct {
// interval is the desired duration between ticks when active.
interval time.Duration

Expand All @@ -73,10 +73,13 @@ type ticker struct {
ticker *time.Ticker
}

// A compile-time constraint to ensure T satisfies the Ticker interface.
var _ Ticker = (*T)(nil)

// New returns a new ticker that signals with the given interval when not
// paused. The ticker starts off inactive.
func New(interval time.Duration) Ticker {
return &ticker{
func New(interval time.Duration) *T {
return &T{
interval: interval,
}
}
Expand All @@ -85,18 +88,18 @@ func New(interval time.Duration) Ticker {
// prescribed interval. This method returns nil when the ticker is paused.
//
// NOTE: Part of the Ticker interface.
func (t *ticker) Ticks() <-chan time.Time {
func (t *T) Ticks() <-chan time.Time {
if t.ticker == nil {
return nil
}
return t.ticker.C
}

// Resumes starts underlying time.Ticker and causes the ticker to begin
// Resume starts underlying time.Ticker and causes the ticker to begin
// delivering scheduled events.
//
// NOTE: Part of the Ticker interface.
func (t *ticker) Resume() {
func (t *T) Resume() {
if t.ticker == nil {
t.ticker = time.NewTicker(t.interval)
}
Expand All @@ -106,7 +109,7 @@ func (t *ticker) Resume() {
// regular intervals.
//
// NOTE: Part of the Ticker interface.
func (t *ticker) Pause() {
func (t *T) Pause() {
if t.ticker != nil {
t.ticker.Stop()
t.ticker = nil
Expand All @@ -118,6 +121,6 @@ func (t *ticker) Pause() {
// implementation, this is equivalent to Pause.
//
// NOTE: Part of the Ticker interface.
func (t *ticker) Stop() {
func (t *T) Stop() {
t.Pause()
}
2 changes: 1 addition & 1 deletion ticker/ticker_test.go
Expand Up @@ -20,7 +20,7 @@ var tickers = []struct {
},
{
"mock ticker",
ticker.MockNew(interval),
ticker.NewForce(interval),
},
}

Expand Down

0 comments on commit c6156bd

Please sign in to comment.