itest: fix inheritance when creating timeout ctxt

This commit fixes the issue where a wrong context being inherited to
create a timeout context. When a parent context timed out, all its
children contexts also timed out, even the children contexts had a
larger timeout value. This means it only makes sense to inherite from a
parent when its children have smaller timeout value. Given the setup of
the itest, all the timeout contexts need to be created from a context
background(hence no timeout on the parent) unless there's an explicit
timeout bound we want to set.
This commit is contained in:
yyforyongyu
2021-08-20 17:33:42 +08:00
parent edffd65e92
commit 104b7a09db
13 changed files with 40 additions and 54 deletions

View File

@@ -550,7 +550,7 @@ tryconnect:
// peers list, or until the 15s timeout expires. // peers list, or until the 15s timeout expires.
func (n *NetworkHarness) EnsureConnected(t *testing.T, a, b *HarnessNode) { func (n *NetworkHarness) EnsureConnected(t *testing.T, a, b *HarnessNode) {
ctxb := context.Background() ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, DefaultTimeout) ctx, cancel := context.WithTimeout(ctxb, DefaultTimeout*2)
defer cancel() defer cancel()
// errConnectionRequested is used to signal that a connection was // errConnectionRequested is used to signal that a connection was
@@ -559,9 +559,7 @@ func (n *NetworkHarness) EnsureConnected(t *testing.T, a, b *HarnessNode) {
errConnectionRequested := errors.New("connection request in progress") errConnectionRequested := errors.New("connection request in progress")
tryConnect := func(a, b *HarnessNode) error { tryConnect := func(a, b *HarnessNode) error {
ctxt, cancel := context.WithTimeout(ctx, DefaultTimeout) bInfo, err := b.GetInfo(ctx, &lnrpc.GetInfoRequest{})
defer cancel()
bInfo, err := b.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
if err != nil { if err != nil {
return err return err
} }
@@ -639,9 +637,7 @@ func (n *NetworkHarness) EnsureConnected(t *testing.T, a, b *HarnessNode) {
// If node B is seen in the ListPeers response from node A, // If node B is seen in the ListPeers response from node A,
// then we can exit early as the connection has been fully // then we can exit early as the connection has been fully
// established. // established.
ctxt, cancel := context.WithTimeout(ctx, DefaultTimeout) resp, err := b.ListPeers(ctx, &lnrpc.ListPeersRequest{})
defer cancel()
resp, err := b.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
if err != nil { if err != nil {
return false return false
} }
@@ -996,8 +992,10 @@ func (n *NetworkHarness) OpenChannel(srcNode, destNode *HarnessNode,
p OpenChannelParams) (lnrpc.Lightning_OpenChannelClient, error) { p OpenChannelParams) (lnrpc.Lightning_OpenChannelClient, error) {
ctxb := context.Background() ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, ChannelOpenTimeout) // The cancel is intentionally left out here because the returned
defer cancel() // item(open channel client) relies on the context being active. This
// will be fixed once we finish refactoring the NetworkHarness.
ctx, _ := context.WithTimeout(ctxb, ChannelOpenTimeout) // nolint: govet
// Wait until srcNode and destNode have the latest chain synced. // Wait until srcNode and destNode have the latest chain synced.
// Otherwise, we may run into a check within the funding manager that // Otherwise, we may run into a check within the funding manager that
@@ -1179,8 +1177,10 @@ func (n *NetworkHarness) CloseChannel(lnNode *HarnessNode,
force bool) (lnrpc.Lightning_CloseChannelClient, *chainhash.Hash, error) { force bool) (lnrpc.Lightning_CloseChannelClient, *chainhash.Hash, error) {
ctxb := context.Background() ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, ChannelCloseTimeout) // The cancel is intentionally left out here because the returned
defer cancel() // item(close channel client) relies on the context being active. This
// will be fixed once we finish refactoring the NetworkHarness.
ctx, _ := context.WithTimeout(ctxb, ChannelCloseTimeout) // nolint: govet
// Create a channel outpoint that we can use to compare to channels // Create a channel outpoint that we can use to compare to channels
// from the ListChannelsResponse. // from the ListChannelsResponse.

View File

@@ -288,9 +288,8 @@ func assertChannelClosed(ctx context.Context, t *harnessTest,
// If the channel appears in list channels, ensure that its state // If the channel appears in list channels, ensure that its state
// contains ChanStatusCoopBroadcasted. // contains ChanStatusCoopBroadcasted.
ctxt, _ := context.WithTimeout(ctx, defaultTimeout)
listChansRequest := &lnrpc.ListChannelsRequest{} listChansRequest := &lnrpc.ListChannelsRequest{}
listChansResp, err := node.ListChannels(ctxt, listChansRequest) listChansResp, err := node.ListChannels(ctx, listChansRequest)
require.NoError(t.t, err, "unable to query for list channels") require.NoError(t.t, err, "unable to query for list channels")
for _, channel := range listChansResp.Channels { for _, channel := range listChansResp.Channels {
@@ -309,9 +308,8 @@ func assertChannelClosed(ctx context.Context, t *harnessTest,
// At this point, the channel should now be marked as being in the // At this point, the channel should now be marked as being in the
// state of "waiting close". // state of "waiting close".
ctxt, _ = context.WithTimeout(ctx, defaultTimeout)
pendingChansRequest := &lnrpc.PendingChannelsRequest{} pendingChansRequest := &lnrpc.PendingChannelsRequest{}
pendingChanResp, err := node.PendingChannels(ctxt, pendingChansRequest) pendingChanResp, err := node.PendingChannels(ctx, pendingChansRequest)
require.NoError(t.t, err, "unable to query for pending channels") require.NoError(t.t, err, "unable to query for pending channels")
var found bool var found bool

View File

@@ -67,8 +67,8 @@ func testForwardInterceptor(net *lntest.NetworkHarness, t *harnessTest) {
testContext.waitForChannels() testContext.waitForChannels()
// Connect the interceptor. // Connect the interceptor.
ctx := context.Background() ctxb := context.Background()
ctxt, cancelInterceptor := context.WithTimeout(ctx, defaultTimeout) ctxt, cancelInterceptor := context.WithTimeout(ctxb, defaultTimeout)
interceptor, err := testContext.bob.RouterClient.HtlcInterceptor(ctxt) interceptor, err := testContext.bob.RouterClient.HtlcInterceptor(ctxt)
require.NoError(t.t, err, "failed to create HtlcInterceptor") require.NoError(t.t, err, "failed to create HtlcInterceptor")
@@ -230,8 +230,7 @@ func testForwardInterceptor(net *lntest.NetworkHarness, t *harnessTest) {
restartAlice, err := net.SuspendNode(alice) restartAlice, err := net.SuspendNode(alice)
require.NoError(t.t, err, "failed to suspend alice") require.NoError(t.t, err, "failed to suspend alice")
ctx = context.Background() ctxt, cancelInterceptor = context.WithTimeout(ctxb, defaultTimeout)
ctxt, cancelInterceptor = context.WithTimeout(ctx, defaultTimeout)
defer cancelInterceptor() defer cancelInterceptor()
interceptor, err = testContext.bob.RouterClient.HtlcInterceptor(ctxt) interceptor, err = testContext.bob.RouterClient.HtlcInterceptor(ctxt)
require.NoError(t.t, err, "failed to create HtlcInterceptor") require.NoError(t.t, err, "failed to create HtlcInterceptor")
@@ -259,7 +258,7 @@ func testForwardInterceptor(net *lntest.NetworkHarness, t *harnessTest) {
}() }()
err = wait.Predicate(func() bool { err = wait.Predicate(func() bool {
channels, err := bob.ListChannels(ctx, &lnrpc.ListChannelsRequest{ channels, err := bob.ListChannels(ctxt, &lnrpc.ListChannelsRequest{
ActiveOnly: true, Peer: alice.PubKey[:], ActiveOnly: true, Peer: alice.PubKey[:],
}) })
return err == nil && len(channels.Channels) > 0 return err == nil && len(channels.Channels) > 0

View File

@@ -165,7 +165,7 @@ func testHoldInvoicePersistence(net *lntest.NetworkHarness, t *harnessTest) {
req := &lnrpc.ListPaymentsRequest{ req := &lnrpc.ListPaymentsRequest{
IncludeIncomplete: true, IncludeIncomplete: true,
} }
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
paymentsResp, err := net.Alice.ListPayments(ctxt, req) paymentsResp, err := net.Alice.ListPayments(ctxt, req)
if err != nil { if err != nil {
return fmt.Errorf("error when obtaining payments: %v", return fmt.Errorf("error when obtaining payments: %v",
@@ -368,7 +368,7 @@ func testHoldInvoicePersistence(net *lntest.NetworkHarness, t *harnessTest) {
req := &lnrpc.ListPaymentsRequest{ req := &lnrpc.ListPaymentsRequest{
IncludeIncomplete: true, IncludeIncomplete: true,
} }
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
paymentsResp, err := net.Alice.ListPayments(ctxt, req) paymentsResp, err := net.Alice.ListPayments(ctxt, req)
if err != nil { if err != nil {
t.Fatalf("error when obtaining Alice payments: %v", err) t.Fatalf("error when obtaining Alice payments: %v", err)

View File

@@ -298,9 +298,6 @@ func testMultiHopHtlcLocalChainClaim(net *lntest.NetworkHarness, t *harnessTest,
// Finally, check that the Alice's payment is correctly marked // Finally, check that the Alice's payment is correctly marked
// succeeded. // succeeded.
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) err = checkPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
err = checkPaymentStatus(
ctxt, alice, preimage, lnrpc.Payment_SUCCEEDED,
)
require.NoError(t.t, err) require.NoError(t.t, err)
} }

View File

@@ -224,10 +224,7 @@ func testMultiHopReceiverChainClaim(net *lntest.NetworkHarness, t *harnessTest,
// Finally, check that the Alice's payment is correctly marked // Finally, check that the Alice's payment is correctly marked
// succeeded. // succeeded.
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) err = checkPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
err = checkPaymentStatus(
ctxt, alice, preimage, lnrpc.Payment_SUCCEEDED,
)
require.NoError(t.t, err) require.NoError(t.t, err)
// We'll close out the channel between Alice and Bob, then shutdown // We'll close out the channel between Alice and Bob, then shutdown

View File

@@ -262,9 +262,6 @@ func testMultiHopHtlcRemoteChainClaim(net *lntest.NetworkHarness, t *harnessTest
// Finally, check that the Alice's payment is correctly marked // Finally, check that the Alice's payment is correctly marked
// succeeded. // succeeded.
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) err = checkPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
err = checkPaymentStatus(
ctxt, alice, preimage, lnrpc.Payment_SUCCEEDED,
)
require.NoError(t.t, err) require.NoError(t.t, err)
} }

View File

@@ -148,8 +148,12 @@ func waitForInvoiceAccepted(t *harnessTest, node *lntest.HarnessNode,
// checkPaymentStatus asserts that the given node list a payment with the given // checkPaymentStatus asserts that the given node list a payment with the given
// preimage has the expected status. // preimage has the expected status.
func checkPaymentStatus(ctxt context.Context, node *lntest.HarnessNode, func checkPaymentStatus(node *lntest.HarnessNode, preimage lntypes.Preimage,
preimage lntypes.Preimage, status lnrpc.Payment_PaymentStatus) error { status lnrpc.Payment_PaymentStatus) error {
ctxb := context.Background()
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
req := &lnrpc.ListPaymentsRequest{ req := &lnrpc.ListPaymentsRequest{
IncludeIncomplete: true, IncludeIncomplete: true,

View File

@@ -69,10 +69,6 @@ func assertTimeoutError(ctxt context.Context, t *harnessTest,
t.t.Helper() t.t.Helper()
// Create a context with a timeout value.
ctxt, cancel := context.WithTimeout(ctxt, defaultTimeout)
defer cancel()
err := connect(ctxt, node, req) err := connect(ctxt, node, req)
// a DeadlineExceeded error will appear in the context if the above // a DeadlineExceeded error will appear in the context if the above

View File

@@ -31,7 +31,7 @@ func testListPayments(net *lntest.NetworkHarness, t *harnessTest) {
// Check that there are no payments before test. // Check that there are no payments before test.
reqInit := &lnrpc.ListPaymentsRequest{} reqInit := &lnrpc.ListPaymentsRequest{}
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
paymentsRespInit, err := net.Alice.ListPayments(ctxt, reqInit) paymentsRespInit, err := net.Alice.ListPayments(ctxt, reqInit)
if err != nil { if err != nil {
t.Fatalf("error when obtaining Alice payments: %v", err) t.Fatalf("error when obtaining Alice payments: %v", err)
@@ -92,7 +92,7 @@ func testListPayments(net *lntest.NetworkHarness, t *harnessTest) {
// Grab Alice's list of payments, she should show the existence of // Grab Alice's list of payments, she should show the existence of
// exactly one payment. // exactly one payment.
req := &lnrpc.ListPaymentsRequest{} req := &lnrpc.ListPaymentsRequest{}
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
paymentsResp, err := net.Alice.ListPayments(ctxt, req) paymentsResp, err := net.Alice.ListPayments(ctxt, req)
if err != nil { if err != nil {
t.Fatalf("error when obtaining Alice payments: %v", err) t.Fatalf("error when obtaining Alice payments: %v", err)
@@ -138,7 +138,7 @@ func testListPayments(net *lntest.NetworkHarness, t *harnessTest) {
// Delete all payments from Alice. DB should have no payments. // Delete all payments from Alice. DB should have no payments.
delReq := &lnrpc.DeleteAllPaymentsRequest{} delReq := &lnrpc.DeleteAllPaymentsRequest{}
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
_, err = net.Alice.DeleteAllPayments(ctxt, delReq) _, err = net.Alice.DeleteAllPayments(ctxt, delReq)
if err != nil { if err != nil {
t.Fatalf("Can't delete payments at the end: %v", err) t.Fatalf("Can't delete payments at the end: %v", err)
@@ -146,7 +146,7 @@ func testListPayments(net *lntest.NetworkHarness, t *harnessTest) {
// Check that there are no payments after test. // Check that there are no payments after test.
listReq := &lnrpc.ListPaymentsRequest{} listReq := &lnrpc.ListPaymentsRequest{}
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
paymentsResp, err = net.Alice.ListPayments(ctxt, listReq) paymentsResp, err = net.Alice.ListPayments(ctxt, listReq)
if err != nil { if err != nil {
t.Fatalf("error when obtaining Alice payments: %v", err) t.Fatalf("error when obtaining Alice payments: %v", err)

View File

@@ -305,7 +305,7 @@ func testSingleHopSendToRouteCase(net *lntest.NetworkHarness, t *harnessTest,
// Verify that the payment's from Carol's PoV have the correct payment // Verify that the payment's from Carol's PoV have the correct payment
// hash and amount. // hash and amount.
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
paymentsResp, err := carol.ListPayments( paymentsResp, err := carol.ListPayments(
ctxt, &lnrpc.ListPaymentsRequest{}, ctxt, &lnrpc.ListPaymentsRequest{},
) )
@@ -385,7 +385,7 @@ func testSingleHopSendToRouteCase(net *lntest.NetworkHarness, t *harnessTest,
// Verify that the invoices's from Dave's PoV have the correct payment // Verify that the invoices's from Dave's PoV have the correct payment
// hash and amount. // hash and amount.
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
invoicesResp, err := dave.ListInvoices( invoicesResp, err := dave.ListInvoices(
ctxt, &lnrpc.ListInvoiceRequest{}, ctxt, &lnrpc.ListInvoiceRequest{},
) )
@@ -1118,7 +1118,7 @@ func testUpdateChannelPolicyForPrivateChannel(net *lntest.NetworkHarness,
// Check that Alice did make the payment with two HTLCs, one failed and // Check that Alice did make the payment with two HTLCs, one failed and
// one succeeded. // one succeeded.
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
paymentsResp, err := net.Alice.ListPayments( paymentsResp, err := net.Alice.ListPayments(
ctxt, &lnrpc.ListPaymentsRequest{}, ctxt, &lnrpc.ListPaymentsRequest{},
) )

View File

@@ -78,7 +78,7 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
payHash := &lnrpc.PaymentHash{ payHash := &lnrpc.PaymentHash{
RHash: invoiceResp.RHash, RHash: invoiceResp.RHash,
} }
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
dbInvoice, err := net.Bob.LookupInvoice(ctxt, payHash) dbInvoice, err := net.Bob.LookupInvoice(ctxt, payHash)
if err != nil { if err != nil {
t.Fatalf("unable to lookup invoice: %v", err) t.Fatalf("unable to lookup invoice: %v", err)
@@ -105,7 +105,7 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
Memo: "test3", Memo: "test3",
Value: paymentAmt, Value: paymentAmt,
} }
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
invoiceResp, err = net.Bob.AddInvoice(ctxt, invoice) invoiceResp, err = net.Bob.AddInvoice(ctxt, invoice)
if err != nil { if err != nil {
t.Fatalf("unable to add invoice: %v", err) t.Fatalf("unable to add invoice: %v", err)
@@ -198,7 +198,7 @@ func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
RouteHints: hints, RouteHints: hints,
} }
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout) ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
invoiceResp, err = net.Bob.AddInvoice(ctxt, invoice) invoiceResp, err = net.Bob.AddInvoice(ctxt, invoice)
if err != nil { if err != nil {
t.Fatalf("unable to add invoice: %v", err) t.Fatalf("unable to add invoice: %v", err)

View File

@@ -98,8 +98,7 @@ func completePaymentRequests(client lnrpc.LightningClient,
// the send before cancelling the request. We wait for the number of // the send before cancelling the request. We wait for the number of
// updates to one of our channels has increased before we return. // updates to one of our channels has increased before we return.
err = wait.Predicate(func() bool { err = wait.Predicate(func() bool {
ctxt, _ := context.WithTimeout(ctx, defaultTimeout) newListResp, err := client.ListChannels(ctx, req)
newListResp, err := client.ListChannels(ctxt, req)
if err != nil { if err != nil {
return false return false
} }
@@ -337,8 +336,7 @@ func waitForNodeBlockHeight(node *lntest.HarnessNode, height int32) error {
var predErr error var predErr error
err := wait.Predicate(func() bool { err := wait.Predicate(func() bool {
ctxt, _ := context.WithTimeout(ctx, defaultTimeout) info, err := node.GetInfo(ctx, &lnrpc.GetInfoRequest{})
info, err := node.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
if err != nil { if err != nil {
predErr = err predErr = err
return false return false