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

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

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