itest: manage context timeout in utils.go

This commit finishes moving the context management into utils.go.
This commit is contained in:
yyforyongyu 2021-08-20 02:14:19 +08:00
parent a6c5255e77
commit edffd65e92
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
11 changed files with 51 additions and 72 deletions

View File

@ -227,8 +227,7 @@ func testChannelUnsettledBalance(net *lntest.NetworkHarness, t *harnessTest) {
// Check each nodes UnsettledBalance field.
for _, node := range nodes {
// Get channel info for the node.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
chanInfo, err := getChanInfo(ctxt, node)
chanInfo, err := getChanInfo(node)
if err != nil {
unsettledErr = err
return false

View File

@ -413,8 +413,7 @@ func channelForceClosureTest(net *lntest.NetworkHarness, t *harnessTest,
)
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
aliceChan, err := getChanInfo(ctxt, alice)
aliceChan, err := getChanInfo(alice)
if err != nil {
t.Fatalf("unable to get alice's channel info: %v", err)
}

View File

@ -491,8 +491,6 @@ func testExternalFundingChanPoint(net *lntest.NetworkHarness, t *harnessTest) {
// testFundingPersistence mirrors testBasicChannelFunding, but adds restarts
// and checks for the state of channels with unconfirmed funding transactions.
func testChannelFundingPersistence(net *lntest.NetworkHarness, t *harnessTest) {
ctxb := context.Background()
chanAmt := funding.MaxBtcFundingAmount
pushAmt := btcutil.Amount(0)
@ -570,8 +568,7 @@ func testChannelFundingPersistence(net *lntest.NetworkHarness, t *harnessTest) {
// Assert that our wallet has our opening transaction with a label
// that does not have a channel ID set yet, because we have not
// reached our required confirmations.
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
tx := findTxAtHeight(ctxt, t, height, fundingTxStr, net.Alice)
tx := findTxAtHeight(t, height, fundingTxStr, net.Alice)
// At this stage, we expect the transaction to be labelled, but not with
// our channel ID because our transaction has not yet confirmed.
@ -600,7 +597,7 @@ func testChannelFundingPersistence(net *lntest.NetworkHarness, t *harnessTest) {
}
// Re-lookup our transaction in the block that it confirmed in.
tx = findTxAtHeight(ctxt, t, height, fundingTxStr, net.Alice)
tx = findTxAtHeight(t, height, fundingTxStr, net.Alice)
// Create an additional check for our channel assertion that will
// check that our label is as expected.

View File

@ -105,8 +105,7 @@ func testHoldInvoiceForceClose(net *lntest.NetworkHarness, t *harnessTest) {
// Our channel should not have been force closed, instead we expect our
// channel to still be open and our invoice to have been canceled before
// expiry.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
chanInfo, err := getChanInfo(ctxt, net.Alice)
chanInfo, err := getChanInfo(net.Alice)
require.NoError(t.t, err)
fundingTxID, err := lnrpc.GetChanPointFundingTxid(chanPoint)

View File

@ -897,8 +897,7 @@ func testDataLossProtection(net *lntest.NetworkHarness, t *harnessTest) {
var nodeChan *lnrpc.Channel
var predErr error
err = wait.Predicate(func() bool {
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
bChan, err := getChanInfo(ctxt, node)
bChan, err := getChanInfo(node)
if err != nil {
t.Fatalf("unable to get channel info: %v", err)
}
@ -937,8 +936,7 @@ func testDataLossProtection(net *lntest.NetworkHarness, t *harnessTest) {
t.Fatalf("unable to send payments: %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
nodeChan, err = getChanInfo(ctxt, node)
nodeChan, err = getChanInfo(node)
if err != nil {
t.Fatalf("unable to get dave chan info: %v", err)
}
@ -960,8 +958,7 @@ func testDataLossProtection(net *lntest.NetworkHarness, t *harnessTest) {
// Now query for the channel state, it should show that it's at
// a state number in the past, not the *latest* state.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
nodeChan, err = getChanInfo(ctxt, node)
nodeChan, err = getChanInfo(node)
if err != nil {
t.Fatalf("unable to get dave chan info: %v", err)
}

View File

@ -125,8 +125,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {
if err != nil {
t.Fatalf("unable to get current blockheight %v", err)
}
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
err = waitForNodeBlockHeight(ctxt, net.Alice, minerHeight)
err = waitForNodeBlockHeight(net.Alice, minerHeight)
if err != nil {
t.Fatalf("unable to sync to chain: %v", err)
}
@ -143,7 +142,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {
// Wait for Alice and Bob to recognize and advertise the new channel
// generated above.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
if err != nil {
t.Fatalf("alice didn't advertise channel before "+
@ -217,8 +216,7 @@ func testOpenChannelAfterReorg(net *lntest.NetworkHarness, t *harnessTest) {
if err != nil {
t.Fatalf("unable to get current blockheight %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
err = waitForNodeBlockHeight(ctxt, net.Alice, tempMinerHeight)
err = waitForNodeBlockHeight(net.Alice, tempMinerHeight)
if err != nil {
t.Fatalf("unable to sync to chain: %v", err)
}

View File

@ -260,8 +260,7 @@ func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
},
)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
info, err := getChanInfo(ctxt, net.Alice)
info, err := getChanInfo(net.Alice)
if err != nil {
t.Fatalf("unable to get alice channel info: %v", err)
}
@ -286,7 +285,7 @@ func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
}
// Wait for Alice to receive the channel edge from the funding manager.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
if err != nil {
t.Fatalf("alice didn't see the alice->bob channel before "+
@ -345,8 +344,7 @@ func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
// htlcs listed and has correct balances. This is needed due to the fact
// that we now pipeline the settles.
err = wait.Predicate(func() bool {
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
aliceChan, err := getChanInfo(ctxt, net.Alice)
aliceChan, err := getChanInfo(net.Alice)
if err != nil {
return false
}
@ -368,8 +366,7 @@ func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
// Wait for Bob to receive revocation from Alice.
err = wait.NoError(func() error {
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
bobChan, err := getChanInfo(ctxt, net.Bob)
bobChan, err := getChanInfo(net.Bob)
if err != nil {
t.Fatalf("unable to get bob's channel info: %v", err)
}
@ -425,8 +422,7 @@ func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest)
},
)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
info, err := getChanInfo(ctxt, net.Alice)
info, err := getChanInfo(net.Alice)
if err != nil {
t.Fatalf("unable to get alice channel info: %v", err)
}
@ -462,7 +458,7 @@ func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest)
}
// Wait for Alice to receive the channel edge from the funding manager.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
if err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint); err != nil {
t.Fatalf("alice didn't see the alice->bob channel before "+
"timeout: %v", err)
@ -534,8 +530,7 @@ func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest)
// Wait for Alice and Bob to receive revocations messages, and update
// states, i.e. balance info.
err = wait.NoError(func() error {
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
aliceInfo, err := getChanInfo(ctxt, net.Alice)
aliceInfo, err := getChanInfo(net.Alice)
if err != nil {
t.Fatalf("unable to get alice's channel info: %v", err)
}
@ -565,8 +560,7 @@ func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest)
// Next query for Bob's and Alice's channel states, in order to confirm
// that all payment have been successful transmitted.
err = wait.NoError(func() error {
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
bobInfo, err := getChanInfo(ctxt, net.Bob)
bobInfo, err := getChanInfo(net.Bob)
if err != nil {
t.Fatalf("unable to get bob's channel info: %v", err)
}

View File

@ -45,8 +45,7 @@ func testGetRecoveryInfo(net *lntest.NetworkHarness, t *harnessTest) {
if err != nil {
t.Fatalf("unable to get current blockheight %v", err)
}
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
err = waitForNodeBlockHeight(ctxt, node, minerHeight)
err = waitForNodeBlockHeight(node, minerHeight)
if err != nil {
t.Fatalf("unable to sync to chain: %v", err)
}

View File

@ -91,8 +91,7 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
var bobChan *lnrpc.Channel
var predErr error
err = wait.Predicate(func() bool {
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
bChan, err := getChanInfo(ctxt, net.Bob)
bChan, err := getChanInfo(net.Bob)
if err != nil {
t.Fatalf("unable to get bob's channel info: %v", err)
}
@ -131,8 +130,7 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
t.Fatalf("unable to send payments: %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
bobChan, err = getChanInfo(ctxt, net.Bob)
bobChan, err = getChanInfo(net.Bob)
if err != nil {
t.Fatalf("unable to get bob chan info: %v", err)
}
@ -149,8 +147,7 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
// Now query for Bob's channel state, it should show that he's at a
// state number in the past, not the *latest* state.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
bobChan, err = getChanInfo(ctxt, net.Bob)
bobChan, err = getChanInfo(net.Bob)
if err != nil {
t.Fatalf("unable to get bob chan info: %v", err)
}
@ -330,8 +327,7 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness
// Next query for Carol's channel state, as we sent 0 payments, Carol
// should now see her balance as being 0 satoshis.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
carolChan, err := getChanInfo(ctxt, carol)
carolChan, err := getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol's channel info: %v", err)
}
@ -361,8 +357,7 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness
t.Fatalf("unable to send payments: %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
_, err = getChanInfo(ctxt, carol)
_, err = getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol chan info: %v", err)
}
@ -379,8 +374,7 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness
// Now query for Carol's channel state, it should show that he's at a
// state number in the past, not the *latest* state.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
carolChan, err = getChanInfo(ctxt, carol)
carolChan, err = getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol chan info: %v", err)
}
@ -551,8 +545,7 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// We'll introduce a closure to validate that Carol's current balance
// matches the given expected amount.
checkCarolBalance := func(expectedAmt int64) {
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
carolChan, err := getChanInfo(ctxt, carol)
carolChan, err := getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol's channel info: %v", err)
}
@ -567,8 +560,7 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// number of updates is at least as large as the provided minimum
// number.
checkCarolNumUpdatesAtLeast := func(minimum uint64) {
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
carolChan, err := getChanInfo(ctxt, carol)
carolChan, err := getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol's channel info: %v", err)
}
@ -621,8 +613,7 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// Next query for Carol's channel state, as we sent 3 payments of 10k
// satoshis each, however Carol should now see her balance as being
// equal to the push amount in satoshis since she has not settled.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
carolChan, err := getChanInfo(ctxt, carol)
carolChan, err := getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol's channel info: %v", err)
}
@ -689,8 +680,7 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// Now query for Carol's channel state, it should show that she's at a
// state number in the past, *not* the latest state.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
carolChan, err = getChanInfo(ctxt, carol)
carolChan, err = getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol chan info: %v", err)
}
@ -1046,8 +1036,7 @@ func testRevokedCloseRetributionAltruistWatchtowerCase(
// Next query for Carol's channel state, as we sent 0 payments, Carol
// should still see her balance as the push amount, which is 1/4 of the
// capacity.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
carolChan, err := getChanInfo(ctxt, carol)
carolChan, err := getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol's channel info: %v", err)
}
@ -1130,8 +1119,7 @@ func testRevokedCloseRetributionAltruistWatchtowerCase(
// Now query for Carol's channel state, it should show that he's at a
// state number in the past, not the *latest* state.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
carolChan, err = getChanInfo(ctxt, carol)
carolChan, err = getChanInfo(carol)
if err != nil {
t.Fatalf("unable to get carol chan info: %v", err)
}

View File

@ -179,10 +179,8 @@ func testSingleHopSendToRouteCase(net *lntest.NetworkHarness, t *harnessTest,
if err != nil {
t.Fatalf("unable to get best height: %v", err)
}
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
require.NoError(t.t, waitForNodeBlockHeight(ctxt, carol, minerHeight))
require.NoError(t.t, waitForNodeBlockHeight(ctxt, dave, minerHeight))
require.NoError(t.t, waitForNodeBlockHeight(carol, minerHeight))
require.NoError(t.t, waitForNodeBlockHeight(dave, minerHeight))
// Query for routes to pay from Carol to Dave using the default CLTV
// config.
@ -190,7 +188,7 @@ func testSingleHopSendToRouteCase(net *lntest.NetworkHarness, t *harnessTest,
PubKey: dave.PubKeyStr,
Amt: paymentAmtSat,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
routes, err := carol.QueryRoutes(ctxt, routesReq)
if err != nil {
t.Fatalf("unable to get route from %s: %v",

View File

@ -190,8 +190,11 @@ func createPayReqs(node *lntest.HarnessNode, paymentAmt btcutil.Amount,
// getChanInfo is a helper method for getting channel info for a node's sole
// channel.
func getChanInfo(ctx context.Context, node *lntest.HarnessNode) (
*lnrpc.Channel, error) {
func getChanInfo(node *lntest.HarnessNode) (*lnrpc.Channel, error) {
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
req := &lnrpc.ListChannelsRequest{}
channelInfo, err := node.ListChannels(ctx, req)
@ -326,8 +329,12 @@ func calculateMaxHtlc(chanCap btcutil.Amount) uint64 {
// waitForNodeBlockHeight queries the node for its current block height until
// it reaches the passed height.
func waitForNodeBlockHeight(ctx context.Context, node *lntest.HarnessNode,
height int32) error {
func waitForNodeBlockHeight(node *lntest.HarnessNode, height int32) error {
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
var predErr error
err := wait.Predicate(func() bool {
ctxt, _ := context.WithTimeout(ctx, defaultTimeout)
@ -465,9 +472,13 @@ func subscribeChannelNotifications(ctxb context.Context, t *harnessTest,
// findTxAtHeight gets all of the transactions that a node's wallet has a record
// of at the target height, and finds and returns the tx with the target txid,
// failing if it is not found.
func findTxAtHeight(ctx context.Context, t *harnessTest, height int32,
func findTxAtHeight(t *harnessTest, height int32,
target string, node *lntest.HarnessNode) *lnrpc.Transaction {
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
txns, err := node.LightningClient.GetTransactions(
ctx, &lnrpc.GetTransactionsRequest{
StartHeight: height,