peer: Create mockswitch, publTx, notifier in ..

test_utils

Signed-off-by: Ononiwu Maureen <maureen.ononiwu@outlook.com>
This commit is contained in:
Ononiwu Maureen 2024-03-15 10:59:48 +01:00 committed by Ononiwu Maureen
parent 536c69b793
commit 8f76c5eeef
No known key found for this signature in database
GPG Key ID: 82470E85193AD2DA
2 changed files with 48 additions and 119 deletions

View File

@ -39,18 +39,7 @@ var (
func TestPeerChannelClosureShutdownResponseLinkRemoved(t *testing.T) {
t.Parallel()
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{}
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, noUpdate, mockSwitch,
)
harness, err := createTestPeerWithChannel(t, noUpdate)
require.NoError(t, err, "unable to create test channels")
alicePeer := harness.peer
@ -90,22 +79,14 @@ func TestPeerChannelClosureShutdownResponseLinkRemoved(t *testing.T) {
func TestPeerChannelClosureAcceptFeeResponder(t *testing.T) {
t.Parallel()
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{}
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, noUpdate, mockSwitch,
)
harness, err := createTestPeerWithChannel(t, noUpdate)
require.NoError(t, err, "unable to create test channels")
alicePeer := harness.peer
bobChan := harness.channel
mockSwitch := harness.mockSwitch
broadcastTxChan := harness.publishTx
notifier := harness.notifier
chanPoint := bobChan.ChannelPoint()
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
@ -198,22 +179,14 @@ func TestPeerChannelClosureAcceptFeeResponder(t *testing.T) {
func TestPeerChannelClosureAcceptFeeInitiator(t *testing.T) {
t.Parallel()
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{}
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, noUpdate, mockSwitch,
)
harness, err := createTestPeerWithChannel(t, noUpdate)
require.NoError(t, err, "unable to create test channels")
bobChan := harness.channel
alicePeer := harness.peer
mockSwitch := harness.mockSwitch
broadcastTxChan := harness.publishTx
notifier := harness.notifier
chanPoint := bobChan.ChannelPoint()
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
@ -325,22 +298,14 @@ func TestPeerChannelClosureAcceptFeeInitiator(t *testing.T) {
func TestPeerChannelClosureFeeNegotiationsResponder(t *testing.T) {
t.Parallel()
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{}
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, noUpdate, mockSwitch,
)
harness, err := createTestPeerWithChannel(t, noUpdate)
require.NoError(t, err, "unable to create test channels")
bobChan := harness.channel
alicePeer := harness.peer
mockSwitch := harness.mockSwitch
broadcastTxChan := harness.publishTx
notifier := harness.notifier
chanPoint := bobChan.ChannelPoint()
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
@ -515,22 +480,14 @@ func TestPeerChannelClosureFeeNegotiationsResponder(t *testing.T) {
func TestPeerChannelClosureFeeNegotiationsInitiator(t *testing.T) {
t.Parallel()
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{}
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, noUpdate, mockSwitch,
)
harness, err := createTestPeerWithChannel(t, noUpdate)
require.NoError(t, err, "unable to create test channels")
alicePeer := harness.peer
bobChan := harness.channel
mockSwitch := harness.mockSwitch
broadcastTxChan := harness.publishTx
notifier := harness.notifier
chanPoint := bobChan.ChannelPoint()
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
@ -845,19 +802,9 @@ func TestCustomShutdownScript(t *testing.T) {
test := test
t.Run(test.name, func(t *testing.T) {
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{}
// Open a channel.
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, test.update,
mockSwitch,
t, test.update,
)
if err != nil {
t.Fatalf("unable to create test channels: %v", err)
@ -865,6 +812,7 @@ func TestCustomShutdownScript(t *testing.T) {
alicePeer := harness.peer
bobChan := harness.channel
mockSwitch := harness.mockSwitch
chanPoint := bobChan.ChannelPoint()
chanID := lnwire.NewChanIDFromOutPoint(chanPoint)
@ -1203,19 +1151,7 @@ func TestUpdateNextRevocation(t *testing.T) {
require := require.New(t)
// TODO(yy): create interface for lnwallet.LightningChannel so we can
// easily mock it without the following setups.
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{}
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, noUpdate, mockSwitch,
)
harness, err := createTestPeerWithChannel(t, noUpdate)
require.NoError(err, "unable to create test channels")
bobChan := harness.channel
@ -1433,15 +1369,6 @@ func TestHandleRemovePendingChannel(t *testing.T) {
func TestStartupWriteMessageRace(t *testing.T) {
t.Parallel()
// Set up parameters for createTestPeerWithChannel.
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{}
// Use a callback to extract the channel created by
// createTestPeerWithChannel, so we can mark it borked below.
// We can't mark it borked within the callback, since the channel hasn't
@ -1453,9 +1380,7 @@ func TestStartupWriteMessageRace(t *testing.T) {
// createTestPeerWithChannel creates a peer and a channel with that
// peer.
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, getChannels, mockSwitch,
)
harness, err := createTestPeerWithChannel(t, getChannels)
require.NoError(t, err, "unable to create test channel")
peer := harness.peer
@ -1540,20 +1465,8 @@ func TestStartupWriteMessageRace(t *testing.T) {
func TestRemovePendingChannel(t *testing.T) {
t.Parallel()
// Set up parameters for createTestPeerWithChannel.
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{}
// createTestPeerWithChannel creates a peer and a channel with that
// peer.
harness, err := createTestPeerWithChannel(
t, notifier, broadcastTxChan, noUpdate, mockSwitch,
)
// createTestPeerWithChannel creates a peer and a channel.
harness, err := createTestPeerWithChannel(t, noUpdate)
require.NoError(t, err, "unable to create test channel")
peer := harness.peer

View File

@ -54,18 +54,31 @@ var (
var noUpdate = func(a, b *channeldb.OpenChannel) {}
type peerTestCtx struct {
peer *Brontide
channel *lnwallet.LightningChannel
peer *Brontide
channel *lnwallet.LightningChannel
notifier *mock.ChainNotifier
publishTx <-chan *wire.MsgTx
mockSwitch *mockMessageSwitch
}
// createTestPeerWithChannel creates a channel between two nodes, and returns a
// peer for one of the nodes, together with the channel seen from both nodes.
// It takes an updateChan function which can be used to modify the default
// values on the channel states for each peer.
func createTestPeerWithChannel(t *testing.T, notifier chainntnfs.ChainNotifier,
publTx chan *wire.MsgTx, updateChan func(a, b *channeldb.OpenChannel),
mockSwitch *mockMessageSwitch) (
*peerTestCtx, error) {
func createTestPeerWithChannel(t *testing.T, updateChan func(a,
b *channeldb.OpenChannel)) (*peerTestCtx, error) {
// TODO(yy): create interface for lnwallet.LightningChannel so we can
// easily mock it without the following setups.
notifier := &mock.ChainNotifier{
SpendChan: make(chan *chainntnfs.SpendDetail),
EpochChan: make(chan *chainntnfs.BlockEpoch),
ConfChan: make(chan *chainntnfs.TxConfirmation),
}
var mockSwitch *mockMessageSwitch
publishTx := make(chan *wire.MsgTx)
nodeKeyLocator := keychain.KeyLocator{
Family: keychain.KeyFamilyNodeKey,
@ -320,7 +333,7 @@ func createTestPeerWithChannel(t *testing.T, notifier chainntnfs.ChainNotifier,
wallet := &lnwallet.LightningWallet{
WalletController: &mock.WalletController{
RootKey: aliceKeyPriv,
PublishedTransactions: publTx,
PublishedTransactions: publishTx,
},
}
@ -425,8 +438,11 @@ func createTestPeerWithChannel(t *testing.T, notifier chainntnfs.ChainNotifier,
go alicePeer.channelManager()
return &peerTestCtx{
peer: alicePeer,
channel: channelBob,
peer: alicePeer,
channel: channelBob,
notifier: notifier,
publishTx: publishTx,
mockSwitch: mockSwitch,
}, nil
}