routing/test: close payment result channel on shutdown, mimicking switch

This commit updates our mock to more closely follow the behavior of the
switch for mocked calls to GetPaymentResult. As it stands, our tests
send a test-created error from the switch when we want to mock shutdown.
In reality, the switch will close its result channel, so we update this
test to follow that behavior. This matters for the commit that follows,
because we start checking the error our payments return. If we have an
error from the switch, our tests will fail with an error that we do
not encounter in practice.
This commit is contained in:
carla 2021-04-23 08:39:40 +02:00
parent 9a78e9da73
commit e0c52e4473
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
2 changed files with 20 additions and 25 deletions

@ -155,10 +155,9 @@ func (m *mockPaymentSession) RequestRoute(_, _ lnwire.MilliSatoshi,
}
type mockPayer struct {
sendResult chan error
paymentResultErr chan error
paymentResult chan *htlcswitch.PaymentResult
quit chan struct{}
sendResult chan error
paymentResult chan *htlcswitch.PaymentResult
quit chan struct{}
}
var _ PaymentAttemptDispatcher = (*mockPayer)(nil)
@ -180,12 +179,16 @@ func (m *mockPayer) GetPaymentResult(paymentID uint64, _ lntypes.Hash,
_ htlcswitch.ErrorDecrypter) (<-chan *htlcswitch.PaymentResult, error) {
select {
case res := <-m.paymentResult:
case res, ok := <-m.paymentResult:
resChan := make(chan *htlcswitch.PaymentResult, 1)
resChan <- res
if !ok {
close(resChan)
} else {
resChan <- res
}
return resChan, nil
case err := <-m.paymentResultErr:
return nil, err
case <-m.quit:
return nil, fmt.Errorf("test quitting")
}

@ -2,7 +2,6 @@ package routing
import (
"crypto/rand"
"fmt"
"sync/atomic"
"testing"
"time"
@ -608,7 +607,7 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
// setupRouter is a helper method that creates and starts the router in
// the desired configuration for this test.
setupRouter := func() (*ChannelRouter, chan error,
chan *htlcswitch.PaymentResult, chan error) {
chan *htlcswitch.PaymentResult) {
chain := newMockChain(startingBlockHeight)
chainView := newMockChainView(chain)
@ -616,13 +615,11 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
// We set uo the use the following channels and a mock Payer to
// synchonize with the interaction to the Switch.
sendResult := make(chan error)
paymentResultErr := make(chan error)
paymentResult := make(chan *htlcswitch.PaymentResult)
payer := &mockPayer{
sendResult: sendResult,
paymentResult: paymentResult,
paymentResultErr: paymentResultErr,
sendResult: sendResult,
paymentResult: paymentResult,
}
router, err := New(Config{
@ -675,10 +672,10 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
t.Fatalf("did not fetch in flight payments at startup")
}
return router, sendResult, paymentResult, paymentResultErr
return router, sendResult, paymentResult
}
router, sendResult, getPaymentResult, getPaymentResultErr := setupRouter()
router, sendResult, getPaymentResult := setupRouter()
defer func() {
if err := router.Stop(); err != nil {
t.Fatal(err)
@ -859,13 +856,9 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
// In this step we manually stop the router.
case stopRouter:
select {
case getPaymentResultErr <- fmt.Errorf(
"shutting down"):
case <-time.After(stepTimeout):
t.Fatalf("unable to send payment " +
"result error")
}
// On shutdown, the switch closes our result channel.
// Mimic this behavior in our mock.
close(getPaymentResult)
if err := router.Stop(); err != nil {
t.Fatalf("unable to restart: %v", err)
@ -873,8 +866,7 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
// In this step we manually start the router.
case startRouter:
router, sendResult, getPaymentResult,
getPaymentResultErr = setupRouter()
router, sendResult, getPaymentResult = setupRouter()
// In this state we expect to receive an error for the
// original payment made.