Files
rustytorch/crates/data/rtx-etl/tests/etl_metrics_test.rs
T
2026-03-04 00:08:42 +00:00

112 lines
3.1 KiB
Rust

//! TDD tests for EtlMetrics functionality
//! These tests define expected behavior for metrics recording
use rtx_etl::dag::TaskType;
use rtx_etl::monitoring::EtlMetrics;
use std::sync::Arc;
use std::time::Duration;
#[tokio::test]
async fn test_record_task_execution() {
// Test recording task execution metrics
let metrics = Arc::new(EtlMetrics::new());
// Should be able to record task execution
metrics
.record_task_execution(
"task1",
&TaskType::Extract,
Duration::from_millis(500),
true,
)
.await;
// Metrics should be updated
let stats = metrics.get_task_stats("task1").await;
assert!(stats.is_some());
let task_stats = stats.unwrap();
assert_eq!(task_stats.total_executions, 1);
assert_eq!(task_stats.successful_executions, 1);
assert_eq!(task_stats.failed_executions, 0);
}
#[tokio::test]
async fn test_record_batch_processed() {
// Test recording batch processing metrics
let metrics = EtlMetrics::new();
// Should be able to record batch processing
metrics
.record_batch_processed(100, Duration::from_millis(1000))
.await;
// Should be able to get batch metrics
let batch_stats = metrics.get_batch_stats().await;
assert_eq!(batch_stats.total_batches, 1);
assert_eq!(batch_stats.total_records, 100);
assert!(batch_stats.avg_processing_time.as_millis() > 0);
}
#[tokio::test]
async fn test_get_task_stats() {
// Test retrieving task statistics
let metrics = Arc::new(EtlMetrics::new());
// Record some executions
metrics
.record_task_execution(
"task1",
&TaskType::Transform,
Duration::from_millis(200),
true,
)
.await;
metrics
.record_task_execution(
"task1",
&TaskType::Transform,
Duration::from_millis(300),
false,
)
.await;
// Should get accurate stats
let stats = metrics.get_task_stats("task1").await.unwrap();
assert_eq!(stats.total_executions, 2);
assert_eq!(stats.successful_executions, 1);
assert_eq!(stats.failed_executions, 1);
assert!(stats.avg_execution_time.as_millis() > 0);
}
#[tokio::test]
async fn test_metrics_thread_safety() {
// Test that metrics can be safely accessed from multiple threads
let metrics = Arc::new(EtlMetrics::new());
let mut handles = vec![];
for i in 0..10 {
let metrics_clone = metrics.clone();
let handle = tokio::spawn(async move {
metrics_clone
.record_task_execution(
&format!("task{}", i),
&TaskType::Load,
Duration::from_millis(100),
true,
)
.await;
});
handles.push(handle);
}
// Wait for all tasks to complete
for handle in handles {
handle.await.unwrap();
}
// Should have recorded all executions
let total_stats = metrics.get_overall_stats().await;
assert_eq!(total_stats.total_tasks_executed, 10);
}