itest: manage context inside assertions - I

This commit changes the methods assertTxLabel, assertReports,
assertSweepFound, and sendAndAssertSuccess to manage their own context
with deadline.
This commit is contained in:
yyforyongyu 2021-08-19 21:16:41 +08:00
parent 74f8fe482d
commit 02e4c3ad4c
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
10 changed files with 48 additions and 54 deletions

View File

@ -976,11 +976,11 @@ func checkPendingHtlcStageAndMaturity(
// assertReports checks that the count of resolutions we have present per // assertReports checks that the count of resolutions we have present per
// type matches a set of expected resolutions. // type matches a set of expected resolutions.
func assertReports(ctxb context.Context, t *harnessTest, func assertReports(t *harnessTest, node *lntest.HarnessNode,
node *lntest.HarnessNode, channelPoint wire.OutPoint, channelPoint wire.OutPoint, expected map[string]*lnrpc.Resolution) {
expected map[string]*lnrpc.Resolution) {
// Get our node's closed channels. // Get our node's closed channels.
ctxb := context.Background()
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout) ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel() 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. // 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) { sweep string, verbose bool) {
// List all sweeps that alice's node had broadcast. // 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( sweepResp, err := node.WalletKitClient.ListSweeps(
ctx, &walletrpc.ListSweepsRequest{ ctx, &walletrpc.ListSweepsRequest{
Verbose: verbose, Verbose: verbose,
@ -1641,6 +1643,7 @@ func assertActiveHtlcs(nodes []*lntest.HarnessNode, payHashes ...[]byte) error {
func assertNumActiveHtlcsChanPoint(node *lntest.HarnessNode, func assertNumActiveHtlcsChanPoint(node *lntest.HarnessNode,
chanPoint wire.OutPoint, numHtlcs int) error { chanPoint wire.OutPoint, numHtlcs int) error {
ctxb := context.Background() ctxb := context.Background()
req := &lnrpc.ListChannelsRequest{} 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 // assertTxLabel is a helper function which finds a target tx in our set
// of transactions and checks that it has the desired label. // of transactions and checks that it has the desired label.
func assertTxLabel(ctx context.Context, t *harnessTest, func assertTxLabel(t *harnessTest, node *lntest.HarnessNode,
node *lntest.HarnessNode, targetTx, label string) { targetTx, label string) {
// List all transactions relevant to our wallet, and find the tx so that // List all transactions relevant to our wallet, and find the tx so that
// we can check the correct label has been set. // 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() defer cancel()
txResp, err := node.GetTransactions( 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 // sendAndAssertSuccess sends the given payment requests and asserts that the
// payment completes successfully. // 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 { req *routerrpc.SendPaymentRequest) *lnrpc.Payment {
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
var result *lnrpc.Payment var result *lnrpc.Payment
err := wait.NoError(func() error { err := wait.NoError(func() error {
stream, err := node.RouterClient.SendPaymentV2(ctx, req) stream, err := node.RouterClient.SendPaymentV2(ctx, req)

View File

@ -117,10 +117,8 @@ func testSendPaymentAMPInvoiceCase(net *lntest.NetworkHarness, t *harnessTest,
require.NoError(t.t, err) require.NoError(t.t, err)
} }
ctxt, _ := context.WithTimeout(context.Background(), 4*defaultTimeout)
payment := sendAndAssertSuccess( payment := sendAndAssertSuccess(
ctxt, t, ctx.alice, t, ctx.alice, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
PaymentRequest: addInvoiceResp.PaymentRequest, PaymentRequest: addInvoiceResp.PaymentRequest,
PaymentAddr: externalPayAddr, PaymentAddr: externalPayAddr,
TimeoutSeconds: 60, TimeoutSeconds: 60,
@ -249,10 +247,8 @@ func testSendPaymentAMP(net *lntest.NetworkHarness, t *harnessTest) {
t.Fatalf("dave policy update: %v", err) t.Fatalf("dave policy update: %v", err)
} }
ctxt, _ := context.WithTimeout(context.Background(), 4*defaultTimeout)
payment := sendAndAssertSuccess( payment := sendAndAssertSuccess(
ctxt, t, ctx.alice, t, ctx.alice, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
Dest: ctx.bob.PubKey[:], Dest: ctx.bob.PubKey[:],
Amt: int64(paymentAmt), Amt: int64(paymentAmt),
FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta, FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta,

View File

@ -785,7 +785,7 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
// Check that we can find the commitment sweep in our set of known // Check that we can find the commitment sweep in our set of known
// sweeps, using the simple transaction id ListSweeps output. // 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 // Restart Alice to ensure that she resumes watching the finalized
// commitment sweep txid. // 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 // Check that we can find the htlc sweep in our set of sweeps using
// the verbose output of the listsweeps output. // 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 // The following restart checks to ensure that the nursery store is
// storing the txid of the previously broadcast htlc sweep txn, and that // 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 // Finally, we check that alice and carol have the set of resolutions
// we expect. // we expect.
assertReports(ctxb, t, alice, op, aliceReports) assertReports(t, alice, op, aliceReports)
assertReports(ctxb, t, carol, op, carolReports) assertReports(t, carol, op, carolReports)
} }
// padCLTV is a small helper function that pads a cltv value with a block // padCLTV is a small helper function that pads a cltv value with a block

View File

@ -129,11 +129,13 @@ func testEtcdFailoverCase(net *lntest.NetworkHarness, ht *harnessTest,
ht.Fatalf("Carol-2 is unable to create payment requests: %v", ht.Fatalf("Carol-2 is unable to create payment requests: %v",
err) err)
} }
sendAndAssertSuccess(ctxb, ht, net.Alice, &routerrpc.SendPaymentRequest{ sendAndAssertSuccess(
PaymentRequest: payReqs[0], ht, net.Alice, &routerrpc.SendPaymentRequest{
TimeoutSeconds: 60, PaymentRequest: payReqs[0],
FeeLimitSat: noFeeLimitMsat, TimeoutSeconds: 60,
}) FeeLimitSat: noFeeLimitMsat,
},
)
// Shut down or kill Carol-1 and wait for Carol-2 to become the leader. // Shut down or kill Carol-1 and wait for Carol-2 to become the leader.
var failoverTimeout time.Duration 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 // Now let Alice pay the second invoice but this time we expect Carol-2
// to receive the payment. // to receive the payment.
sendAndAssertSuccess(ctxb, ht, net.Alice, &routerrpc.SendPaymentRequest{ sendAndAssertSuccess(
PaymentRequest: payReqs[1], ht, net.Alice, &routerrpc.SendPaymentRequest{
TimeoutSeconds: 60, PaymentRequest: payReqs[1],
FeeLimitSat: noFeeLimitMsat, TimeoutSeconds: 60,
}) FeeLimitSat: noFeeLimitMsat,
},
)
shutdownAndAssert(net, ht, carol2) shutdownAndAssert(net, ht, carol2)
} }

View File

@ -1806,7 +1806,7 @@ func testSweepAllCoins(net *lntest.NetworkHarness, t *harnessTest) {
} }
sweepTxStr := sweepTx.TxHash().String() 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 // 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 // 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) 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. // Finally, Ainz should now have no coins at all within his wallet.
balReq := &lnrpc.WalletBalanceRequest{} balReq := &lnrpc.WalletBalanceRequest{}

View File

@ -289,10 +289,8 @@ out:
t.Fatalf("unable to generate carol invoice: %v", err) t.Fatalf("unable to generate carol invoice: %v", err)
} }
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
sendAndAssertSuccess( sendAndAssertSuccess(
ctxt, t, net.Bob, t, net.Bob, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
PaymentRequest: carolInvoice2.PaymentRequest, PaymentRequest: carolInvoice2.PaymentRequest,
TimeoutSeconds: 60, TimeoutSeconds: 60,
FeeLimitMsat: noFeeLimitMsat, FeeLimitMsat: noFeeLimitMsat,

View File

@ -81,10 +81,8 @@ func testListPayments(net *lntest.NetworkHarness, t *harnessTest) {
// With the invoice for Bob added, send a payment towards Alice paying // With the invoice for Bob added, send a payment towards Alice paying
// to the above generated invoice. // to the above generated invoice.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
sendAndAssertSuccess( sendAndAssertSuccess(
ctxt, t, net.Alice, t, net.Alice, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
PaymentRequest: invoiceResp.PaymentRequest, PaymentRequest: invoiceResp.PaymentRequest,
TimeoutSeconds: 60, TimeoutSeconds: 60,
FeeLimitSat: 1000000, 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 // Send payment to Bob so that a channel update to disk will be
// executed. // executed.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
sendAndAssertSuccess( sendAndAssertSuccess(
ctxt, t, net.Alice, &routerrpc.SendPaymentRequest{ t, net.Alice, &routerrpc.SendPaymentRequest{
PaymentRequest: bobPayReqs[0], PaymentRequest: bobPayReqs[0],
TimeoutSeconds: 60, TimeoutSeconds: 60,
FeeLimitSat: 1000000, FeeLimitSat: 1000000,

View File

@ -2060,8 +2060,7 @@ func testRouteFeeCutoff(net *lntest.NetworkHarness, t *harnessTest) {
sendReq.FeeLimitMsat = 1000 * paymentAmt * limit.Percent / 100 sendReq.FeeLimitMsat = 1000 * paymentAmt * limit.Percent / 100
} }
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) result := sendAndAssertSuccess(t, net.Alice, sendReq)
result := sendAndAssertSuccess(ctxt, t, net.Alice, sendReq)
checkRoute(result.Htlcs[0].Route) checkRoute(result.Htlcs[0].Route)
} }

View File

@ -68,10 +68,8 @@ func testSendMultiPathPayment(net *lntest.NetworkHarness, t *harnessTest) {
rHash := rHashes[0] rHash := rHashes[0]
payReq := payReqs[0] payReq := payReqs[0]
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
payment := sendAndAssertSuccess( payment := sendAndAssertSuccess(
ctxt, t, ctx.alice, t, ctx.alice, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
PaymentRequest: payReq, PaymentRequest: payReq,
MaxParts: 10, MaxParts: 10,
TimeoutSeconds: 60, TimeoutSeconds: 60,
@ -106,7 +104,7 @@ func testSendMultiPathPayment(net *lntest.NetworkHarness, t *harnessTest) {
// Make sure Bob show the invoice as settled for the full // Make sure Bob show the invoice as settled for the full
// amount. // amount.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout) ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
inv, err := ctx.bob.LookupInvoice( inv, err := ctx.bob.LookupInvoice(
ctxt, &lnrpc.PaymentHash{ ctxt, &lnrpc.PaymentHash{
RHash: rHash, RHash: rHash,

View File

@ -62,10 +62,8 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
// With the invoice for Bob added, send a payment towards Alice paying // With the invoice for Bob added, send a payment towards Alice paying
// to the above generated invoice. // to the above generated invoice.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
resp := sendAndAssertSuccess( resp := sendAndAssertSuccess(
ctxt, t, net.Alice, t, net.Alice, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
PaymentRequest: invoiceResp.PaymentRequest, PaymentRequest: invoiceResp.PaymentRequest,
TimeoutSeconds: 60, TimeoutSeconds: 60,
FeeLimitMsat: noFeeLimitMsat, FeeLimitMsat: noFeeLimitMsat,
@ -115,10 +113,8 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
// Next send another payment, but this time using a zpay32 encoded // Next send another payment, but this time using a zpay32 encoded
// invoice rather than manually specifying the payment details. // invoice rather than manually specifying the payment details.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
sendAndAssertSuccess( sendAndAssertSuccess(
ctxt, t, net.Alice, t, net.Alice, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
PaymentRequest: invoiceResp.PaymentRequest, PaymentRequest: invoiceResp.PaymentRequest,
TimeoutSeconds: 60, TimeoutSeconds: 60,
FeeLimitMsat: noFeeLimitMsat, FeeLimitMsat: noFeeLimitMsat,
@ -139,10 +135,8 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
keySendPreimage := lntypes.Preimage{3, 4, 5, 11} keySendPreimage := lntypes.Preimage{3, 4, 5, 11}
keySendHash := keySendPreimage.Hash() keySendHash := keySendPreimage.Hash()
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
sendAndAssertSuccess( sendAndAssertSuccess(
ctxt, t, net.Alice, t, net.Alice, &routerrpc.SendPaymentRequest{
&routerrpc.SendPaymentRequest{
Dest: net.Bob.PubKey[:], Dest: net.Bob.PubKey[:],
Amt: paymentAmt, Amt: paymentAmt,
FinalCltvDelta: 40, FinalCltvDelta: 40,