routing/test: assert error value for payment failures

This commit is contained in:
carla 2021-04-23 08:39:41 +02:00
parent e0c52e4473
commit 198d567cb2
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -14,6 +14,7 @@ import (
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"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/require"
) )
const stepTimeout = 5 * time.Second const stepTimeout = 5 * time.Second
@ -58,6 +59,11 @@ type paymentLifecycleTestCase struct {
// routes is the sequence of routes we will provide to the // routes is the sequence of routes we will provide to the
// router when it requests a new route. // router when it requests a new route.
routes []*route.Route routes []*route.Route
// paymentErr is the error we expect our payment to fail with. This
// should be nil for tests with paymentSuccess steps and non-nil for
// payments with paymentError steps.
paymentErr error
} }
const ( const (
@ -274,7 +280,8 @@ func TestRouterPaymentStateMachine(t *testing.T) {
routerFailPayment, routerFailPayment,
paymentError, paymentError,
}, },
routes: []*route.Route{rt}, routes: []*route.Route{rt},
paymentErr: channeldb.FailureReasonNoRoute,
}, },
{ {
// We expect the payment to fail immediately if we have // We expect the payment to fail immediately if we have
@ -286,7 +293,8 @@ func TestRouterPaymentStateMachine(t *testing.T) {
routerFailPayment, routerFailPayment,
paymentError, paymentError,
}, },
routes: []*route.Route{}, routes: []*route.Route{},
paymentErr: channeldb.FailureReasonNoRoute,
}, },
{ {
// A normal payment flow, where we attempt to resend // A normal payment flow, where we attempt to resend
@ -354,7 +362,8 @@ func TestRouterPaymentStateMachine(t *testing.T) {
getPaymentResultSuccess, getPaymentResultSuccess,
routerSettleAttempt, routerSettleAttempt,
}, },
routes: []*route.Route{rt}, routes: []*route.Route{rt},
paymentErr: ErrRouterShuttingDown,
}, },
{ {
// Tests that we are allowed to resend a payment after // Tests that we are allowed to resend a payment after
@ -391,7 +400,8 @@ func TestRouterPaymentStateMachine(t *testing.T) {
routerSettleAttempt, routerSettleAttempt,
resentPaymentSuccess, resentPaymentSuccess,
}, },
routes: []*route.Route{rt}, routes: []*route.Route{rt},
paymentErr: channeldb.FailureReasonNoRoute,
}, },
// ===================================== // =====================================
@ -576,6 +586,7 @@ func TestRouterPaymentStateMachine(t *testing.T) {
routes: []*route.Route{ routes: []*route.Route{
shard, shard, shard, shard, shard, shard, shard, shard, shard, shard, shard, shard,
}, },
paymentErr: channeldb.FailureReasonPaymentDetails,
}, },
} }
@ -871,11 +882,12 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
// In this state we expect to receive an error for the // In this state we expect to receive an error for the
// original payment made. // original payment made.
case paymentError: case paymentError:
require.Error(t, test.paymentErr,
"paymentError not set")
select { select {
case err := <-paymentResult: case err := <-paymentResult:
if err == nil { require.Equal(t, test.paymentErr, err)
t.Fatalf("expected error")
}
case <-time.After(stepTimeout): case <-time.After(stepTimeout):
t.Fatalf("got no payment result") t.Fatalf("got no payment result")
@ -884,6 +896,8 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
// In this state we expect the original payment to // In this state we expect the original payment to
// succeed. // succeed.
case paymentSuccess: case paymentSuccess:
require.Nil(t, test.paymentErr)
select { select {
case err := <-paymentResult: case err := <-paymentResult:
if err != nil { if err != nil {