@@ -4187,37 +4187,46 @@ func TestSendMPPaymentFailedWithShardsInFlight(t *testing.T) {
4187
4187
// Create a buffered chan and it will be returned by GetPaymentResult.
4188
4188
payer .resultChan = make (chan * htlcswitch.PaymentResult , 10 )
4189
4189
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
4194
4198
payer .On ("GetPaymentResult" ,
4195
4199
mock .Anything , identifier , mock .Anything ,
4196
4200
).Run (func (args mock.Arguments ) {
4197
4201
// Before the mock method is returned, we send the result to
4198
4202
// the read-only chan.
4199
4203
4200
4204
// Update the counter.
4201
- failAttemptCount ++
4205
+ getPaymentResultCnt ++
4202
4206
4203
4207
// We fail the first attempt with terminal error.
4204
- if failAttemptCount == 1 {
4208
+ if getPaymentResultCnt == 1 {
4205
4209
payer .resultChan <- & htlcswitch.PaymentResult {
4206
4210
Error : htlcswitch .NewForwardingError (
4207
4211
& lnwire.FailIncorrectDetails {},
4208
4212
1 ,
4209
4213
),
4210
4214
}
4211
4215
return
4212
-
4213
4216
}
4214
4217
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
+ }
4218
4227
})
4219
4228
4220
- // Mock the FailAttempt method to fail EXACTLY once.
4229
+ // Mock the FailAttempt method to fail (at least once) .
4221
4230
var failedAttempt channeldb.HTLCAttempt
4222
4231
controlTower .On ("FailAttempt" ,
4223
4232
identifier , mock .Anything , mock .Anything ,
@@ -4227,22 +4236,27 @@ func TestSendMPPaymentFailedWithShardsInFlight(t *testing.T) {
4227
4236
failedAttempt = payment .HTLCs [0 ]
4228
4237
failedAttempt .Failure = & channeldb.HTLCFailInfo {}
4229
4238
payment .HTLCs [0 ] = failedAttempt
4230
- }). Once ()
4239
+ })
4231
4240
4232
4241
// Setup ReportPaymentFail to return nil reason and error so the
4233
4242
// payment won't fail.
4234
4243
failureReason := channeldb .FailureReasonPaymentDetails
4244
+ cntReportPaymentFail := 0
4235
4245
missionControl .On ("ReportPaymentFail" ,
4236
4246
mock .Anything , mock .Anything , mock .Anything , mock .Anything ,
4237
4247
).Return (& failureReason , nil ).Run (func (args mock.Arguments ) {
4238
4248
payment .FailureReason = & failureReason
4239
- }).Once ()
4249
+ cntReportPaymentFail ++
4250
+ })
4240
4251
4241
4252
// Simple mocking the rest.
4242
- controlTower .On ("Fail" , identifier , failureReason ).Return (nil ).Once ()
4253
+ cntFail := 0
4254
+ controlTower .On ("Fail" , identifier , failureReason ).Return (nil )
4243
4255
payer .On ("SendHTLC" ,
4244
4256
mock .Anything , mock .Anything , mock .Anything ,
4245
- ).Return (nil )
4257
+ ).Return (nil ).Run (func (args mock.Arguments ) {
4258
+ cntFail ++
4259
+ })
4246
4260
4247
4261
// Call the actual method SendPayment on router. This is place inside a
4248
4262
// goroutine so we can set a timeout for the whole test, in case
@@ -4264,6 +4278,9 @@ func TestSendMPPaymentFailedWithShardsInFlight(t *testing.T) {
4264
4278
// methods are called as expected.
4265
4279
require .Error (t , err , "expected send payment error" )
4266
4280
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 )
4267
4284
4268
4285
controlTower .AssertExpectations (t )
4269
4286
payer .AssertExpectations (t )
0 commit comments