@@ -4187,37 +4187,46 @@ func TestSendMPPaymentFailedWithShardsInFlight(t *testing.T) {
41874187 // Create a buffered chan and it will be returned by GetPaymentResult.
41884188 payer .resultChan = make (chan * htlcswitch.PaymentResult , 10 )
41894189
4190- // We use the failAttemptCount to track how many attempts we want to
4191- // fail. Each time the following mock method is called, the count gets
4192- // updated.
4193- failAttemptCount := 0
4190+ // We use the getPaymentResultCnt to track how many times we called
4191+ // GetPaymentResult. As shard launch is sequential, and we fail the
4192+ // first shard that calls GetPaymentResult, we may end up with different
4193+ // counts since the lifecycle itself is asynchronous. To avoid flakes
4194+ // due to this undeterminsitic behavior, we'll compare the final
4195+ // getPaymentResultCnt with other counters to create a final test
4196+ // expectation.
4197+ getPaymentResultCnt := 0
41944198 payer .On ("GetPaymentResult" ,
41954199 mock .Anything , identifier , mock .Anything ,
41964200 ).Run (func (args mock.Arguments ) {
41974201 // Before the mock method is returned, we send the result to
41984202 // the read-only chan.
41994203
42004204 // Update the counter.
4201- failAttemptCount ++
4205+ getPaymentResultCnt ++
42024206
42034207 // We fail the first attempt with terminal error.
4204- if failAttemptCount == 1 {
4208+ if getPaymentResultCnt == 1 {
42054209 payer .resultChan <- & htlcswitch.PaymentResult {
42064210 Error : htlcswitch .NewForwardingError (
42074211 & lnwire.FailIncorrectDetails {},
42084212 1 ,
42094213 ),
42104214 }
42114215 return
4212-
42134216 }
42144217
4215- // For the rest attempts we will NOT send anything to the
4216- // resultChan, thus making all the shards in active state,
4217- // neither settled or failed.
4218+ // For the rest of the attempts we'll simulate that a network
4219+ // result update_fail_htlc has been received. This way the
4220+ // payment will fail cleanly.
4221+ payer .resultChan <- & htlcswitch.PaymentResult {
4222+ Error : htlcswitch .NewForwardingError (
4223+ & lnwire.FailTemporaryChannelFailure {},
4224+ 1 ,
4225+ ),
4226+ }
42184227 })
42194228
4220- // Mock the FailAttempt method to fail EXACTLY once.
4229+ // Mock the FailAttempt method to fail (at least once) .
42214230 var failedAttempt channeldb.HTLCAttempt
42224231 controlTower .On ("FailAttempt" ,
42234232 identifier , mock .Anything , mock .Anything ,
@@ -4227,22 +4236,27 @@ func TestSendMPPaymentFailedWithShardsInFlight(t *testing.T) {
42274236 failedAttempt = payment .HTLCs [0 ]
42284237 failedAttempt .Failure = & channeldb.HTLCFailInfo {}
42294238 payment .HTLCs [0 ] = failedAttempt
4230- }). Once ()
4239+ })
42314240
42324241 // Setup ReportPaymentFail to return nil reason and error so the
42334242 // payment won't fail.
42344243 failureReason := channeldb .FailureReasonPaymentDetails
4244+ cntReportPaymentFail := 0
42354245 missionControl .On ("ReportPaymentFail" ,
42364246 mock .Anything , mock .Anything , mock .Anything , mock .Anything ,
42374247 ).Return (& failureReason , nil ).Run (func (args mock.Arguments ) {
42384248 payment .FailureReason = & failureReason
4239- }).Once ()
4249+ cntReportPaymentFail ++
4250+ })
42404251
42414252 // Simple mocking the rest.
4242- controlTower .On ("Fail" , identifier , failureReason ).Return (nil ).Once ()
4253+ cntFail := 0
4254+ controlTower .On ("Fail" , identifier , failureReason ).Return (nil )
42434255 payer .On ("SendHTLC" ,
42444256 mock .Anything , mock .Anything , mock .Anything ,
4245- ).Return (nil )
4257+ ).Return (nil ).Run (func (args mock.Arguments ) {
4258+ cntFail ++
4259+ })
42464260
42474261 // Call the actual method SendPayment on router. This is place inside a
42484262 // goroutine so we can set a timeout for the whole test, in case
@@ -4264,6 +4278,9 @@ func TestSendMPPaymentFailedWithShardsInFlight(t *testing.T) {
42644278 // methods are called as expected.
42654279 require .Error (t , err , "expected send payment error" )
42664280 require .EqualValues (t , [32 ]byte {}, p , "preimage not match" )
4281+ require .GreaterOrEqual (t , getPaymentResultCnt , 1 )
4282+ require .Equal (t , getPaymentResultCnt , cntReportPaymentFail )
4283+ require .Equal (t , getPaymentResultCnt , cntFail )
42674284
42684285 controlTower .AssertExpectations (t )
42694286 payer .AssertExpectations (t )
0 commit comments