itest: manage context inside WaitForChannelOpen and WaitForChannelClose

This commit is contained in:
yyforyongyu 2021-08-20 01:20:06 +08:00
parent 1629858a3d
commit 3e26d77f91
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
7 changed files with 22 additions and 22 deletions

View file

@ -1123,9 +1123,13 @@ func (n *NetworkHarness) OpenPendingChannel(ctx context.Context,
// consuming a message from the past open channel stream. If the passed context
// has a timeout, then if the timeout is reached before the channel has been
// opened, then an error is returned.
func (n *NetworkHarness) WaitForChannelOpen(ctx context.Context,
func (n *NetworkHarness) WaitForChannelOpen(
openChanStream lnrpc.Lightning_OpenChannelClient) (*lnrpc.ChannelPoint, error) {
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, ChannelOpenTimeout)
defer cancel()
errChan := make(chan error)
respChan := make(chan *lnrpc.ChannelPoint)
go func() {
@ -1294,9 +1298,13 @@ func (n *NetworkHarness) CloseChannel(ctx context.Context,
// stream that the node has deemed the channel has been fully closed. If the
// passed context has a timeout, then if the timeout is reached before the
// notification is received then an error is returned.
func (n *NetworkHarness) WaitForChannelClose(ctx context.Context,
func (n *NetworkHarness) WaitForChannelClose(
closeChanStream lnrpc.Lightning_CloseChannelClient) (*chainhash.Hash, error) {
ctxb := context.Background()
ctx, cancel := context.WithTimeout(ctxb, ChannelCloseTimeout)
defer cancel()
errChan := make(chan error)
updateChan := make(chan *lnrpc.CloseStatusUpdate_ChanClose)
go func() {

View file

@ -84,7 +84,7 @@ func openChannelAndAssert(t *harnessTest, net *lntest.NetworkHarness,
// case that the channel is public, it is announced to the network.
block := mineBlocks(t, net, 6, 1)[0]
fundingChanPoint, err := net.WaitForChannelOpen(ctx, chanOpenUpdate)
fundingChanPoint, err := net.WaitForChannelOpen(chanOpenUpdate)
require.NoError(t.t, err, "error while waiting for channel open")
fundingTxID, err := lnrpc.GetChanPointFundingTxid(fundingChanPoint)
@ -343,7 +343,7 @@ func assertChannelClosed(ctx context.Context, t *harnessTest,
block := mineBlocks(t, net, 1, expectedTxes)[0]
closingTxid, err := net.WaitForChannelClose(ctx, closeUpdates)
closingTxid, err := net.WaitForChannelClose(closeUpdates)
require.NoError(t.t, err, "error while waiting for channel close")
assertTxInBlock(t, block, closingTxid)

View file

@ -314,8 +314,7 @@ func testUnannouncedChannels(net *lntest.NetworkHarness, t *harnessTest) {
// One block is enough to make the channel ready for use, since the
// nodes have defaultNumConfs=1 set.
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
fundingChanPoint, err := net.WaitForChannelOpen(ctxt, chanOpenUpdate)
fundingChanPoint, err := net.WaitForChannelOpen(chanOpenUpdate)
if err != nil {
t.Fatalf("error while waiting for channel open: %v", err)
}
@ -324,7 +323,7 @@ func testUnannouncedChannels(net *lntest.NetworkHarness, t *harnessTest) {
req := &lnrpc.ChannelGraphRequest{
IncludeUnannounced: true,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
chanGraph, err := net.Alice.DescribeGraph(ctxt, req)
if err != nil {
t.Fatalf("unable to query alice's graph: %v", err)

View file

@ -354,8 +354,7 @@ func testUnconfirmedChannelFunding(net *lntest.NetworkHarness, t *harnessTest) {
// parties. Two transactions should be mined, the unconfirmed spend and
// the funding tx.
mineBlocks(t, net, 6, 2)
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
chanPoint, err := net.WaitForChannelOpen(ctxt, chanOpenUpdate)
chanPoint, err := net.WaitForChannelOpen(chanOpenUpdate)
require.NoError(t.t, err, "error while waitinng for channel open")
// With the channel open, we'll check the balances on each side of the

View file

@ -547,8 +547,7 @@ func testMaxPendingChannels(net *lntest.NetworkHarness, t *harnessTest) {
chanPoints := make([]*lnrpc.ChannelPoint, maxPendingChannels)
for i, stream := range openStreams {
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
fundingChanPoint, err := net.WaitForChannelOpen(ctxt, stream)
fundingChanPoint, err := net.WaitForChannelOpen(stream)
if err != nil {
t.Fatalf("error while waiting for channel open: %v", err)
}

View file

@ -203,8 +203,7 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
// block.
block := mineBlocks(t, net, 1, 1)[0]
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
breachTXID, err := net.WaitForChannelClose(closeUpdates)
if err != nil {
t.Fatalf("error while waiting for channel close: %v", err)
}
@ -439,8 +438,7 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness
t.Fatalf("unable to stop Dave's node: %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
breachTXID, err := net.WaitForChannelClose(closeUpdates)
if err != nil {
t.Fatalf("error while waiting for channel close: %v", err)
}
@ -746,8 +744,7 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// Finally, wait for the final close status update, then ensure that
// the closing transaction was included in the block.
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
breachTXID, err := net.WaitForChannelClose(closeUpdates)
if err != nil {
t.Fatalf("error while waiting for channel close: %v", err)
}
@ -1181,8 +1178,7 @@ func testRevokedCloseRetributionAltruistWatchtowerCase(
// block.
block := mineBlocks(t, net, 1, 1)[0]
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
breachTXID, err := net.WaitForChannelClose(ctxt, closeUpdates)
breachTXID, err := net.WaitForChannelClose(closeUpdates)
if err != nil {
t.Fatalf("error while waiting for channel close: %v", err)
}

View file

@ -840,8 +840,7 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
// nodes have defaultNumConfs=1 set.
block := mineBlocks(t, net, 1, 1)[0]
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
chanPointPrivate, err := net.WaitForChannelOpen(ctxt, chanOpenUpdate)
chanPointPrivate, err := net.WaitForChannelOpen(chanOpenUpdate)
if err != nil {
t.Fatalf("error while waiting for channel open: %v", err)
}
@ -857,7 +856,7 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
Hash: *fundingTxID,
Index: chanPointPrivate.OutputIndex,
}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
err = net.AssertChannelExists(ctxt, carol, &privateFundPoint)
if err != nil {
t.Fatalf("unable to assert channel existence: %v", err)