#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rate_limiter() {
let limiter = RateLimiter::new(10, 5);
assert!(limiter.try_acquire(5));
assert!(limiter.try_acquire(5));
assert!(!limiter.try_acquire(1));
std::thread::sleep(Duration::from_millis(201));
assert!(limiter.try_acquire(1)); }
#[actix_rt::test]
async fn test_backpressure_controller() {
let controller = BackpressureController::new(3);
let permit1 = controller.try_acquire_permit().unwrap();
let _permit2 = controller.try_acquire_permit().unwrap();
let _permit3 = controller.try_acquire_permit().unwrap();
assert!(matches!(
controller.try_acquire_permit(),
Err(BackpressureError::QueueFull)
));
drop(permit1);
let _permit4 = controller.try_acquire_permit().unwrap();
}
#[actix_rt::test]
async fn test_adaptive_rate_controller() {
let controller = AdaptiveRateController::new(100, 10, 1000);
assert_eq!(controller.get_current_rate(), 100);
controller.adapt_rate().await;
let new_rate = controller.get_current_rate();
assert!((10..=1000).contains(&new_rate));
}
#[actix_rt::test]
async fn test_bulkhead() {
let bulkhead = Bulkhead::new("test".to_string(), 2);
let handle1 = tokio::spawn({
let bulkhead = bulkhead.clone();
async move {
bulkhead
.execute(async {
tokio::time::sleep(Duration::from_millis(100)).await;
1
})
.await
}
});
let handle2 = tokio::spawn({
let bulkhead = bulkhead.clone();
async move {
bulkhead
.execute(async {
tokio::time::sleep(Duration::from_millis(100)).await;
2
})
.await
}
});
tokio::time::sleep(Duration::from_millis(10)).await;
let result = bulkhead.execute(async { 3 }).await;
assert!(matches!(result, Err(BackpressureError::QueueFull)));
let _r1 = handle1.await.unwrap();
let _r2 = handle2.await.unwrap();
}
}