liquidity/test: use mock for server restrictions

This change makes the test easier to change when we add loop in
restrictions as well.
pull/419/head
carla 2 years ago
parent 5e47a0c6e9
commit f6c3d77c51
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route" "github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -1292,6 +1293,19 @@ func TestInFlightLimit(t *testing.T) {
} }
} }
type mockServer struct {
mock.Mock
}
// Restrictions mocks a call to the server to get swap size restrictions.
func (m *mockServer) Restrictions(ctx context.Context, swapType swap.Type) (
*Restrictions, error) {
args := m.Called(ctx, swapType)
return args.Get(0).(*Restrictions), args.Error(1)
}
// TestSizeRestrictions tests the use of client-set size restrictions on swaps. // TestSizeRestrictions tests the use of client-set size restrictions on swaps.
func TestSizeRestrictions(t *testing.T) { func TestSizeRestrictions(t *testing.T) {
var ( var (
@ -1321,9 +1335,7 @@ func TestSizeRestrictions(t *testing.T) {
// has configured. // has configured.
clientRestrictions Restrictions clientRestrictions Restrictions
// server holds the server's mocked responses to our terms prepareMock func(m *mockServer)
// endpoint.
serverRestrictions []Restrictions
// suggestions is the set of suggestions we expect. // suggestions is the set of suggestions we expect.
suggestions *Suggestions suggestions *Suggestions
@ -1336,9 +1348,6 @@ func TestSizeRestrictions(t *testing.T) {
clientRestrictions: Restrictions{ clientRestrictions: Restrictions{
Minimum: 7000, Minimum: 7000,
}, },
serverRestrictions: []Restrictions{
serverRestrictions, serverRestrictions,
},
suggestions: &Suggestions{ suggestions: &Suggestions{
OutSwaps: []loop.OutRequest{ OutSwaps: []loop.OutRequest{
chan1Rec, chan1Rec,
@ -1352,9 +1361,6 @@ func TestSizeRestrictions(t *testing.T) {
clientRestrictions: Restrictions{ clientRestrictions: Restrictions{
Minimum: 8000, Minimum: 8000,
}, },
serverRestrictions: []Restrictions{
serverRestrictions, serverRestrictions,
},
suggestions: &Suggestions{ suggestions: &Suggestions{
DisqualifiedChans: map[lnwire.ShortChannelID]Reason{ DisqualifiedChans: map[lnwire.ShortChannelID]Reason{
chanID1: ReasonLiquidityOk, chanID1: ReasonLiquidityOk,
@ -1367,9 +1373,6 @@ func TestSizeRestrictions(t *testing.T) {
clientRestrictions: Restrictions{ clientRestrictions: Restrictions{
Maximum: 7000, Maximum: 7000,
}, },
serverRestrictions: []Restrictions{
serverRestrictions, serverRestrictions,
},
suggestions: &Suggestions{ suggestions: &Suggestions{
OutSwaps: []loop.OutRequest{ OutSwaps: []loop.OutRequest{
outSwap, outSwap,
@ -1387,12 +1390,26 @@ func TestSizeRestrictions(t *testing.T) {
Minimum: 6500, Minimum: 6500,
Maximum: 9000, Maximum: 9000,
}, },
serverRestrictions: []Restrictions{ prepareMock: func(m *mockServer) {
serverRestrictions, restrictions := serverRestrictions
{
Minimum: 5000, m.On(
Maximum: 6000, "Restrictions", mock.Anything,
}, swap.TypeOut,
).Return(
&restrictions, nil,
).Once()
m.On(
"Restrictions", mock.Anything,
swap.TypeOut,
).Return(
&Restrictions{
Minimum: 5000,
Maximum: 6000,
}, nil,
).Once()
}, },
suggestions: nil, suggestions: nil,
expectedError: ErrMaxExceedsServer, expectedError: ErrMaxExceedsServer,
@ -1415,27 +1432,32 @@ func TestSizeRestrictions(t *testing.T) {
chanID1: chanRule, chanID1: chanRule,
} }
// callCount tracks the number of calls we make to // Use a mock that has our expected calls for the test
// our restrictions endpoint. // case set to provide server restrictions.
var callCount int mockServer := &mockServer{}
cfg.Restrictions = func(_ context.Context, _ swap.Type) (
*Restrictions, error) {
restrictions := testCase.serverRestrictions[callCount] // If the test wants us to prime the mock, use its
callCount++ // function, otherwise just return our default
// restrictions.
if testCase.prepareMock != nil {
testCase.prepareMock(mockServer)
} else {
restrictions := serverRestrictions
return &restrictions, nil mockServer.On(
"Restrictions", mock.Anything,
swap.TypeOut,
).Return(&restrictions, nil)
} }
cfg.Restrictions = mockServer.Restrictions
testSuggestSwaps( testSuggestSwaps(
t, newSuggestSwapsSetup(cfg, lnd, params), t, newSuggestSwapsSetup(cfg, lnd, params),
testCase.suggestions, testCase.expectedError, testCase.suggestions, testCase.expectedError,
) )
require.Equal( mockServer.AssertExpectations(t)
t, callCount, len(testCase.serverRestrictions),
"too many restrictions provided by mock",
)
}) })
} }
} }

Loading…
Cancel
Save