use std::time::Duration;
#[tokio::test]
async fn test_backfill_failure_threshold_detection() {
use datafold::fold_db_core::infrastructure::backfill_tracker::{
BackfillStatus, BackfillTracker,
};
let tracker = BackfillTracker::new(None);
let backfill_hash = BackfillTracker::generate_hash("TestTransform", "TestSource");
tracker.start_backfill_with_hash(
backfill_hash.clone(),
"TestTransform".to_string(),
"TestSource".to_string(),
).await;
println!("✅ Started backfill with hash: {}", backfill_hash);
tracker.set_mutations_expected(&backfill_hash, 100).await;
for i in 0..15 {
tracker.increment_mutation_failed(&backfill_hash, format!("Test error {}", i)).await;
}
for _ in 0..5 {
tracker.increment_mutation_completed(&backfill_hash).await;
}
tokio::time::sleep(Duration::from_millis(100)).await;
let backfills = tracker.get_all_backfills();
let backfill = backfills
.iter()
.find(|b| b.backfill_hash == backfill_hash)
.expect("Backfill should exist");
println!("📊 Backfill status after failures:");
println!(" - Status: {:?}", backfill.status);
println!(" - Mutations completed: {}", backfill.mutations_completed);
println!(" - Mutations failed: {}", backfill.mutations_failed);
println!(" - Error: {:?}", backfill.error);
assert_eq!(
backfill.status,
BackfillStatus::Failed,
"Backfill should be marked as Failed with 75% failure rate"
);
assert!(
backfill.error.is_some(),
"Failed backfill should have an error message"
);
println!("✅ Backfill failure threshold detection working correctly");
}
#[tokio::test]
async fn test_backfill_low_failure_rate_completes() {
use datafold::fold_db_core::infrastructure::backfill_tracker::{
BackfillStatus, BackfillTracker,
};
let tracker = BackfillTracker::new(None);
let backfill_hash = BackfillTracker::generate_hash("TestTransform", "TestSource");
tracker.start_backfill_with_hash(
backfill_hash.clone(),
"TestTransform".to_string(),
"TestSource".to_string(),
).await;
println!("✅ Started backfill with hash: {}", backfill_hash);
let expected_count = 100;
tracker.set_mutations_expected(&backfill_hash, expected_count).await;
for i in 0..5 {
tracker.increment_mutation_failed(&backfill_hash, format!("Test error {}", i)).await;
}
for _ in 0..expected_count {
tracker.increment_mutation_completed(&backfill_hash).await;
}
tokio::time::sleep(Duration::from_millis(100)).await;
let backfills = tracker.get_all_backfills();
let backfill = backfills
.iter()
.find(|b| b.backfill_hash == backfill_hash)
.expect("Backfill should exist");
println!("📊 Backfill status after low failure rate:");
println!(" - Status: {:?}", backfill.status);
println!(" - Mutations completed: {}", backfill.mutations_completed);
println!(" - Mutations failed: {}", backfill.mutations_failed);
assert_eq!(
backfill.status,
BackfillStatus::Completed,
"Backfill should be Completed when all expected mutations are processed"
);
assert!(
backfill.error.is_none(),
"Completed backfill should not have an error message"
);
println!("✅ Backfill with low failure rate completed successfully");
}