github.com/decred/dcrlnd@v0.7.6/routing/bandwidth_test.go (about)

     1  package routing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/decred/dcrd/dcrutil/v4"
     7  	"github.com/decred/dcrlnd/htlcswitch"
     8  	"github.com/decred/dcrlnd/lnwire"
     9  	"github.com/go-errors/errors"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // TestBandwidthManager tests getting of bandwidth hints from a bandwidth
    14  // manager.
    15  func TestBandwidthManager(t *testing.T) {
    16  	var (
    17  		chan1ID      uint64         = 101
    18  		chan2ID      uint64         = 102
    19  		chanCapacity dcrutil.Amount = 100000
    20  	)
    21  
    22  	testCases := []struct {
    23  		name              string
    24  		channelID         uint64
    25  		linkQuery         getLinkQuery
    26  		expectedBandwidth lnwire.MilliAtom
    27  		expectFound       bool
    28  	}{
    29  		{
    30  			name:      "channel not ours",
    31  			channelID: chan2ID,
    32  			// Set a link query that will fail our test since we
    33  			// don't expect to query the switch for a channel that
    34  			// is not ours.
    35  			linkQuery: func(id lnwire.ShortChannelID) (
    36  				htlcswitch.ChannelLink, error) {
    37  
    38  				require.Fail(t, "link query unexpected for: "+
    39  					"%v", id)
    40  
    41  				return nil, nil
    42  			},
    43  			expectedBandwidth: 0,
    44  			expectFound:       false,
    45  		},
    46  		{
    47  			name:      "channel ours, link not online",
    48  			channelID: chan1ID,
    49  			linkQuery: func(lnwire.ShortChannelID) (
    50  				htlcswitch.ChannelLink, error) {
    51  
    52  				return nil, htlcswitch.ErrChannelLinkNotFound
    53  			},
    54  			expectedBandwidth: 0,
    55  			expectFound:       true,
    56  		},
    57  		{
    58  			name:      "channel ours, link not eligible",
    59  			channelID: chan1ID,
    60  			linkQuery: func(lnwire.ShortChannelID) (
    61  				htlcswitch.ChannelLink, error) {
    62  
    63  				return &mockLink{
    64  					ineligible: true,
    65  				}, nil
    66  			},
    67  			expectedBandwidth: 0,
    68  			expectFound:       true,
    69  		},
    70  		{
    71  			name:      "channel ours, link can't add htlc",
    72  			channelID: chan1ID,
    73  			linkQuery: func(lnwire.ShortChannelID) (
    74  				htlcswitch.ChannelLink, error) {
    75  
    76  				return &mockLink{
    77  					mayAddOutgoingErr: errors.New(
    78  						"can't add htlc",
    79  					),
    80  				}, nil
    81  			},
    82  			expectedBandwidth: 0,
    83  			expectFound:       true,
    84  		},
    85  		{
    86  			name:      "channel ours, bandwidth available",
    87  			channelID: chan1ID,
    88  			linkQuery: func(lnwire.ShortChannelID) (
    89  				htlcswitch.ChannelLink, error) {
    90  
    91  				return &mockLink{
    92  					bandwidth: 321,
    93  				}, nil
    94  			},
    95  			expectedBandwidth: 321,
    96  			expectFound:       true,
    97  		},
    98  	}
    99  
   100  	for _, testCase := range testCases {
   101  		testCase := testCase
   102  
   103  		t.Run(testCase.name, func(t *testing.T) {
   104  			g := newMockGraph(t)
   105  
   106  			sourceNode := newMockNode(sourceNodeID)
   107  			targetNode := newMockNode(targetNodeID)
   108  
   109  			g.addNode(sourceNode)
   110  			g.addNode(targetNode)
   111  			g.addChannel(
   112  				chan1ID, sourceNodeID, targetNodeID,
   113  				chanCapacity,
   114  			)
   115  
   116  			m, err := newBandwidthManager(
   117  				g, sourceNode.pubkey, testCase.linkQuery,
   118  			)
   119  			require.NoError(t, err)
   120  
   121  			bandwidth, found := m.availableChanBandwidth(
   122  				testCase.channelID, 10,
   123  			)
   124  			require.Equal(t, testCase.expectedBandwidth, bandwidth)
   125  			require.Equal(t, testCase.expectFound, found)
   126  		})
   127  	}
   128  }