upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2026-01-09 15:32:02 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2026-01-09 15:32:02 +0000
commit29be4cb7d0fbd29325c995a76ba1b1f47beecca5 (patch)
tree3f31325217e60d9b207753e70457648758249008
parent208ea60836cfc98857cf3359a73d8874ed5d935a (diff)
Fix sync tests after Syncing status introduction
- Fix relay_connected() helper to check v >= 2 (Syncing/Connected states) - Fix unit test to use status value 3 (Connected) instead of 1 (Connecting) - Fix clippy warning: use .to_vec() instead of .iter().cloned().collect() All 61 sync integration tests now passing. All 238 unit tests passing. Clippy clean.
-rw-r--r--src/purgatory/sync/context.rs3
-rw-r--r--src/sync/algorithms.rs12
-rw-r--r--src/sync/mod.rs47
-rw-r--r--tests/common/sync_helpers.rs4
4 files changed, 51 insertions, 15 deletions
diff --git a/src/purgatory/sync/context.rs b/src/purgatory/sync/context.rs
index 1c2d7f2..4d34c7c 100644
--- a/src/purgatory/sync/context.rs
+++ b/src/purgatory/sync/context.rs
@@ -648,10 +648,9 @@ pub mod mock {
648 )]; 648 )];
649 649
650 // Create a single clone tag with multiple values (NIP-34 format) 650 // Create a single clone tag with multiple values (NIP-34 format)
651 let clone_values: Vec<String> = self.clone_urls.iter().cloned().collect();
652 tags.push(nostr_sdk::Tag::custom( 651 tags.push(nostr_sdk::Tag::custom(
653 nostr_sdk::TagKind::Custom("clone".into()), 652 nostr_sdk::TagKind::Custom("clone".into()),
654 clone_values, 653 self.clone_urls.to_vec(),
655 )); 654 ));
656 655
657 let event = EventBuilder::new(Kind::from(30617), "") 656 let event = EventBuilder::new(Kind::from(30617), "")
diff --git a/src/sync/algorithms.rs b/src/sync/algorithms.rs
index 7536f41..39788bc 100644
--- a/src/sync/algorithms.rs
+++ b/src/sync/algorithms.rs
@@ -349,6 +349,9 @@ mod tests {
349 last_connected: None, 349 last_connected: None,
350 disconnected_at: None, 350 disconnected_at: None,
351 announcements_synced: false, 351 announcements_synced: false,
352 historic_sync_completed: false,
353 historic_sync_completed_at: None,
354 historic_sync_had_failures: false,
352 }, 355 },
353 ); 356 );
354 357
@@ -442,6 +445,9 @@ mod tests {
442 last_connected: None, 445 last_connected: None,
443 disconnected_at: None, 446 disconnected_at: None,
444 announcements_synced: false, 447 announcements_synced: false,
448 historic_sync_completed: false,
449 historic_sync_completed_at: None,
450 historic_sync_had_failures: false,
445 }, 451 },
446 ); 452 );
447 453
@@ -476,6 +482,9 @@ mod tests {
476 last_connected: None, 482 last_connected: None,
477 disconnected_at: None, 483 disconnected_at: None,
478 announcements_synced: false, 484 announcements_synced: false,
485 historic_sync_completed: false,
486 historic_sync_completed_at: None,
487 historic_sync_had_failures: false,
479 }, 488 },
480 ); 489 );
481 490
@@ -537,6 +546,9 @@ mod tests {
537 last_connected: None, 546 last_connected: None,
538 disconnected_at: None, 547 disconnected_at: None,
539 announcements_synced: false, 548 announcements_synced: false,
549 historic_sync_completed: false,
550 historic_sync_completed_at: None,
551 historic_sync_had_failures: false,
540 }, 552 },
541 ); 553 );
542 554
diff --git a/src/sync/mod.rs b/src/sync/mod.rs
index 0e5b9bb..8b1da0e 100644
--- a/src/sync/mod.rs
+++ b/src/sync/mod.rs
@@ -908,8 +908,22 @@ impl SyncManager {
908 // Release lock before checking if historic sync is complete 908 // Release lock before checking if historic sync is complete
909 drop(relay_index); 909 drop(relay_index);
910 910
911 // Check if all historic sync is complete (no more pending batches) 911 // Spawn background task to check if historic sync is complete
912 self.check_and_complete_historic_sync(relay_url).await; 912 // This avoids blocking the confirm_batch flow for 6 seconds
913 let relay_url = relay_url.to_string();
914 let pending_index = self.pending_sync_index.clone();
915 let relay_index = self.relay_sync_index.clone();
916 let metrics = self.metrics.clone();
917
918 tokio::spawn(async move {
919 Self::check_and_complete_historic_sync_impl(
920 &relay_url,
921 pending_index,
922 relay_index,
923 metrics,
924 )
925 .await;
926 });
913 } 927 }
914 928
915 /// Check if historic sync is complete and transition to Connected status 929 /// Check if historic sync is complete and transition to Connected status
@@ -930,11 +944,17 @@ impl SyncManager {
930 /// - Buffer for processing: 1 second 944 /// - Buffer for processing: 1 second
931 /// 945 ///
932 /// Called after each batch is confirmed to detect completion. 946 /// Called after each batch is confirmed to detect completion.
933 async fn check_and_complete_historic_sync(&self, relay_url: &str) { 947 /// Spawned as a background task to avoid blocking the confirm_batch flow.
948 async fn check_and_complete_historic_sync_impl(
949 relay_url: &str,
950 pending_index: PendingSyncIndex,
951 relay_index: RelaySyncIndex,
952 metrics: Option<SyncMetrics>,
953 ) {
934 // First check: Are there any pending batches? 954 // First check: Are there any pending batches?
935 let has_pending = { 955 let has_pending = {
936 let pending = self.pending_sync_index.read().await; 956 let pending = pending_index.read().await;
937 pending.get(relay_url).map_or(false, |batches| !batches.is_empty()) 957 pending.get(relay_url).is_some_and(|batches| !batches.is_empty())
938 }; 958 };
939 959
940 if has_pending { 960 if has_pending {
@@ -948,8 +968,8 @@ impl SyncManager {
948 968
949 // Second check: Are there still no pending batches? 969 // Second check: Are there still no pending batches?
950 let has_pending = { 970 let has_pending = {
951 let pending = self.pending_sync_index.read().await; 971 let pending = pending_index.read().await;
952 pending.get(relay_url).map_or(false, |batches| !batches.is_empty()) 972 pending.get(relay_url).is_some_and(|batches| !batches.is_empty())
953 }; 973 };
954 974
955 if has_pending { 975 if has_pending {
@@ -958,8 +978,8 @@ impl SyncManager {
958 } 978 }
959 979
960 // No pending batches after waiting - safe to transition to Connected or ConnectedDegraded 980 // No pending batches after waiting - safe to transition to Connected or ConnectedDegraded
961 let mut relay_index = self.relay_sync_index.write().await; 981 let mut relay_index_guard = relay_index.write().await;
962 if let Some(state) = relay_index.get_mut(relay_url) { 982 if let Some(state) = relay_index_guard.get_mut(relay_url) {
963 if state.connection_status == ConnectionStatus::Syncing { 983 if state.connection_status == ConnectionStatus::Syncing {
964 // Check if any batches failed during historic sync 984 // Check if any batches failed during historic sync
965 let new_status = if state.historic_sync_had_failures { 985 let new_status = if state.historic_sync_had_failures {
@@ -983,7 +1003,7 @@ impl SyncManager {
983 ); 1003 );
984 1004
985 // Update metrics 1005 // Update metrics
986 if let Some(ref metrics) = self.metrics { 1006 if let Some(ref metrics) = metrics {
987 metrics.record_connection_status(relay_url, new_status); 1007 metrics.record_connection_status(relay_url, new_status);
988 } 1008 }
989 } 1009 }
@@ -1215,7 +1235,9 @@ impl SyncManager {
1215 ); 1235 );
1216 return; 1236 return;
1217 } 1237 }
1218 Some(status) if status.is_live_sync_active() => { 1238 Some(ConnectionStatus::Syncing)
1239 | Some(ConnectionStatus::Connected)
1240 | Some(ConnectionStatus::ConnectedHistoricSyncFailures) => {
1219 // Continue to subscribe - live sync is active, can accept new filters 1241 // Continue to subscribe - live sync is active, can accept new filters
1220 } 1242 }
1221 } 1243 }
@@ -1736,6 +1758,9 @@ impl SyncManager {
1736 repos: HashSet::new(), 1758 repos: HashSet::new(),
1737 root_events: HashSet::new(), 1759 root_events: HashSet::new(),
1738 announcements_synced: false, 1760 announcements_synced: false,
1761 historic_sync_completed: false,
1762 historic_sync_completed_at: None,
1763 historic_sync_had_failures: false,
1739 }; 1764 };
1740 index.insert(relay_url.clone(), new_state); 1765 index.insert(relay_url.clone(), new_state);
1741 true 1766 true
diff --git a/tests/common/sync_helpers.rs b/tests/common/sync_helpers.rs
index 27422e9..d6a6ee4 100644
--- a/tests/common/sync_helpers.rs
+++ b/tests/common/sync_helpers.rs
@@ -708,7 +708,7 @@ impl ParsedMetrics {
708 /// Check if a specific relay is connected 708 /// Check if a specific relay is connected
709 pub fn relay_connected(&self, relay: &str) -> Option<bool> { 709 pub fn relay_connected(&self, relay: &str) -> Option<bool> {
710 self.gauge("ngit_sync_relay_connected", &[("relay", relay)]) 710 self.gauge("ngit_sync_relay_connected", &[("relay", relay)])
711 .map(|v| v == 1) 711 .map(|v| v >= 2) // Syncing (2), Connected (3), or ConnectedHistoricSyncFailures (4)
712 } 712 }
713 713
714 /// Get total number of connected relays 714 /// Get total number of connected relays
@@ -1052,7 +1052,7 @@ mod tests {
1052 1052
1053 #[test] 1053 #[test]
1054 fn test_parse_metric_with_relay_url_label() { 1054 fn test_parse_metric_with_relay_url_label() {
1055 let text = r#"ngit_sync_relay_connected{relay="ws://127.0.0.1:12345"} 1"#; 1055 let text = r#"ngit_sync_relay_connected{relay="ws://127.0.0.1:12345"} 3"#;
1056 let metrics = ParsedMetrics::parse(text); 1056 let metrics = ParsedMetrics::parse(text);
1057 assert_eq!(metrics.relay_connected("ws://127.0.0.1:12345"), Some(true)); 1057 assert_eq!(metrics.relay_connected("ws://127.0.0.1:12345"), Some(true));
1058 } 1058 }