@@ -628,6 +628,30 @@ mod tests {
628
628
assert_eq ! ( * payer. attempts. borrow( ) , 2 ) ;
629
629
}
630
630
631
+ #[ test]
632
+ fn pays_invoice_on_partial_failure ( ) {
633
+ let event_handler = |_: & _ | { panic ! ( ) } ;
634
+
635
+ let payment_preimage = PaymentPreimage ( [ 1 ; 32 ] ) ;
636
+ let invoice = invoice ( payment_preimage) ;
637
+ let retry = TestRouter :: retry_for_invoice ( & invoice) ;
638
+ let final_value_msat = invoice. amount_milli_satoshis ( ) . unwrap ( ) ;
639
+
640
+ let payer = TestPayer :: new ( )
641
+ . fails_with_partial_failure ( retry. clone ( ) , OnAttempt ( 1 ) )
642
+ . fails_with_partial_failure ( retry, OnAttempt ( 2 ) )
643
+ . expect_send ( Amount :: ForInvoice ( final_value_msat) )
644
+ . expect_send ( Amount :: OnRetry ( final_value_msat / 2 ) )
645
+ . expect_send ( Amount :: OnRetry ( final_value_msat / 2 ) ) ;
646
+ let router = TestRouter { } ;
647
+ let scorer = RefCell :: new ( TestScorer :: new ( ) ) ;
648
+ let logger = TestLogger :: new ( ) ;
649
+ let invoice_payer =
650
+ InvoicePayer :: new ( & payer, router, & scorer, & logger, event_handler, RetryAttempts ( 2 ) ) ;
651
+
652
+ assert ! ( invoice_payer. pay_invoice( & invoice) . is_ok( ) ) ;
653
+ }
654
+
631
655
#[ test]
632
656
fn retries_payment_path_for_unknown_payment ( ) {
633
657
let event_handled = core:: cell:: RefCell :: new ( false ) ;
@@ -1233,7 +1257,7 @@ mod tests {
1233
1257
struct TestPayer {
1234
1258
expectations : core:: cell:: RefCell < VecDeque < Amount > > ,
1235
1259
attempts : core:: cell:: RefCell < usize > ,
1236
- failing_on_attempt : Option < usize > ,
1260
+ failing_on_attempt : core :: cell :: RefCell < HashMap < usize , PaymentSendFailure > > ,
1237
1261
}
1238
1262
1239
1263
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -1243,12 +1267,14 @@ mod tests {
1243
1267
OnRetry ( u64 ) ,
1244
1268
}
1245
1269
1270
+ struct OnAttempt ( usize ) ;
1271
+
1246
1272
impl TestPayer {
1247
1273
fn new ( ) -> Self {
1248
1274
Self {
1249
1275
expectations : core:: cell:: RefCell :: new ( VecDeque :: new ( ) ) ,
1250
1276
attempts : core:: cell:: RefCell :: new ( 0 ) ,
1251
- failing_on_attempt : None ,
1277
+ failing_on_attempt : core :: cell :: RefCell :: new ( HashMap :: new ( ) ) ,
1252
1278
}
1253
1279
}
1254
1280
@@ -1258,27 +1284,39 @@ mod tests {
1258
1284
}
1259
1285
1260
1286
fn fails_on_attempt ( self , attempt : usize ) -> Self {
1261
- Self {
1262
- expectations : core:: cell:: RefCell :: new ( self . expectations . borrow ( ) . clone ( ) ) ,
1263
- attempts : core:: cell:: RefCell :: new ( 0 ) ,
1264
- failing_on_attempt : Some ( attempt) ,
1265
- }
1287
+ let failure = PaymentSendFailure :: ParameterError ( APIError :: MonitorUpdateFailed ) ;
1288
+ self . fails_with ( failure, OnAttempt ( attempt) )
1289
+ }
1290
+
1291
+ fn fails_with_partial_failure ( self , retry : RouteParameters , attempt : OnAttempt ) -> Self {
1292
+ self . fails_with ( PaymentSendFailure :: PartialFailure {
1293
+ results : vec ! [ ] ,
1294
+ failed_paths_retry : Some ( retry) ,
1295
+ payment_id : PaymentId ( [ 1 ; 32 ] ) ,
1296
+ } , attempt)
1297
+ }
1298
+
1299
+ fn fails_with ( self , failure : PaymentSendFailure , attempt : OnAttempt ) -> Self {
1300
+ self . failing_on_attempt . borrow_mut ( ) . insert ( attempt. 0 , failure) ;
1301
+ self
1266
1302
}
1267
1303
1268
1304
fn check_attempts ( & self ) -> Result < PaymentId , PaymentSendFailure > {
1269
1305
let mut attempts = self . attempts . borrow_mut ( ) ;
1270
1306
* attempts += 1 ;
1271
- match self . failing_on_attempt {
1307
+
1308
+ match self . failing_on_attempt . borrow_mut ( ) . remove ( & * attempts) {
1309
+ Some ( failure) => Err ( failure) ,
1272
1310
None => Ok ( PaymentId ( [ 1 ; 32 ] ) ) ,
1273
- Some ( attempt) if attempt != * attempts => Ok ( PaymentId ( [ 1 ; 32 ] ) ) ,
1274
- Some ( _) => Err ( PaymentSendFailure :: ParameterError ( APIError :: MonitorUpdateFailed ) ) ,
1275
1311
}
1276
1312
}
1277
1313
1278
1314
fn check_value_msats ( & self , actual_value_msats : Amount ) {
1279
1315
let expected_value_msats = self . expectations . borrow_mut ( ) . pop_front ( ) ;
1280
1316
if let Some ( expected_value_msats) = expected_value_msats {
1281
1317
assert_eq ! ( actual_value_msats, expected_value_msats) ;
1318
+ } else {
1319
+ panic ! ( "Unexpected amount: {:?}" , actual_value_msats) ;
1282
1320
}
1283
1321
}
1284
1322
}
0 commit comments