From 02e4c3ad4c192c379cdb3262c81e4633611710e9 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 19 Aug 2021 21:16:41 +0800 Subject: [PATCH] itest: manage context inside assertions - I This commit changes the methods assertTxLabel, assertReports, assertSweepFound, and sendAndAssertSuccess to manage their own context with deadline. --- lntest/itest/assertions.go | 26 ++++++++++++------- lntest/itest/lnd_amp_test.go | 8 ++---- lntest/itest/lnd_channel_force_close.go | 8 +++--- lntest/itest/lnd_etcd_failover_test.go | 24 ++++++++++------- lntest/itest/lnd_misc_test.go | 4 +-- .../lnd_multi-hop-error-propagation_test.go | 4 +-- lntest/itest/lnd_payment_test.go | 7 ++--- lntest/itest/lnd_routing_test.go | 3 +-- .../itest/lnd_send_multi_path_payment_test.go | 6 ++--- lntest/itest/lnd_single_hop_invoice_test.go | 12 +++------ 10 files changed, 48 insertions(+), 54 deletions(-) diff --git a/lntest/itest/assertions.go b/lntest/itest/assertions.go index 6819a23a6..7fe5dc78d 100644 --- a/lntest/itest/assertions.go +++ b/lntest/itest/assertions.go @@ -976,11 +976,11 @@ func checkPendingHtlcStageAndMaturity( // assertReports checks that the count of resolutions we have present per // type matches a set of expected resolutions. -func assertReports(ctxb context.Context, t *harnessTest, - node *lntest.HarnessNode, channelPoint wire.OutPoint, - expected map[string]*lnrpc.Resolution) { +func assertReports(t *harnessTest, node *lntest.HarnessNode, + channelPoint wire.OutPoint, expected map[string]*lnrpc.Resolution) { // Get our node's closed channels. + ctxb := context.Background() ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout) defer cancel() @@ -1011,11 +1011,13 @@ func assertReports(ctxb context.Context, t *harnessTest, } // assertSweepFound looks up a sweep in a nodes list of broadcast sweeps. -func assertSweepFound(ctx context.Context, t *testing.T, node *lntest.HarnessNode, +func assertSweepFound(t *testing.T, node *lntest.HarnessNode, sweep string, verbose bool) { // List all sweeps that alice's node had broadcast. - ctx, _ = context.WithTimeout(ctx, defaultTimeout) + ctxb := context.Background() + ctx, cancel := context.WithTimeout(ctxb, defaultTimeout) + defer cancel() sweepResp, err := node.WalletKitClient.ListSweeps( ctx, &walletrpc.ListSweepsRequest{ Verbose: verbose, @@ -1641,6 +1643,7 @@ func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error { func assertNumActiveHtlcsChanPoint(node *lntest.HarnessNode, chanPoint wire.OutPoint, numHtlcs int) error { + ctxb := context.Background() req := &lnrpc.ListChannelsRequest{} @@ -1732,12 +1735,13 @@ func getSpendingTxInMempool(t *harnessTest, miner *rpcclient.Client, // assertTxLabel is a helper function which finds a target tx in our set // of transactions and checks that it has the desired label. -func assertTxLabel(ctx context.Context, t *harnessTest, - node *lntest.HarnessNode, targetTx, label string) { +func assertTxLabel(t *harnessTest, node *lntest.HarnessNode, + targetTx, label string) { // List all transactions relevant to our wallet, and find the tx so that // we can check the correct label has been set. - ctxt, cancel := context.WithTimeout(ctx, defaultTimeout) + ctxb := context.Background() + ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout) defer cancel() txResp, err := node.GetTransactions( @@ -1756,9 +1760,13 @@ func assertTxLabel(ctx context.Context, t *harnessTest, // sendAndAssertSuccess sends the given payment requests and asserts that the // payment completes successfully. -func sendAndAssertSuccess(ctx context.Context, t *harnessTest, node *lntest.HarnessNode, +func sendAndAssertSuccess(t *harnessTest, node *lntest.HarnessNode, req *routerrpc.SendPaymentRequest) *lnrpc.Payment { + ctxb := context.Background() + ctx, cancel := context.WithTimeout(ctxb, defaultTimeout) + defer cancel() + var result *lnrpc.Payment err := wait.NoError(func() error { stream, err := node.RouterClient.SendPaymentV2(ctx, req) diff --git a/lntest/itest/lnd_amp_test.go b/lntest/itest/lnd_amp_test.go index 6cf17920d..505255e95 100644 --- a/lntest/itest/lnd_amp_test.go +++ b/lntest/itest/lnd_amp_test.go @@ -117,10 +117,8 @@ func testSendPaymentAMPInvoiceCase(net *lntest.NetworkHarness, t *harnessTest, require.NoError(t.t, err) } - ctxt, _ := context.WithTimeout(context.Background(), 4*defaultTimeout) payment := sendAndAssertSuccess( - ctxt, t, ctx.alice, - &routerrpc.SendPaymentRequest{ + t, ctx.alice, &routerrpc.SendPaymentRequest{ PaymentRequest: addInvoiceResp.PaymentRequest, PaymentAddr: externalPayAddr, TimeoutSeconds: 60, @@ -249,10 +247,8 @@ func testSendPaymentAMP(net *lntest.NetworkHarness, t *harnessTest) { t.Fatalf("dave policy update: %v", err) } - ctxt, _ := context.WithTimeout(context.Background(), 4*defaultTimeout) payment := sendAndAssertSuccess( - ctxt, t, ctx.alice, - &routerrpc.SendPaymentRequest{ + t, ctx.alice, &routerrpc.SendPaymentRequest{ Dest: ctx.bob.PubKey[:], Amt: int64(paymentAmt), FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta, diff --git a/lntest/itest/lnd_channel_force_close.go b/lntest/itest/lnd_channel_force_close.go index 65fe6b3e0..eeb52e906 100644 --- a/lntest/itest/lnd_channel_force_close.go +++ b/lntest/itest/lnd_channel_force_close.go @@ -785,7 +785,7 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest, // Check that we can find the commitment sweep in our set of known // sweeps, using the simple transaction id ListSweeps output. - assertSweepFound(ctxb, t.t, alice, sweepingTXID.String(), false) + assertSweepFound(t.t, alice, sweepingTXID.String(), false) // Restart Alice to ensure that she resumes watching the finalized // commitment sweep txid. @@ -1218,7 +1218,7 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest, // Check that we can find the htlc sweep in our set of sweeps using // the verbose output of the listsweeps output. - assertSweepFound(ctxb, t.t, alice, htlcSweepTx.Hash().String(), true) + assertSweepFound(t.t, alice, htlcSweepTx.Hash().String(), true) // The following restart checks to ensure that the nursery store is // storing the txid of the previously broadcast htlc sweep txn, and that @@ -1336,8 +1336,8 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest, // Finally, we check that alice and carol have the set of resolutions // we expect. - assertReports(ctxb, t, alice, op, aliceReports) - assertReports(ctxb, t, carol, op, carolReports) + assertReports(t, alice, op, aliceReports) + assertReports(t, carol, op, carolReports) } // padCLTV is a small helper function that pads a cltv value with a block diff --git a/lntest/itest/lnd_etcd_failover_test.go b/lntest/itest/lnd_etcd_failover_test.go index 7dac42f2c..bf3ca7ec3 100644 --- a/lntest/itest/lnd_etcd_failover_test.go +++ b/lntest/itest/lnd_etcd_failover_test.go @@ -129,11 +129,13 @@ func testEtcdFailoverCase(net *lntest.NetworkHarness, ht *harnessTest, ht.Fatalf("Carol-2 is unable to create payment requests: %v", err) } - sendAndAssertSuccess(ctxb, ht, net.Alice, &routerrpc.SendPaymentRequest{ - PaymentRequest: payReqs[0], - TimeoutSeconds: 60, - FeeLimitSat: noFeeLimitMsat, - }) + sendAndAssertSuccess( + ht, net.Alice, &routerrpc.SendPaymentRequest{ + PaymentRequest: payReqs[0], + TimeoutSeconds: 60, + FeeLimitSat: noFeeLimitMsat, + }, + ) // Shut down or kill Carol-1 and wait for Carol-2 to become the leader. var failoverTimeout time.Duration @@ -177,11 +179,13 @@ func testEtcdFailoverCase(net *lntest.NetworkHarness, ht *harnessTest, // Now let Alice pay the second invoice but this time we expect Carol-2 // to receive the payment. - sendAndAssertSuccess(ctxb, ht, net.Alice, &routerrpc.SendPaymentRequest{ - PaymentRequest: payReqs[1], - TimeoutSeconds: 60, - FeeLimitSat: noFeeLimitMsat, - }) + sendAndAssertSuccess( + ht, net.Alice, &routerrpc.SendPaymentRequest{ + PaymentRequest: payReqs[1], + TimeoutSeconds: 60, + FeeLimitSat: noFeeLimitMsat, + }, + ) shutdownAndAssert(net, ht, carol2) } diff --git a/lntest/itest/lnd_misc_test.go b/lntest/itest/lnd_misc_test.go index e93e57063..4c6ab9541 100644 --- a/lntest/itest/lnd_misc_test.go +++ b/lntest/itest/lnd_misc_test.go @@ -1806,7 +1806,7 @@ func testSweepAllCoins(net *lntest.NetworkHarness, t *harnessTest) { } sweepTxStr := sweepTx.TxHash().String() - assertTxLabel(ctxb, t, ainz, sweepTxStr, sendCoinsLabel) + assertTxLabel(t, ainz, sweepTxStr, sendCoinsLabel) // While we are looking at labels, we test our label transaction command // to make sure it is behaving as expected. First, we try to label our @@ -1865,7 +1865,7 @@ func testSweepAllCoins(net *lntest.NetworkHarness, t *harnessTest) { t.Fatalf("could not label tx: %v", err) } - assertTxLabel(ctxb, t, ainz, sweepTxStr, newLabel) + assertTxLabel(t, ainz, sweepTxStr, newLabel) // Finally, Ainz should now have no coins at all within his wallet. balReq := &lnrpc.WalletBalanceRequest{} diff --git a/lntest/itest/lnd_multi-hop-error-propagation_test.go b/lntest/itest/lnd_multi-hop-error-propagation_test.go index 1f1168826..199dde38b 100644 --- a/lntest/itest/lnd_multi-hop-error-propagation_test.go +++ b/lntest/itest/lnd_multi-hop-error-propagation_test.go @@ -289,10 +289,8 @@ out: t.Fatalf("unable to generate carol invoice: %v", err) } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) sendAndAssertSuccess( - ctxt, t, net.Bob, - &routerrpc.SendPaymentRequest{ + t, net.Bob, &routerrpc.SendPaymentRequest{ PaymentRequest: carolInvoice2.PaymentRequest, TimeoutSeconds: 60, FeeLimitMsat: noFeeLimitMsat, diff --git a/lntest/itest/lnd_payment_test.go b/lntest/itest/lnd_payment_test.go index 98c254392..bf1f02c09 100644 --- a/lntest/itest/lnd_payment_test.go +++ b/lntest/itest/lnd_payment_test.go @@ -81,10 +81,8 @@ func testListPayments(net *lntest.NetworkHarness, t *harnessTest) { // With the invoice for Bob added, send a payment towards Alice paying // to the above generated invoice. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) sendAndAssertSuccess( - ctxt, t, net.Alice, - &routerrpc.SendPaymentRequest{ + t, net.Alice, &routerrpc.SendPaymentRequest{ PaymentRequest: invoiceResp.PaymentRequest, TimeoutSeconds: 60, FeeLimitSat: 1000000, @@ -217,9 +215,8 @@ func testPaymentFollowingChannelOpen(net *lntest.NetworkHarness, t *harnessTest) // Send payment to Bob so that a channel update to disk will be // executed. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) sendAndAssertSuccess( - ctxt, t, net.Alice, &routerrpc.SendPaymentRequest{ + t, net.Alice, &routerrpc.SendPaymentRequest{ PaymentRequest: bobPayReqs[0], TimeoutSeconds: 60, FeeLimitSat: 1000000, diff --git a/lntest/itest/lnd_routing_test.go b/lntest/itest/lnd_routing_test.go index d307040ea..7406f1343 100644 --- a/lntest/itest/lnd_routing_test.go +++ b/lntest/itest/lnd_routing_test.go @@ -2060,8 +2060,7 @@ func testRouteFeeCutoff(net *lntest.NetworkHarness, t *harnessTest) { sendReq.FeeLimitMsat = 1000 * paymentAmt * limit.Percent / 100 } - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) - result := sendAndAssertSuccess(ctxt, t, net.Alice, sendReq) + result := sendAndAssertSuccess(t, net.Alice, sendReq) checkRoute(result.Htlcs[0].Route) } diff --git a/lntest/itest/lnd_send_multi_path_payment_test.go b/lntest/itest/lnd_send_multi_path_payment_test.go index e4300fd20..45b25b446 100644 --- a/lntest/itest/lnd_send_multi_path_payment_test.go +++ b/lntest/itest/lnd_send_multi_path_payment_test.go @@ -68,10 +68,8 @@ func testSendMultiPathPayment(net *lntest.NetworkHarness, t *harnessTest) { rHash := rHashes[0] payReq := payReqs[0] - ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) payment := sendAndAssertSuccess( - ctxt, t, ctx.alice, - &routerrpc.SendPaymentRequest{ + t, ctx.alice, &routerrpc.SendPaymentRequest{ PaymentRequest: payReq, MaxParts: 10, TimeoutSeconds: 60, @@ -106,7 +104,7 @@ func testSendMultiPathPayment(net *lntest.NetworkHarness, t *harnessTest) { // Make sure Bob show the invoice as settled for the full // amount. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) + ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) inv, err := ctx.bob.LookupInvoice( ctxt, &lnrpc.PaymentHash{ RHash: rHash, diff --git a/lntest/itest/lnd_single_hop_invoice_test.go b/lntest/itest/lnd_single_hop_invoice_test.go index 8e8c79d01..6bc2780b8 100644 --- a/lntest/itest/lnd_single_hop_invoice_test.go +++ b/lntest/itest/lnd_single_hop_invoice_test.go @@ -62,10 +62,8 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) { // With the invoice for Bob added, send a payment towards Alice paying // to the above generated invoice. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) resp := sendAndAssertSuccess( - ctxt, t, net.Alice, - &routerrpc.SendPaymentRequest{ + t, net.Alice, &routerrpc.SendPaymentRequest{ PaymentRequest: invoiceResp.PaymentRequest, TimeoutSeconds: 60, FeeLimitMsat: noFeeLimitMsat, @@ -115,10 +113,8 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) { // Next send another payment, but this time using a zpay32 encoded // invoice rather than manually specifying the payment details. - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) sendAndAssertSuccess( - ctxt, t, net.Alice, - &routerrpc.SendPaymentRequest{ + t, net.Alice, &routerrpc.SendPaymentRequest{ PaymentRequest: invoiceResp.PaymentRequest, TimeoutSeconds: 60, FeeLimitMsat: noFeeLimitMsat, @@ -139,10 +135,8 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) { keySendPreimage := lntypes.Preimage{3, 4, 5, 11} keySendHash := keySendPreimage.Hash() - ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) sendAndAssertSuccess( - ctxt, t, net.Alice, - &routerrpc.SendPaymentRequest{ + t, net.Alice, &routerrpc.SendPaymentRequest{ Dest: net.Bob.PubKey[:], Amt: paymentAmt, FinalCltvDelta: 40,