]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Test for removing stale AwaitingInvoice payments
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 31 Aug 2023 01:22:18 +0000 (20:22 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 7 Sep 2023 21:45:11 +0000 (16:45 -0500)
lightning/src/ln/outbound_payment.rs

index aa92043cf9124ba5c62778ebea0f4bd6dd9127e2..f8989366509b56a482783fbf41af82ae9c67d681 100644 (file)
@@ -1585,7 +1585,7 @@ mod tests {
        use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
        use crate::ln::features::{ChannelFeatures, NodeFeatures};
        use crate::ln::msgs::{ErrorAction, LightningError};
-       use crate::ln::outbound_payment::{OutboundPayments, Retry, RetryableSendFailure};
+       use crate::ln::outbound_payment::{INVOICE_REQUEST_TIMEOUT_TICKS, OutboundPayments, Retry, RetryableSendFailure};
        use crate::routing::gossip::NetworkGraph;
        use crate::routing::router::{InFlightHtlcs, Path, PaymentParameters, Route, RouteHop, RouteParameters};
        use crate::sync::{Arc, Mutex, RwLock};
@@ -1782,4 +1782,35 @@ mod tests {
                } else { panic!("Unexpected event"); }
                if let Event::PaymentFailed { .. } = events[1].0 { } else { panic!("Unexpected event"); }
        }
+
+       #[test]
+       fn removes_stale_awaiting_invoice() {
+               let pending_events = Mutex::new(VecDeque::new());
+               let outbound_payments = OutboundPayments::new();
+               let payment_id = PaymentId([0; 32]);
+
+               assert!(!outbound_payments.has_pending_payments());
+               assert!(outbound_payments.add_new_awaiting_invoice(payment_id).is_ok());
+               assert!(outbound_payments.has_pending_payments());
+
+               for _ in 0..INVOICE_REQUEST_TIMEOUT_TICKS {
+                       outbound_payments.remove_stale_payments(&pending_events);
+                       assert!(outbound_payments.has_pending_payments());
+                       assert!(pending_events.lock().unwrap().is_empty());
+               }
+
+               outbound_payments.remove_stale_payments(&pending_events);
+               assert!(!outbound_payments.has_pending_payments());
+               assert!(!pending_events.lock().unwrap().is_empty());
+               assert_eq!(
+                       pending_events.lock().unwrap().pop_front(),
+                       Some((Event::InvoiceRequestFailed { payment_id }, None)),
+               );
+               assert!(pending_events.lock().unwrap().is_empty());
+
+               assert!(outbound_payments.add_new_awaiting_invoice(payment_id).is_ok());
+               assert!(outbound_payments.has_pending_payments());
+
+               assert!(outbound_payments.add_new_awaiting_invoice(payment_id).is_err());
+       }
 }