|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + cch::{LndConnectionInfo, LndTrackerActor, LndTrackerArgs, LndTrackerMessage}, |
| 5 | + fiber::types::Hash256, |
| 6 | +}; |
| 7 | + |
| 8 | +use ractor::{concurrency::Duration as RactorDuration, Actor, ActorRef, OutputPort}; |
| 9 | +use tokio_util::{sync::CancellationToken, task::TaskTracker}; |
| 10 | + |
| 11 | +// Helper function to create test arguments |
| 12 | +fn create_test_args() -> LndTrackerArgs { |
| 13 | + let port = Arc::new(OutputPort::default()); |
| 14 | + let tracker = TaskTracker::new(); |
| 15 | + let token = CancellationToken::new(); |
| 16 | + let lnd_connection = LndConnectionInfo { |
| 17 | + // Tracker will keep running because this URI is unreachable |
| 18 | + uri: "https://localhost:10009".parse().unwrap(), |
| 19 | + cert: None, |
| 20 | + macaroon: None, |
| 21 | + }; |
| 22 | + |
| 23 | + LndTrackerArgs { |
| 24 | + port, |
| 25 | + lnd_connection, |
| 26 | + token, |
| 27 | + tracker, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Helper function to create a test payment hash |
| 32 | +fn test_payment_hash(value: u8) -> Hash256 { |
| 33 | + let mut bytes = [0u8; 32]; |
| 34 | + bytes[0] = value; |
| 35 | + Hash256::from(bytes) |
| 36 | +} |
| 37 | + |
| 38 | +// Helper function to create a test `LndTrackerActor` (without spawning trackers) |
| 39 | +async fn create_test_actor() -> (ActorRef<LndTrackerMessage>, tokio::task::JoinHandle<()>) { |
| 40 | + // Use spawn instead of spawn_linked to avoid needing a root actor |
| 41 | + let args = create_test_args(); |
| 42 | + let (actor_ref, actor_handle) = |
| 43 | + Actor::spawn(Some("test_lnd_tracker".to_string()), LndTrackerActor, args) |
| 44 | + .await |
| 45 | + .expect("Failed to spawn test actor"); |
| 46 | + |
| 47 | + (actor_ref, actor_handle) |
| 48 | +} |
| 49 | + |
| 50 | +// Test completion decrements active_invoice_trackers counter |
| 51 | +#[tokio::test] |
| 52 | +async fn test_completion_decrements_counter() { |
| 53 | + let (actor_ref, _handle) = create_test_actor().await; |
| 54 | + let payment_hash = test_payment_hash(1); |
| 55 | + |
| 56 | + // Add invoice to queue (without processing to avoid LND calls) |
| 57 | + actor_ref |
| 58 | + .cast(LndTrackerMessage::TrackInvoice(payment_hash)) |
| 59 | + .expect("Failed to send TrackInvoice"); |
| 60 | + tokio::time::sleep(tokio::time::Duration::from_millis(50)).await; |
| 61 | + |
| 62 | + // Send completion message (simulating a tracker that finished) |
| 63 | + actor_ref |
| 64 | + .cast(LndTrackerMessage::InvoiceTrackerCompleted { |
| 65 | + payment_hash, |
| 66 | + completed_successfully: true, |
| 67 | + }) |
| 68 | + .expect("Failed to send completion"); |
| 69 | + |
| 70 | + tokio::time::sleep(tokio::time::Duration::from_millis(50)).await; |
| 71 | + |
| 72 | + // Verify counter behavior (should handle completion gracefully) |
| 73 | + let final_state = actor_ref |
| 74 | + .call( |
| 75 | + |reply_port| LndTrackerMessage::GetState(reply_port), |
| 76 | + Some(RactorDuration::from_millis(1000)), |
| 77 | + ) |
| 78 | + .await |
| 79 | + .expect("Actor should be responsive after completion"); |
| 80 | + |
| 81 | + assert!(final_state.is_success()); |
| 82 | + let final_state = final_state.unwrap(); |
| 83 | + assert_eq!(final_state.invoice_queue_len, 0); |
| 84 | + assert_eq!(final_state.active_invoice_trackers, 0); |
| 85 | +} |
| 86 | + |
| 87 | +// Test completion triggers queue processing for waiting invoices |
| 88 | +#[tokio::test] |
| 89 | +async fn test_completion_triggers_queue_processing() { |
| 90 | + let (actor_ref, _handle) = create_test_actor().await; |
| 91 | + |
| 92 | + // Add 6 invoices to queue |
| 93 | + for i in 0..6 { |
| 94 | + let payment_hash = test_payment_hash(i); |
| 95 | + actor_ref |
| 96 | + .cast(LndTrackerMessage::TrackInvoice(payment_hash)) |
| 97 | + .expect("Failed to send TrackInvoice"); |
| 98 | + } |
| 99 | + |
| 100 | + tokio::time::sleep(tokio::time::Duration::from_millis(50)).await; |
| 101 | + |
| 102 | + // Verify invoices are queued |
| 103 | + let state_before = actor_ref |
| 104 | + .call( |
| 105 | + |reply_port| LndTrackerMessage::GetState(reply_port), |
| 106 | + Some(RactorDuration::from_millis(1000)), |
| 107 | + ) |
| 108 | + .await |
| 109 | + .expect("Failed to get state") |
| 110 | + .expect("Failed to get state"); |
| 111 | + |
| 112 | + assert_eq!( |
| 113 | + state_before.invoice_queue_len, 1, |
| 114 | + "Should have 1 invoice in queue" |
| 115 | + ); |
| 116 | + assert_eq!( |
| 117 | + state_before.active_invoice_trackers, 5, |
| 118 | + "Should have 5 active invoice trackers" |
| 119 | + ); |
| 120 | + |
| 121 | + let completed_hash = test_payment_hash(1); |
| 122 | + actor_ref |
| 123 | + .cast(LndTrackerMessage::InvoiceTrackerCompleted { |
| 124 | + payment_hash: completed_hash, |
| 125 | + completed_successfully: true, |
| 126 | + }) |
| 127 | + .expect("Failed to send completion"); |
| 128 | + |
| 129 | + tokio::time::sleep(tokio::time::Duration::from_millis(50)).await; |
| 130 | + |
| 131 | + // Verify actor is still responsive |
| 132 | + let state_after = actor_ref |
| 133 | + .call( |
| 134 | + |reply_port| LndTrackerMessage::GetState(reply_port), |
| 135 | + Some(RactorDuration::from_millis(1000)), |
| 136 | + ) |
| 137 | + .await |
| 138 | + .expect("Failed to get state") |
| 139 | + .expect("Failed to get state"); |
| 140 | + |
| 141 | + assert_eq!( |
| 142 | + state_after.invoice_queue_len, 0, |
| 143 | + "Should have 0 invoices in queue" |
| 144 | + ); |
| 145 | + assert_eq!( |
| 146 | + state_after.active_invoice_trackers, 5, |
| 147 | + "Should have 5 active invoice trackers" |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +// Test timeout re-queues active invoices to end of queue |
| 152 | +#[tokio::test] |
| 153 | +async fn test_timeout_requeues_active_invoices() { |
| 154 | + let (actor_ref, _handle) = create_test_actor().await; |
| 155 | + let payment_hash = test_payment_hash(1); |
| 156 | + |
| 157 | + // Add invoice to queue (without processing to avoid LND calls) |
| 158 | + actor_ref |
| 159 | + .cast(LndTrackerMessage::TrackInvoice(payment_hash)) |
| 160 | + .expect("Failed to send TrackInvoice"); |
| 161 | + tokio::time::sleep(tokio::time::Duration::from_millis(50)).await; |
| 162 | + |
| 163 | + // Send completion message (simulating a tracker that finished) |
| 164 | + actor_ref |
| 165 | + .cast(LndTrackerMessage::InvoiceTrackerCompleted { |
| 166 | + payment_hash, |
| 167 | + completed_successfully: false, |
| 168 | + }) |
| 169 | + .expect("Failed to send completion"); |
| 170 | + |
| 171 | + tokio::time::sleep(tokio::time::Duration::from_millis(50)).await; |
| 172 | + |
| 173 | + // Verify counter behavior (should handle completion gracefully) |
| 174 | + let final_state = actor_ref |
| 175 | + .call( |
| 176 | + |reply_port| LndTrackerMessage::GetState(reply_port), |
| 177 | + Some(RactorDuration::from_millis(1000)), |
| 178 | + ) |
| 179 | + .await |
| 180 | + .expect("Actor should be responsive after completion"); |
| 181 | + |
| 182 | + assert!(final_state.is_success()); |
| 183 | + let final_state = final_state.unwrap(); |
| 184 | + assert_eq!(final_state.invoice_queue_len, 0); |
| 185 | + assert_eq!(final_state.active_invoice_trackers, 1); |
| 186 | +} |
0 commit comments