From abdd01ed9a182dd20dc2a81fcafa756a9e92812f Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 6 Feb 2019 16:48:54 -0800 Subject: [PATCH] ticker+htlcswitch: rename Mock -> Force --- htlcswitch/link_test.go | 10 +++++----- htlcswitch/mock.go | 4 ++-- htlcswitch/switch_test.go | 2 +- htlcswitch/test_utils.go | 4 ++-- ticker/mock.go | 23 +++++++++++++---------- ticker/ticker.go | 2 +- ticker/ticker_test.go | 2 +- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index c0b6e2a13..868b26269 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -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, @@ -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, @@ -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) @@ -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) @@ -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, diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 89913c17b..939488ab8 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -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) {}, } diff --git a/htlcswitch/switch_test.go b/htlcswitch/switch_test.go index 0513bdc28..59b0e4edc 100644 --- a/htlcswitch/switch_test.go +++ b/htlcswitch/switch_test.go @@ -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") } diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index 58bcd8565..868569153 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -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) {}, diff --git a/ticker/mock.go b/ticker/mock.go index b82d1604e..016e37020 100644 --- a/ticker/mock.go +++ b/ticker/mock.go @@ -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 @@ -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{}), @@ -64,7 +67,7 @@ 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 } @@ -72,7 +75,7 @@ func (m *Mock) Ticks() <-chan time.Time { // delivering scheduled events. // // NOTE: Part of the Ticker interface. -func (m *Mock) Resume() { +func (m *Force) Resume() { atomic.StoreUint32(&m.isActive, 1) } @@ -80,7 +83,7 @@ func (m *Mock) Resume() { // 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 @@ -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() diff --git a/ticker/ticker.go b/ticker/ticker.go index 782cbe5ac..5cf33ea40 100644 --- a/ticker/ticker.go +++ b/ticker/ticker.go @@ -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. diff --git a/ticker/ticker_test.go b/ticker/ticker_test.go index 2abd05432..81da3d7a2 100644 --- a/ticker/ticker_test.go +++ b/ticker/ticker_test.go @@ -20,7 +20,7 @@ var tickers = []struct { }, { "mock ticker", - ticker.MockNew(interval), + ticker.NewForce(interval), }, }