2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-11 13:11:12 +00:00

liquidity/test: pass full parameters into suggest swaps and add default

As we add more parameters in this PR, we wil want to test values
beyond our default values. This commit updates the suggest swaps test
helper to take a full setup struct, and adds a default set of channels
and rules which will be used in the absense of this setup struct.
This commit is contained in:
carla 2020-10-12 13:34:53 +02:00
parent 7b56804bbe
commit bda6d36a90
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

View File

@ -351,13 +351,16 @@ func TestRestrictedSuggestions(t *testing.T) {
return testCase.loopIn, nil return testCase.loopIn, nil
} }
rules := map[lnwire.ShortChannelID]*ThresholdRule{ lnd.Channels = testCase.channels
params := defaultParameters
params.ChannelRules = map[lnwire.ShortChannelID]*ThresholdRule{
chanID1: chanRule, chanID1: chanRule,
chanID2: chanRule, chanID2: chanRule,
} }
testSuggestSwaps( testSuggestSwaps(
t, cfg, lnd, testCase.channels, rules, t, newSuggestSwapsSetup(cfg, lnd, params),
testCase.expected, testCase.expected,
) )
}) })
@ -397,16 +400,18 @@ func TestSweepFeeLimit(t *testing.T) {
loop.DefaultSweepConfTarget, testCase.feeRate, loop.DefaultSweepConfTarget, testCase.feeRate,
) )
channels := []lndclient.ChannelInfo{ lnd.Channels = []lndclient.ChannelInfo{
channel1, channel1,
} }
rules := map[lnwire.ShortChannelID]*ThresholdRule{ params := defaultParameters
params.ChannelRules = map[lnwire.ShortChannelID]*ThresholdRule{
chanID1: chanRule, chanID1: chanRule,
} }
testSuggestSwaps( testSuggestSwaps(
t, cfg, lnd, channels, rules, testCase.swaps, t, newSuggestSwapsSetup(cfg, lnd, params),
testCase.swaps,
) )
}) })
} }
@ -448,12 +453,15 @@ func TestSuggestSwaps(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
cfg, lnd := newTestConfig() cfg, lnd := newTestConfig()
channels := []lndclient.ChannelInfo{ lnd.Channels = []lndclient.ChannelInfo{
channel1, channel1,
} }
params := defaultParameters
params.ChannelRules = testCase.rules
testSuggestSwaps( testSuggestSwaps(
t, cfg, lnd, channels, testCase.rules, t, newSuggestSwapsSetup(cfg, lnd, params),
testCase.swaps, testCase.swaps,
) )
}) })
@ -513,40 +521,78 @@ func TestFeeLimits(t *testing.T) {
return testCase.quote, nil return testCase.quote, nil
} }
channels := []lndclient.ChannelInfo{ lnd.Channels = []lndclient.ChannelInfo{
channel1, channel1,
} }
rules := map[lnwire.ShortChannelID]*ThresholdRule{
params := defaultParameters
params.ChannelRules = map[lnwire.ShortChannelID]*ThresholdRule{
chanID1: chanRule, chanID1: chanRule,
} }
testSuggestSwaps( testSuggestSwaps(
t, cfg, lnd, channels, rules, testCase.expected, t, newSuggestSwapsSetup(cfg, lnd, params),
testCase.expected,
) )
}) })
} }
} }
// testSuggestSwaps tests getting swap suggestions. // testSuggestSwapsSetup contains the elements that are used to create a
func testSuggestSwaps(t *testing.T, cfg *Config, lnd *test.LndMockServices, // suggest swaps test.
channels []lndclient.ChannelInfo, type testSuggestSwapsSetup struct {
rules map[lnwire.ShortChannelID]*ThresholdRule, cfg *Config
lnd *test.LndMockServices
params Parameters
}
// newSuggestSwapsSetup creates a suggest swaps setup struct.
func newSuggestSwapsSetup(cfg *Config, lnd *test.LndMockServices,
params Parameters) *testSuggestSwapsSetup {
return &testSuggestSwapsSetup{
cfg: cfg,
lnd: lnd,
params: params,
}
}
// testSuggestSwaps tests getting swap suggestions. It takes a setup struct
// which contains custom setup for the test. If this struct is nil, it will
// use the default parameters and setup two channels (channel1 + channel2) with
// chanRule set for each.
func testSuggestSwaps(t *testing.T, setup *testSuggestSwapsSetup,
expected []loop.OutRequest) { expected []loop.OutRequest) {
t.Parallel() t.Parallel()
// Create a mock lnd with the set of channels set in our test case and // If our setup struct is nil, we replace it with our default test
// update our test case lnd to use these channels. // values.
lnd.Channels = channels if setup == nil {
cfg, lnd := newTestConfig()
lnd.Channels = []lndclient.ChannelInfo{
channel1, channel2,
}
params := defaultParameters
params.ChannelRules = map[lnwire.ShortChannelID]*ThresholdRule{
chanID1: chanRule,
chanID2: chanRule,
}
setup = &testSuggestSwapsSetup{
cfg: cfg,
lnd: lnd,
params: params,
}
}
// Create a new manager, get our current set of parameters and update // Create a new manager, get our current set of parameters and update
// them to use the rules set by the test. // them to use the rules set by the test.
manager := NewManager(cfg) manager := NewManager(setup.cfg)
currentParams := manager.GetParameters() err := manager.SetParameters(setup.params)
currentParams.ChannelRules = rules
err := manager.SetParameters(currentParams)
require.NoError(t, err) require.NoError(t, err)
actual, err := manager.SuggestSwaps(context.Background()) actual, err := manager.SuggestSwaps(context.Background())