From 3530254ff435cc5504ab695f14db516fae62f431 Mon Sep 17 00:00:00 2001 From: ziggie Date: Sun, 21 Jan 2024 16:28:52 +0000 Subject: [PATCH] peer: add unit test. Add a unit test for the removal of a pending channel. --- peer/brontide_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/peer/brontide_test.go b/peer/brontide_test.go index 7ef02fff3..2ee9b3dcc 100644 --- a/peer/brontide_test.go +++ b/peer/brontide_test.go @@ -2,6 +2,7 @@ package peer import ( "bytes" + "fmt" "testing" "time" @@ -16,6 +17,7 @@ import ( "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/lntest/mock" + "github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet/chancloser" "github.com/lightningnetwork/lnd/lnwire" @@ -1451,3 +1453,88 @@ func TestStartupWriteMessageRace(t *testing.T) { } } } + +// TestRemovePendingChannel checks that we are able to remove a pending channel +// successfully from the peers channel map. This also makes sure the +// removePendingChannel is initialized so we don't send to a nil channel and +// get stuck. +func TestRemovePendingChannel(t *testing.T) { + t.Parallel() + + // Set up parameters for createTestPeer. + notifier := &mock.ChainNotifier{ + SpendChan: make(chan *chainntnfs.SpendDetail), + EpochChan: make(chan *chainntnfs.BlockEpoch), + ConfChan: make(chan *chainntnfs.TxConfirmation), + } + broadcastTxChan := make(chan *wire.MsgTx) + mockSwitch := &mockMessageSwitch{} + + // createTestPeer creates a peer and a channel with that peer. + peer, _, err := createTestPeer( + t, notifier, broadcastTxChan, noUpdate, mockSwitch, + ) + require.NoError(t, err, "unable to create test channel") + + // Add a pending channel to the peer Alice. + errChan := make(chan error, 1) + pendingChanID := lnwire.ChannelID{1} + req := &newChannelMsg{ + channelID: pendingChanID, + err: errChan, + } + + select { + case peer.newPendingChannel <- req: + // Operation completed successfully + case <-time.After(timeout): + t.Fatalf("not able to remove pending channel") + } + + // Make sure the channel was added as a pending channel. + // The peer was already created with one active channel therefore the + // `activeChannels` had already one channel prior to adding the new one. + // The `addedChannels` map only tracks new channels in the current life + // cycle therefore the initial channel is not part of it. + err = wait.NoError(func() error { + if peer.activeChannels.Len() == 2 && + peer.addedChannels.Len() == 1 { + + return nil + } + + return fmt.Errorf("pending channel not successfully added") + }, wait.DefaultTimeout) + + require.NoError(t, err) + + // Now try to remove it, the errChan needs to be reopened because it was + // closed during the pending channel registration above. + errChan = make(chan error, 1) + req = &newChannelMsg{ + channelID: pendingChanID, + err: errChan, + } + + select { + case peer.removePendingChannel <- req: + // Operation completed successfully + case <-time.After(timeout): + t.Fatalf("not able to remove pending channel") + } + + // Make sure the pending channel is successfully removed from both + // channel maps. + // The initial channel between the peer is still active at this point. + err = wait.NoError(func() error { + if peer.activeChannels.Len() == 1 && + peer.addedChannels.Len() == 0 { + + return nil + } + + return fmt.Errorf("pending channel not successfully removed") + }, wait.DefaultTimeout) + + require.NoError(t, err) +}