routing: unify all dummy errors to be errDummy

This commit is contained in:
yyforyongyu
2023-10-20 07:45:52 +08:00
parent 7ccb77269d
commit 98378d9408

View File

@@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// errDummy is used by the mockers to return a dummy error.
var errDummy = errors.New("dummy") var errDummy = errors.New("dummy")
// createTestPaymentLifecycle creates a `paymentLifecycle` without mocks. // createTestPaymentLifecycle creates a `paymentLifecycle` without mocks.
@@ -304,10 +305,9 @@ func TestCheckTimeoutTimedOut(t *testing.T) {
// by the function too. // by the function too.
// //
// Mock `FailPayment` to return a dummy error. // Mock `FailPayment` to return a dummy error.
dummyErr := errors.New("dummy")
ct = &mockControlTower{} ct = &mockControlTower{}
ct.On("FailPayment", ct.On("FailPayment",
p.identifier, channeldb.FailureReasonTimeout).Return(dummyErr) p.identifier, channeldb.FailureReasonTimeout).Return(errDummy)
// Mount the mocked control tower. // Mount the mocked control tower.
p.router.cfg.Control = ct p.router.cfg.Control = ct
@@ -320,7 +320,7 @@ func TestCheckTimeoutTimedOut(t *testing.T) {
// Call the function and expect an error. // Call the function and expect an error.
err = p.checkTimeout() err = p.checkTimeout()
require.ErrorIs(t, err, dummyErr) require.ErrorIs(t, err, errDummy)
// Assert that `FailPayment` is called as expected. // Assert that `FailPayment` is called as expected.
ct.AssertExpectations(t) ct.AssertExpectations(t)
@@ -399,15 +399,14 @@ func TestRequestRouteHandleCriticalErr(t *testing.T) {
p.feeLimit = ps.FeesPaid + 1 p.feeLimit = ps.FeesPaid + 1
// Mock the paySession's `RequestRoute` method to return an error. // Mock the paySession's `RequestRoute` method to return an error.
dummyErr := errors.New("dummy")
paySession.On("RequestRoute", paySession.On("RequestRoute",
mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything,
).Return(nil, dummyErr) ).Return(nil, errDummy)
result, err := p.requestRoute(ps) result, err := p.requestRoute(ps)
// Expect an error is returned since it's critical. // Expect an error is returned since it's critical.
require.ErrorIs(t, err, dummyErr, "error not matched") require.ErrorIs(t, err, errDummy, "error not matched")
require.Nil(t, result, "expected no route returned") require.Nil(t, result, "expected no route returned")
// Assert that `RequestRoute` is called as expected. // Assert that `RequestRoute` is called as expected.
@@ -462,10 +461,9 @@ func TestRequestRouteFailPaymentError(t *testing.T) {
// Mock the control tower's `FailPayment` method. // Mock the control tower's `FailPayment` method.
ct := &mockControlTower{} ct := &mockControlTower{}
dummyErr := errors.New("dummy")
ct.On("FailPayment", ct.On("FailPayment",
p.identifier, errNoTlvPayload.FailureReason(), p.identifier, errNoTlvPayload.FailureReason(),
).Return(dummyErr) ).Return(errDummy)
// Mount the mocked control tower and payment session. // Mount the mocked control tower and payment session.
p.router.cfg.Control = ct p.router.cfg.Control = ct
@@ -489,7 +487,7 @@ func TestRequestRouteFailPaymentError(t *testing.T) {
result, err := p.requestRoute(ps) result, err := p.requestRoute(ps)
// Expect an error is returned. // Expect an error is returned.
require.ErrorIs(t, err, dummyErr, "error not matched") require.ErrorIs(t, err, errDummy, "error not matched")
require.Nil(t, result, "expected no route returned") require.Nil(t, result, "expected no route returned")
// Assert that `RequestRoute` is called as expected. // Assert that `RequestRoute` is called as expected.
@@ -1247,18 +1245,17 @@ func TestCollectResultExitOnErr(t *testing.T) {
// `CancelShard` should be called with the attemptID. // `CancelShard` should be called with the attemptID.
m.shardTracker.On("CancelShard", attempt.AttemptID).Return(nil).Once() m.shardTracker.On("CancelShard", attempt.AttemptID).Return(nil).Once()
// Mock `FailAttempt` to return a switch error. // Mock `FailAttempt` to return a dummy error.
switchErr := errors.New("switch err")
m.control.On("FailAttempt", m.control.On("FailAttempt",
p.identifier, attempt.AttemptID, mock.Anything, p.identifier, attempt.AttemptID, mock.Anything,
).Return(nil, switchErr).Once() ).Return(nil, errDummy).Once()
// Mock the clock to return a current time. // Mock the clock to return a current time.
m.clock.On("Now").Return(time.Now()) m.clock.On("Now").Return(time.Now())
// Now call the method under test. // Now call the method under test.
result, err := p.collectResult(attempt) result, err := p.collectResult(attempt)
require.ErrorIs(t, err, switchErr, "expected switch error") require.ErrorIs(t, err, errDummy, "expected dummy error")
require.Nil(t, result, "expected nil attempt") require.Nil(t, result, "expected nil attempt")
} }
@@ -1299,18 +1296,17 @@ func TestCollectResultExitOnResultErr(t *testing.T) {
// `CancelShard` should be called with the attemptID. // `CancelShard` should be called with the attemptID.
m.shardTracker.On("CancelShard", attempt.AttemptID).Return(nil).Once() m.shardTracker.On("CancelShard", attempt.AttemptID).Return(nil).Once()
// Mock `FailAttempt` to return a switch error. // Mock `FailAttempt` to return a dummy error.
switchErr := errors.New("switch err")
m.control.On("FailAttempt", m.control.On("FailAttempt",
p.identifier, attempt.AttemptID, mock.Anything, p.identifier, attempt.AttemptID, mock.Anything,
).Return(nil, switchErr).Once() ).Return(nil, errDummy).Once()
// Mock the clock to return a current time. // Mock the clock to return a current time.
m.clock.On("Now").Return(time.Now()) m.clock.On("Now").Return(time.Now())
// Now call the method under test. // Now call the method under test.
result, err := p.collectResult(attempt) result, err := p.collectResult(attempt)
require.ErrorIs(t, err, switchErr, "expected switch error") require.ErrorIs(t, err, errDummy, "expected dummy error")
require.Nil(t, result, "expected nil attempt") require.Nil(t, result, "expected nil attempt")
} }