use crate::http::{CircuitBreaker, CircuitBreakerConfig};
use std::time::{Duration, Instant};
fn cb(failure_threshold: u32, cooldown_secs: u64, success_threshold: u32) -> CircuitBreaker {
CircuitBreaker::new(CircuitBreakerConfig {
failure_threshold,
cooldown_secs,
success_threshold,
..CircuitBreakerConfig::default()
})
.expect("测试熔断器配置应合法")
}
#[test]
fn default_config_values() {
let c = CircuitBreakerConfig::default();
assert_eq!(c.failure_threshold, 5);
assert_eq!(c.cooldown_secs, 30);
assert_eq!(c.success_threshold, 1);
assert_eq!(c.max_tracked_hosts, 1024);
assert_eq!(c.host_idle_ttl_secs, 600);
}
#[test]
fn healthy_host_always_allowed() {
let b = cb(3, 10, 1);
assert!(b.allow("api.example.com"));
b.on_failure("api.example.com");
b.on_failure("api.example.com");
assert!(b.allow("api.example.com"));
}
#[test]
fn opens_after_threshold_consecutive_failures() {
let b = cb(3, 10, 1);
b.on_failure("h");
b.on_failure("h");
assert!(b.allow("h"), "2 次失败 < 阈值 3,仍放行");
b.on_failure("h"); assert!(!b.allow("h"), "达阈值后应熔断");
}
#[test]
fn success_resets_failure_count() {
let b = cb(3, 10, 1);
b.on_failure("h");
b.on_failure("h");
b.on_success("h"); b.on_failure("h");
b.on_failure("h");
assert!(b.allow("h"), "成功清零后再两次失败不应熔断");
}
#[test]
fn per_host_isolation() {
let b = cb(2, 10, 1);
b.on_failure("bad");
b.on_failure("bad"); assert!(!b.allow("bad"));
assert!(b.allow("good"), "好 host 不受坏 host 影响");
}
#[test]
fn open_rejects_until_cooldown_then_half_opens() {
let b = cb(1, 10, 1); let t0 = Instant::now();
b.on_failure_at("h", t0); assert!(!b.allow_at("h", t0 + Duration::from_secs(5)), "冷却内拒绝");
assert!(
b.allow_at("h", t0 + Duration::from_secs(10)),
"冷却后放行探测"
);
}
#[test]
fn half_open_success_closes() {
let b = cb(1, 10, 2); let t0 = Instant::now();
b.on_failure_at("h", t0);
assert!(b.allow_at("h", t0 + Duration::from_secs(10))); b.on_success("h"); assert!(b.allow("h"), "HalfOpen 期间继续放行");
b.on_success("h"); assert!(b.allow("h"));
}
#[test]
fn half_open_failure_reopens() {
let b = cb(1, 10, 1);
let t0 = Instant::now();
b.on_failure_at("h", t0);
assert!(b.allow_at("h", t0 + Duration::from_secs(10))); let t1 = t0 + Duration::from_secs(11);
b.on_failure_at("h", t1);
assert!(
!b.allow_at("h", t1 + Duration::from_secs(5)),
"重开后冷却内拒绝"
);
assert!(
b.allow_at("h", t1 + Duration::from_secs(10)),
"新冷却结束后再放行"
);
}
#[test]
fn threshold_one_opens_immediately() {
let b = cb(1, 5, 1);
let t0 = Instant::now();
b.on_failure_at("h", t0);
assert!(!b.allow_at("h", t0), "阈值 1 时单次失败立即熔断");
}
#[test]
fn shared_state_across_clones() {
let b = cb(2, 10, 1);
let b2 = b.clone();
b.on_failure("h");
b2.on_failure("h"); assert!(!b.allow("h"));
assert!(!b2.allow("h"));
}
#[test]
fn host_map_caps_tracked_hosts() {
let b = CircuitBreaker::new(CircuitBreakerConfig {
max_tracked_hosts: 4,
..CircuitBreakerConfig::default()
})
.expect("测试熔断器配置应合法");
let t0 = Instant::now();
for i in 0..64u64 {
b.on_failure_at(&format!("host-{i}"), t0 + Duration::from_secs(i));
}
assert!(
b.tracked_host_count() <= 4,
"状态 map 不应超过 max_tracked_hosts 上限,实际为 {}",
b.tracked_host_count()
);
}
#[test]
fn idle_host_entries_are_reclaimed() {
let b = CircuitBreaker::new(CircuitBreakerConfig {
failure_threshold: 1,
..CircuitBreakerConfig::default()
})
.expect("测试熔断器配置应合法");
let t0 = Instant::now();
b.on_failure_at("stale.example.com", t0); assert!(!b.allow_at("stale.example.com", t0), "打开后应拒绝");
b.on_failure_at("fresh.example.com", t0 + Duration::from_secs(601));
assert!(
b.allow("stale.example.com"),
"陈旧条目含已打开熔断被遗忘后应回到健康(可放行)"
);
}