Skip to content

Commit e8e05fd

Browse files
committed
Add PartialFailure test cases
1 parent c389dce commit e8e05fd

File tree

1 file changed

+104
-3
lines changed

1 file changed

+104
-3
lines changed

lightning-invoice/src/payment.rs

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ mod tests {
810810
use std::time::{SystemTime, Duration};
811811
use time_utils::tests::SinceEpoch;
812812
use DEFAULT_EXPIRY_TIME;
813-
use lightning::util::errors::APIError::{ChannelUnavailable, MonitorUpdateFailed};
813+
use lightning::util::errors::APIError::{APIMisuseError, ChannelUnavailable, FeeRateTooHigh, MonitorUpdateFailed, RouteError};
814814

815815
fn invoice(payment_preimage: PaymentPreimage) -> Invoice {
816816
let payment_hash = Sha256::hash(&payment_preimage.0);
@@ -958,8 +958,8 @@ mod tests {
958958
let final_value_msat = invoice.amount_milli_satoshis().unwrap();
959959

960960
let payer = TestPayer::new()
961-
.fails_with_partial_failure(retry.clone(), OnAttempt(1))
962-
.fails_with_partial_failure(retry, OnAttempt(2))
961+
.fails_with_partial_failure(retry.clone(), OnAttempt(1), None)
962+
.fails_with_partial_failure(retry, OnAttempt(2), None)
963963
.expect_send(Amount::ForInvoice(final_value_msat))
964964
.expect_send(Amount::OnRetry(final_value_msat / 2))
965965
.expect_send(Amount::OnRetry(final_value_msat / 2));
@@ -1641,6 +1641,107 @@ mod tests {
16411641
invoice_payer.pay_invoice(&payment_invoice_2).unwrap();
16421642
}
16431643

1644+
// This following partial failure tests are variants of `inflight_map_data_trivial_test` above,
1645+
// except with partial failure errors.
1646+
#[test]
1647+
fn test_partial_failure_inflight_map() {
1648+
let event_handled = core::cell::RefCell::new(false);
1649+
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1650+
1651+
let payment_preimage = PaymentPreimage([1; 32]);
1652+
let invoice_to_pay = invoice(payment_preimage);
1653+
let payment_hash = Some(PaymentHash(invoice_to_pay.payment_hash().clone().into_inner()));
1654+
let final_value_msat = invoice_to_pay.amount_milli_satoshis().unwrap();
1655+
1656+
let retry = TestRouter::retry_for_invoice(&invoice_to_pay);
1657+
let payer = TestPayer::new()
1658+
.fails_with_partial_failure(retry.clone(), OnAttempt(1),
1659+
Some(vec![
1660+
Err(ChannelUnavailable { err: "abc".to_string() }),
1661+
Err(MonitorUpdateFailed)
1662+
]))
1663+
.expect_send(Amount::ForInvoice(final_value_msat));
1664+
1665+
let final_value_msat = invoice_to_pay.amount_milli_satoshis().unwrap();
1666+
let route = TestRouter::route_for_value(final_value_msat);
1667+
let router = TestRouter {};
1668+
let scorer = RefCell::new(TestScorer::new());
1669+
let logger = TestLogger::new();
1670+
let invoice_payer =
1671+
InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, Retry::Attempts(0));
1672+
1673+
invoice_payer.pay_invoice(&invoice_to_pay).unwrap();
1674+
let inflight_map = invoice_payer.create_inflight_map();
1675+
1676+
// Only the second path, that failed with `MonitorUpdateFailed` should be added to our
1677+
// inflight map.
1678+
assert_eq!(inflight_map.len(), 1);
1679+
}
1680+
1681+
#[test]
1682+
fn test_partial_failure_inflight_map_2() {
1683+
let event_handled = core::cell::RefCell::new(false);
1684+
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1685+
1686+
let payment_preimage = PaymentPreimage([1; 32]);
1687+
let invoice_to_pay = invoice(payment_preimage);
1688+
let payment_hash = Some(PaymentHash(invoice_to_pay.payment_hash().clone().into_inner()));
1689+
let final_value_msat = invoice_to_pay.amount_milli_satoshis().unwrap();
1690+
1691+
let retry = TestRouter::retry_for_invoice(&invoice_to_pay);
1692+
let payer = TestPayer::new()
1693+
.fails_with_partial_failure(retry.clone(), OnAttempt(1),
1694+
Some(vec![
1695+
Err(ChannelUnavailable { err: "abc".to_string() }),
1696+
Err(RouteError { err: "abc" })
1697+
]))
1698+
.expect_send(Amount::ForInvoice(final_value_msat));
1699+
1700+
let final_value_msat = invoice_to_pay.amount_milli_satoshis().unwrap();
1701+
let route = TestRouter::route_for_value(final_value_msat);
1702+
let router = TestRouter {};
1703+
let scorer = RefCell::new(TestScorer::new());
1704+
let logger = TestLogger::new();
1705+
let invoice_payer =
1706+
InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, Retry::Attempts(0));
1707+
1708+
invoice_payer.pay_invoice(&invoice_to_pay).unwrap();
1709+
let inflight_map = invoice_payer.create_inflight_map();
1710+
assert_eq!(inflight_map.len(), 0);
1711+
}
1712+
1713+
#[test]
1714+
fn test_partial_failure_inflight_map_3() {
1715+
let event_handled = core::cell::RefCell::new(false);
1716+
let event_handler = |_: &_| { *event_handled.borrow_mut() = true; };
1717+
1718+
let payment_preimage = PaymentPreimage([1; 32]);
1719+
let invoice_to_pay = invoice(payment_preimage);
1720+
let payment_hash = Some(PaymentHash(invoice_to_pay.payment_hash().clone().into_inner()));
1721+
let final_value_msat = invoice_to_pay.amount_milli_satoshis().unwrap();
1722+
1723+
let retry = TestRouter::retry_for_invoice(&invoice_to_pay);
1724+
let payer = TestPayer::new()
1725+
.fails_with_partial_failure(retry.clone(), OnAttempt(1),
1726+
Some(vec![
1727+
Err(FeeRateTooHigh { err: "abc".to_string(), feerate: 0 }),
1728+
Err(APIMisuseError { err: "abc".to_string() })
1729+
]))
1730+
.expect_send(Amount::ForInvoice(final_value_msat));
1731+
1732+
let final_value_msat = invoice_to_pay.amount_milli_satoshis().unwrap();
1733+
let route = TestRouter::route_for_value(final_value_msat);
1734+
let router = TestRouter {};
1735+
let scorer = RefCell::new(TestScorer::new());
1736+
let logger = TestLogger::new();
1737+
let invoice_payer =
1738+
InvoicePayer::new(&payer, router, &scorer, &logger, event_handler, Retry::Attempts(0));
1739+
1740+
invoice_payer.pay_invoice(&invoice_to_pay).unwrap();
1741+
let inflight_map = invoice_payer.create_inflight_map();
1742+
assert_eq!(inflight_map.len(), 0);
1743+
}
1744+
16441745
struct TestRouter;
16451746

16461747
impl TestRouter {

0 commit comments

Comments
 (0)