use crate::rate_limit::{RateLimitConfig, MIN_BURST_FOR_ZONE_REPLAY};
#[test]
fn test_rate_limit_config_default() {
let config = RateLimitConfig::default();
assert_eq!(config.requests_per_period, 600);
assert_eq!(config.period_secs, 60);
assert_eq!(config.burst_size, MIN_BURST_FOR_ZONE_REPLAY);
assert!(config.enabled);
}
#[test]
fn test_rate_limit_config_validation() {
let config = RateLimitConfig::default();
assert!(config.validate().is_ok());
let invalid_config = RateLimitConfig {
requests_per_period: 0,
..Default::default()
};
assert!(invalid_config.validate().is_err());
let invalid_config = RateLimitConfig {
period_secs: 0,
..Default::default()
};
assert!(invalid_config.validate().is_err());
let invalid_config = RateLimitConfig {
burst_size: 0,
..Default::default()
};
assert!(invalid_config.validate().is_err());
}
#[test]
fn test_rate_limit_config_from_env() {
std::env::remove_var("RATE_LIMIT_ENABLED");
std::env::remove_var("RATE_LIMIT_REQUESTS");
std::env::remove_var("RATE_LIMIT_PERIOD_SECS");
std::env::remove_var("RATE_LIMIT_BURST");
let config = RateLimitConfig::from_env();
assert_eq!(config.requests_per_period, 600);
assert_eq!(config.period_secs, 60);
assert_eq!(config.burst_size, MIN_BURST_FOR_ZONE_REPLAY);
assert!(config.enabled);
}
#[test]
fn test_replenish_interval_does_not_floor_the_configured_rate() {
let config = RateLimitConfig {
requests_per_period: 100,
period_secs: 60,
..Default::default()
};
assert_eq!(config.replenish_period().as_millis(), 600);
}
#[test]
fn test_replenish_interval_for_sub_second_rates() {
let config = RateLimitConfig {
requests_per_period: 30,
period_secs: 60,
..Default::default()
};
assert_eq!(config.replenish_period().as_millis(), 2000);
}
#[test]
fn test_default_burst_absorbs_a_zone_replay() {
let config = RateLimitConfig::default();
assert!(
config.burst_size >= MIN_BURST_FOR_ZONE_REPLAY,
"default burst {} cannot absorb a zone replay ({} calls)",
config.burst_size,
MIN_BURST_FOR_ZONE_REPLAY
);
}