Skip to content

Commit 756b305

Browse files
committed
Test retrying payment on partial send failure
Add some test coverage for when InvoicePayer retries within pay_invoice because the Payer returned a partial failure.
1 parent ab95762 commit 756b305

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

lightning-invoice/src/payment.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,30 @@ mod tests {
628628
assert_eq!(*payer.attempts.borrow(), 2);
629629
}
630630

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+
631655
#[test]
632656
fn retries_payment_path_for_unknown_payment() {
633657
let event_handled = core::cell::RefCell::new(false);
@@ -1233,7 +1257,7 @@ mod tests {
12331257
struct TestPayer {
12341258
expectations: core::cell::RefCell<VecDeque<Amount>>,
12351259
attempts: core::cell::RefCell<usize>,
1236-
failing_on_attempt: Option<usize>,
1260+
failing_on_attempt: core::cell::RefCell<HashMap<usize, PaymentSendFailure>>,
12371261
}
12381262

12391263
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -1243,12 +1267,14 @@ mod tests {
12431267
OnRetry(u64),
12441268
}
12451269

1270+
struct OnAttempt(usize);
1271+
12461272
impl TestPayer {
12471273
fn new() -> Self {
12481274
Self {
12491275
expectations: core::cell::RefCell::new(VecDeque::new()),
12501276
attempts: core::cell::RefCell::new(0),
1251-
failing_on_attempt: None,
1277+
failing_on_attempt: core::cell::RefCell::new(HashMap::new()),
12521278
}
12531279
}
12541280

@@ -1258,27 +1284,39 @@ mod tests {
12581284
}
12591285

12601286
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
12661302
}
12671303

12681304
fn check_attempts(&self) -> Result<PaymentId, PaymentSendFailure> {
12691305
let mut attempts = self.attempts.borrow_mut();
12701306
*attempts += 1;
1271-
match self.failing_on_attempt {
1307+
1308+
match self.failing_on_attempt.borrow_mut().remove(&*attempts) {
1309+
Some(failure) => Err(failure),
12721310
None => Ok(PaymentId([1; 32])),
1273-
Some(attempt) if attempt != *attempts => Ok(PaymentId([1; 32])),
1274-
Some(_) => Err(PaymentSendFailure::ParameterError(APIError::MonitorUpdateFailed)),
12751311
}
12761312
}
12771313

12781314
fn check_value_msats(&self, actual_value_msats: Amount) {
12791315
let expected_value_msats = self.expectations.borrow_mut().pop_front();
12801316
if let Some(expected_value_msats) = expected_value_msats {
12811317
assert_eq!(actual_value_msats, expected_value_msats);
1318+
} else {
1319+
panic!("Unexpected amount: {:?}", actual_value_msats);
12821320
}
12831321
}
12841322
}

0 commit comments

Comments
 (0)