From c9915e027e02b21e01222c40afd8e6259592ab34 Mon Sep 17 00:00:00 2001 From: carla Date: Fri, 21 Feb 2020 13:24:23 +0200 Subject: [PATCH] contractcourt/test: replace timeouts with constants --- contractcourt/channel_arbitrator_test.go | 68 +++++++++++++----------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/contractcourt/channel_arbitrator_test.go b/contractcourt/channel_arbitrator_test.go index 0abfb381..2e7d4063 100644 --- a/contractcourt/channel_arbitrator_test.go +++ b/contractcourt/channel_arbitrator_test.go @@ -21,6 +21,14 @@ import ( "github.com/lightningnetwork/lnd/lnwire" ) +const ( + defaultTimeout = time.Second * 5 + + // stateTimeout is the timeout we allow when waiting for state + // transitions. + stateTimeout = time.Second * 15 +) + type mockArbitratorLog struct { state ArbitratorState newStates chan ArbitratorState @@ -221,7 +229,7 @@ func (c *chanArbTestCtx) AssertStateTransitions(expectedStates ...ArbitratorStat var state ArbitratorState select { case state = <-newStatesChan: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): c.t.Fatalf("new state not received") } @@ -441,7 +449,7 @@ func TestChannelArbitratorCooperativeClose(t *testing.T) { if c.CloseType != channeldb.CooperativeClose { t.Fatalf("expected cooperative close, got %v", c.CloseType) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("timeout waiting for channel close") } @@ -449,7 +457,7 @@ func TestChannelArbitratorCooperativeClose(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -504,7 +512,7 @@ func TestChannelArbitratorRemoteForceClose(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -566,7 +574,7 @@ func TestChannelArbitratorLocalForceClose(t *testing.T) { if state != StateBroadcastCommit { t.Fatalf("state during PublishTx was %v", state) } - case <-time.After(15 * time.Second): + case <-time.After(stateTimeout): t.Fatalf("did not get state update") } @@ -576,7 +584,7 @@ func TestChannelArbitratorLocalForceClose(t *testing.T) { select { case <-respChan: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -585,7 +593,7 @@ func TestChannelArbitratorLocalForceClose(t *testing.T) { if err != nil { t.Fatalf("error force closing channel: %v", err) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -610,7 +618,7 @@ func TestChannelArbitratorLocalForceClose(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -654,7 +662,7 @@ func TestChannelArbitratorBreachClose(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -738,7 +746,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) { ) select { case <-respChan: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -747,7 +755,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) { if err != nil { t.Fatalf("error force closing channel: %v", err) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -825,7 +833,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) { t.Fatalf("wrong htlc index: expected %v, got %v", outgoingDustHtlc.HtlcIndex, msgs[0].HtlcIndex) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("resolution msgs not sent") } @@ -887,7 +895,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) { // htlcTimeoutResolver and should send the contract off for incubation. select { case <-chanArbCtx.incubationRequests: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -907,7 +915,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) { t.Fatalf("wrong htlc index: expected %v, got %v", htlc.HtlcIndex, msgs[0].HtlcIndex) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("resolution msgs not sent") } @@ -927,7 +935,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) { chanArbCtxNew.AssertStateTransitions(StateFullyResolved) select { case <-chanArbCtxNew.resolvedChan: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -989,7 +997,7 @@ func TestChannelArbitratorLocalForceCloseRemoteConfirmed(t *testing.T) { if state != StateBroadcastCommit { t.Fatalf("state during PublishTx was %v", state) } - case <-time.After(15 * time.Second): + case <-time.After(stateTimeout): t.Fatalf("no state update received") } @@ -1000,7 +1008,7 @@ func TestChannelArbitratorLocalForceCloseRemoteConfirmed(t *testing.T) { // Wait for a response to the force close. select { case <-respChan: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -1009,7 +1017,7 @@ func TestChannelArbitratorLocalForceCloseRemoteConfirmed(t *testing.T) { if err != nil { t.Fatalf("error force closing channel: %v", err) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -1035,7 +1043,7 @@ func TestChannelArbitratorLocalForceCloseRemoteConfirmed(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(15 * time.Second): + case <-time.After(stateTimeout): t.Fatalf("contract was not resolved") } } @@ -1097,7 +1105,7 @@ func TestChannelArbitratorLocalForceDoubleSpend(t *testing.T) { if state != StateBroadcastCommit { t.Fatalf("state during PublishTx was %v", state) } - case <-time.After(15 * time.Second): + case <-time.After(stateTimeout): t.Fatalf("no state update received") } @@ -1108,7 +1116,7 @@ func TestChannelArbitratorLocalForceDoubleSpend(t *testing.T) { // Wait for a response to the force close. select { case <-respChan: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -1117,7 +1125,7 @@ func TestChannelArbitratorLocalForceDoubleSpend(t *testing.T) { if err != nil { t.Fatalf("error force closing channel: %v", err) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -1143,7 +1151,7 @@ func TestChannelArbitratorLocalForceDoubleSpend(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(15 * time.Second): + case <-time.After(stateTimeout): t.Fatalf("contract was not resolved") } } @@ -1264,7 +1272,7 @@ func TestChannelArbitratorPersistence(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -1328,7 +1336,7 @@ func TestChannelArbitratorForceCloseBreachedChannel(t *testing.T) { if state != StateBroadcastCommit { t.Fatalf("state during PublishTx was %v", state) } - case <-time.After(15 * time.Second): + case <-time.After(stateTimeout): t.Fatalf("no state update received") } @@ -1339,7 +1347,7 @@ func TestChannelArbitratorForceCloseBreachedChannel(t *testing.T) { t.Fatalf("unexpected error force closing channel: %v", err) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("no response received") } @@ -1363,7 +1371,7 @@ func TestChannelArbitratorForceCloseBreachedChannel(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -1467,7 +1475,7 @@ func TestChannelArbitratorCommitFailure(t *testing.T) { select { case <-closed: - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("channel was not marked closed") } @@ -1501,7 +1509,7 @@ func TestChannelArbitratorCommitFailure(t *testing.T) { select { case <-chanArbCtx.resolvedChan: // Expected. - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("contract was not resolved") } } @@ -1805,7 +1813,7 @@ func TestChannelArbitratorDanglingCommitForceClose(t *testing.T) { t.Fatalf("wrong htlc index: expected %v, got %v", htlcIndex, msgs[0].HtlcIndex) } - case <-time.After(5 * time.Second): + case <-time.After(defaultTimeout): t.Fatalf("resolution msgs not sent") }