upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-12-11 09:48:24 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-12-11 09:48:24 +0000
commit532a7d0d5d8461bad0fc799aacb5eea0135f79f3 (patch)
treeef4956f91b99244cfe2dd2d1968bdb458e20e0ae /tests
parent5b9f55b95e56eccf4e3d67deb9ed8d97c7355baa (diff)
Phase 4: Add health state and multi-relay aggregate tests
Diffstat (limited to 'tests')
-rw-r--r--tests/sync/metrics.rs103
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/sync/metrics.rs b/tests/sync/metrics.rs
index 241af8f..82d681e 100644
--- a/tests/sync/metrics.rs
+++ b/tests/sync/metrics.rs
@@ -482,4 +482,107 @@ async fn test_relay_connected_status() {
482 ); 482 );
483 483
484 harness.stop_all().await; 484 harness.stop_all().await;
485}
486
487// ============================================================================
488// Phase 4: Health State and Multi-Relay Aggregate Tests
489// ============================================================================
490
491/// Test that health state degrades when a relay becomes unreachable.
492///
493/// This test validates that `ngit_sync_relay_status` gauge transitions from
494/// healthy (1) to degraded (2) or dead (3) when a relay cannot be connected to.
495///
496/// NOTE: This test may fail until sync metrics recording is fully wired up.
497/// The test documents the expected behavior.
498#[tokio::test]
499#[ignore] // Ignored until sync metrics are fully wired up
500async fn test_health_state_degrades_on_failure() {
501 use crate::common::sync_helpers::MetricsTestHarness;
502
503 let mut harness = MetricsTestHarness::with_sources(0).await;
504 harness.start_syncing_relay_to_nowhere().await;
505
506 // Initially might be trying to connect
507 tokio::time::sleep(Duration::from_secs(1)).await;
508 let initial = harness.get_metrics().await.unwrap();
509
510 // After several failures, should degrade (status = 2 or 3)
511 tokio::time::sleep(Duration::from_secs(5)).await;
512 let later = harness.get_metrics().await.unwrap();
513
514 // Get the relay status (1=healthy, 2=degraded, 3=dead)
515 let status = later.gauge("ngit_sync_relay_status", &[]).unwrap_or(0);
516
517 println!("Initial metrics: {:?}", initial.gauge("ngit_sync_relay_status", &[]));
518 println!("Later status: {}", status);
519
520 assert!(
521 status >= 2,
522 "Health should degrade to 2 (degraded) or 3 (dead), got {}",
523 status
524 );
525
526 harness.stop_all().await;
527}
528
529/// Test that aggregate relay counts are tracked correctly.
530///
531/// This test validates the aggregate metrics:
532/// - `ngit_sync_relays_tracked_total`
533/// - `ngit_sync_relays_connected_total`
534///
535/// Note: Current implementation may only support one sync source, so this tests
536/// with one source, verifying tracked=1 and connected=1, then connected=0 after stopping.
537///
538/// NOTE: This test may fail until sync metrics recording is fully wired up.
539/// The test documents the expected behavior.
540#[tokio::test]
541#[ignore] // Ignored until sync metrics are fully wired up
542async fn test_multi_source_aggregate_counts() {
543 use crate::common::sync_helpers::MetricsTestHarness;
544
545 // Note: Current impl only supports ONE sync source, so this tests
546 // that with one source, tracked=1 and connected=1
547 let mut harness = MetricsTestHarness::with_sources(1).await;
548 harness.start_syncing_relay(0).await;
549 tokio::time::sleep(Duration::from_secs(2)).await;
550
551 let metrics = harness.get_metrics().await.unwrap();
552
553 println!("Tracked total: {:?}", metrics.relays_tracked_total());
554 println!("Connected total: {:?}", metrics.relays_connected_total());
555
556 assert_eq!(
557 metrics.relays_tracked_total(),
558 Some(1),
559 "Should track 1 relay"
560 );
561 assert_eq!(
562 metrics.relays_connected_total(),
563 Some(1),
564 "Should have 1 connected"
565 );
566
567 // Stop source, verify connected drops to 0
568 harness.stop_source(0).await;
569 tokio::time::sleep(Duration::from_secs(2)).await;
570
571 let metrics = harness.get_metrics().await.unwrap();
572
573 println!("After stop - Tracked total: {:?}", metrics.relays_tracked_total());
574 println!("After stop - Connected total: {:?}", metrics.relays_connected_total());
575
576 assert_eq!(
577 metrics.relays_tracked_total(),
578 Some(1),
579 "Still tracking 1 relay"
580 );
581 assert_eq!(
582 metrics.relays_connected_total(),
583 Some(0),
584 "Should have 0 connected"
585 );
586
587 harness.stop_all().await;
485} \ No newline at end of file 588} \ No newline at end of file