diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 1ebd5b25bf..278caa461c 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -620,6 +620,18 @@ async fn settle_force_close_balance( peer_node.sync_wallets().unwrap(); } + // The sweeper only tracks the output once the `SpendableOutputs` event was handled in the + // background, and only broadcasts the sweep on a subsequent sync. + exponential_backoff_poll(|| { + node.sync_wallets().unwrap(); + let pending_balances = node.list_balances().pending_balances_from_channel_closures; + let is_broadcast = pending_balances + .iter() + .any(|balance| !matches!(balance, PendingSweepBalance::PendingBroadcast { .. })); + is_broadcast.then_some(()) + }) + .await; + let balances = node.list_balances(); assert!(balances.lightning_balances.is_empty(), "Unexpected balance state: {:?}", balances); assert_eq!(balances.pending_balances_from_channel_closures.len(), 1); diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs index 8ce5bdb236..3b2e7dfffc 100644 --- a/tests/integration_tests_rust.rs +++ b/tests/integration_tests_rust.rs @@ -2943,8 +2943,11 @@ async fn do_connection_restart_behavior(persist: bool) { assert_eq!(peer_details_a.is_persisted, persist); assert!(peer_details_a.is_connected); - // Sleep a bit to allow for the connection to happen. - tokio::time::sleep(std::time::Duration::from_secs(5)).await; + // Wait for node B to register the inbound connection. + common::exponential_backoff_poll(|| { + node_b.list_peers().iter().any(|p| p.is_connected).then_some(()) + }) + .await; let peer_details_b = node_b.list_peers().first().unwrap().clone(); assert_eq!(peer_details_b.node_id, node_id_a); @@ -2957,10 +2960,14 @@ async fn do_connection_restart_behavior(persist: bool) { node_b.start().unwrap(); node_a.start().unwrap(); - // Sleep a bit to allow for the reconnect to happen. - tokio::time::sleep(std::time::Duration::from_secs(5)).await; - if persist { + // Wait for the reconnect to happen. + common::exponential_backoff_poll(|| { + let is_connected = |node: &TestNode| node.list_peers().iter().any(|p| p.is_connected); + (is_connected(&node_a) && is_connected(&node_b)).then_some(()) + }) + .await; + let peer_details_a = node_a.list_peers().first().unwrap().clone(); assert_eq!(peer_details_a.node_id, node_id_b); assert_eq!(peer_details_a.is_persisted, persist); @@ -2969,8 +2976,11 @@ async fn do_connection_restart_behavior(persist: bool) { let peer_details_b = node_b.list_peers().first().unwrap().clone(); assert_eq!(peer_details_b.node_id, node_id_a); assert_eq!(peer_details_b.is_persisted, false); - assert!(peer_details_a.is_connected); + assert!(peer_details_b.is_connected); } else { + // Sleep a bit so that an unexpected reconnect would have had time to happen. + tokio::time::sleep(std::time::Duration::from_secs(5)).await; + assert!(node_a.list_peers().is_empty()); assert!(node_b.list_peers().is_empty()); }