#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use super::*;
use tokio::time::sleep;
#[tokio::test]
#[ignore = "Test hangs during compilation - tdg module conflicts"]
async fn test_resource_controller_creation() {
let controller = PlatformResourceController::new(ResourceLimits::default());
let usage = controller.get_current_usage().await;
assert_eq!(usage.active_operations, 0);
assert_eq!(usage.memory_pressure, ResourcePressure::Low);
}
#[tokio::test]
#[ignore = "Test hangs - needs investigation"]
async fn test_resource_allocation_success() {
let controller = PlatformResourceController::new(ResourceLimits::default());
controller.start_monitoring().await.unwrap();
let allocation = controller
.request_resources(
"test-op-1".to_string(),
OperationType::Analysis,
OperationPriority::High,
100.0,
)
.await
.unwrap();
let usage = controller.get_current_usage().await;
assert_eq!(usage.active_operations, 1);
drop(allocation);
sleep(Duration::from_millis(100)).await;
controller.stop_monitoring().await;
}
#[tokio::test]
#[ignore = "Stack overflow issue - needs investigation"]
async fn test_memory_limit_enforcement() {
let limits = ResourceLimits {
max_memory_mb: 200.0, ..Default::default()
};
let controller = PlatformResourceController::new(limits);
controller.start_monitoring().await.unwrap();
let result = controller
.request_resources(
"test-op-memory".to_string(),
OperationType::Analysis,
OperationPriority::Low,
300.0, )
.await;
assert!(result.is_err());
controller.stop_monitoring().await;
}
#[tokio::test]
async fn test_critical_priority_bypass() {
let limits = ResourceLimits {
max_memory_mb: 100.0, ..Default::default()
};
let controller = PlatformResourceController::new(limits);
controller.start_monitoring().await.unwrap();
let allocation = controller
.request_resources(
"critical-op".to_string(),
OperationType::Commit,
OperationPriority::Critical,
150.0, )
.await;
assert!(allocation.is_ok());
controller.stop_monitoring().await;
}
#[tokio::test]
#[ignore = "Test hangs - needs investigation"]
async fn test_operation_counting() {
let limits = ResourceLimits {
max_concurrent_ops: 2, ..Default::default()
};
let controller = PlatformResourceController::new(limits);
controller.start_monitoring().await.unwrap();
let _alloc1 = controller
.request_resources(
"op-1".to_string(),
OperationType::Analysis,
OperationPriority::High,
50.0,
)
.await
.unwrap();
let _alloc2 = controller
.request_resources(
"op-2".to_string(),
OperationType::Analysis,
OperationPriority::High,
50.0,
)
.await
.unwrap();
let result = controller
.request_resources(
"op-3".to_string(),
OperationType::Background,
OperationPriority::Low,
50.0,
)
.await;
assert!(result.is_err());
controller.stop_monitoring().await;
}
#[tokio::test]
async fn test_enforcement_stats() {
let controller = PlatformResourceController::new(ResourceLimits::default());
controller.start_monitoring().await.unwrap();
let _alloc = controller
.request_resources(
"stats-test".to_string(),
OperationType::Analysis,
OperationPriority::Medium,
100.0,
)
.await
.unwrap();
let stats = controller.get_enforcement_stats().await;
assert!(stats.total_requests > 0);
assert!(stats.allowed_requests > 0);
controller.stop_monitoring().await;
}
#[tokio::test]
async fn test_factory_patterns() {
let default_ctrl = ResourceControllerFactory::create_default();
let dev_ctrl = ResourceControllerFactory::create_dev_optimized();
let prod_ctrl = ResourceControllerFactory::create_prod_optimized();
let ci_ctrl = ResourceControllerFactory::create_ci_optimized();
default_ctrl.start_monitoring().await.unwrap();
dev_ctrl.start_monitoring().await.unwrap();
prod_ctrl.start_monitoring().await.unwrap();
ci_ctrl.start_monitoring().await.unwrap();
let _alloc1 = default_ctrl
.request_resources(
"factory-test-1".to_string(),
OperationType::Analysis,
OperationPriority::Medium,
50.0,
)
.await
.unwrap();
let _alloc2 = dev_ctrl
.request_resources(
"factory-test-2".to_string(),
OperationType::Analysis,
OperationPriority::Medium,
50.0,
)
.await
.unwrap();
default_ctrl.stop_monitoring().await;
dev_ctrl.stop_monitoring().await;
prod_ctrl.stop_monitoring().await;
ci_ctrl.stop_monitoring().await;
}
#[tokio::test]
#[ignore = "Test involves background monitoring task that may hang in CI"]
async fn test_resource_monitoring_lifecycle() {
let controller = PlatformResourceController::new(ResourceLimits::default());
controller.start_monitoring().await.unwrap();
controller.start_monitoring().await.unwrap();
controller.stop_monitoring().await;
controller.start_monitoring().await.unwrap();
controller.stop_monitoring().await;
}
#[tokio::test]
#[ignore = "Test involves background monitoring task that may hang in CI"]
async fn test_resource_pressure_levels() {
let limits = ResourceLimits {
max_memory_mb: 1000.0,
memory_warning_threshold: 0.7, ..Default::default()
};
let controller = PlatformResourceController::new(limits);
controller.start_monitoring().await.unwrap();
let _alloc1 = controller
.request_resources(
"pressure-low".to_string(),
OperationType::Analysis,
OperationPriority::Medium,
500.0, )
.await
.unwrap();
let usage1 = controller.get_current_usage().await;
assert_eq!(usage1.memory_pressure, ResourcePressure::Low);
let _alloc2 = controller
.request_resources(
"pressure-medium".to_string(),
OperationType::Analysis,
OperationPriority::Medium,
250.0, )
.await
.unwrap();
let usage2 = controller.get_current_usage().await;
assert_eq!(usage2.memory_pressure, ResourcePressure::Medium);
controller.stop_monitoring().await;
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod property_tests {
use proptest::prelude::*;
proptest! {
#[test]
fn basic_property_stability(_input in ".*") {
prop_assert!(true);
}
#[test]
fn module_consistency_check(_x in 0u32..1000) {
prop_assert!(_x < 1001);
}
}
}