Skip to content

Commit a4506c9

Browse files
committed
accounts: safe check of pendingPayments in tests
In this commit, we avoid race conditions that can happen for tests that directly access the service's `pendingPayment` map.
1 parent 4d8d4e1 commit a4506c9

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

accounts/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,18 @@ func (s *InterceptorService) removePayment(ctx context.Context,
860860
return nil
861861
}
862862

863+
// hasPayment returns true if the payment is currently being tracked by the
864+
// service.
865+
//
866+
// NOTE: this is currently used only for tests.
867+
func (s *InterceptorService) hasPayment(hash lntypes.Hash) bool {
868+
s.RLock()
869+
defer s.RUnlock()
870+
871+
_, ok := s.pendingPayments[hash]
872+
return ok
873+
}
874+
863875
// successState returns true if a payment was completed successfully.
864876
func successState(status lnrpc.Payment_PaymentStatus) bool {
865877
return status == lnrpc.Payment_SUCCEEDED

accounts/service_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,14 @@ func TestAccountService(t *testing.T) {
383383
// Assert that the invoice subscription succeeded.
384384
require.Contains(t, s.invoiceToAccount, testHash)
385385

386-
// But setting up the payment tracking should have failed.
386+
// But setting up the payment tracking should have
387+
// failed.
387388
require.False(t, s.IsRunning())
388389

389-
// Finally let's assert that we didn't successfully add the
390-
// payment to pending payment, and that lnd isn't awaiting
391-
// the payment request.
392-
require.NotContains(t, s.pendingPayments, testHash)
390+
// Finally let's assert that we didn't successfully add
391+
// the payment to pending payment, and that lnd isn't
392+
// awaiting the payment request.
393+
require.False(t, s.hasPayment(testHash))
393394
r.assertNoPaymentRequest(t)
394395
},
395396
}, {
@@ -426,7 +427,9 @@ func TestAccountService(t *testing.T) {
426427
// This will cause an error send an update over
427428
// the payment channel, which should disable the
428429
// service.
429-
s.pendingPayments = make(map[lntypes.Hash]*trackedPayment)
430+
s.pendingPayments = make(
431+
map[lntypes.Hash]*trackedPayment,
432+
)
430433

431434
// Send an invalid payment over the payment chan
432435
// which should error and disable the service
@@ -568,7 +571,7 @@ func TestAccountService(t *testing.T) {
568571
return p.Status == lnrpc.Payment_FAILED
569572
})
570573

571-
require.NotContains(t, s.pendingPayments, testHash2)
574+
require.False(t, s.hasPayment(testHash2))
572575

573576
// Finally, if an unknown payment turns out to be
574577
// a non-initiated payment, we should stop the tracking
@@ -616,7 +619,7 @@ func TestAccountService(t *testing.T) {
616619

617620
// Ensure that the payment was removed from the pending
618621
// payments.
619-
require.NotContains(t, s.pendingPayments, testHash3)
622+
require.False(t, s.hasPayment(testHash3))
620623
},
621624
}, {
622625
name: "keep track of invoice indexes",

0 commit comments

Comments
 (0)