use super::*;
fn thresholds() -> BackpressureThresholds {
BackpressureThresholds {
l0_slowdown: Some(8),
l0_stop: Some(16),
bytes_slowdown: Some(1_000),
bytes_stop: Some(4_000),
max_slowdown: Some(Duration::from_millis(10)),
}
}
#[test]
fn off_thresholds_are_always_none() {
let off = BackpressureThresholds::default();
assert!(off.is_off());
assert_eq!(
Backpressure::compute(1_000_000, u64::MAX, &off),
Backpressure::None
);
}
#[test]
fn below_all_thresholds_is_none() {
assert_eq!(
Backpressure::compute(0, 0, &thresholds()),
Backpressure::None
);
assert_eq!(
Backpressure::compute(7, 999, &thresholds()),
Backpressure::None
);
}
#[test]
fn l0_count_at_slowdown_threshold_yields_zero_delay_slowdown() {
let v = Backpressure::compute(8, 0, &thresholds());
assert_eq!(
v,
Backpressure::Slowdown {
suggested_delay: Duration::ZERO
}
);
}
#[test]
fn l0_count_at_stop_threshold_yields_stop() {
assert_eq!(
Backpressure::compute(16, 0, &thresholds()),
Backpressure::Stop
);
assert_eq!(
Backpressure::compute(100, 0, &thresholds()),
Backpressure::Stop
);
}
#[test]
fn bytes_axis_drives_the_verdict_independently() {
assert!(matches!(
Backpressure::compute(0, 2_000, &thresholds()),
Backpressure::Slowdown { .. }
));
assert_eq!(
Backpressure::compute(0, 4_000, &thresholds()),
Backpressure::Stop
);
}
#[test]
fn stop_dominates_slowdown_across_axes() {
assert_eq!(
Backpressure::compute(8, 4_000, &thresholds()),
Backpressure::Stop
);
}
#[test]
fn slowdown_delay_grows_monotonically_with_overage() {
let t = thresholds();
let mut last = Duration::ZERO;
for count in 8..16 {
let Backpressure::Slowdown { suggested_delay } = Backpressure::compute(count, 0, &t) else {
panic!("expected Slowdown at L0={count}");
};
assert!(
suggested_delay >= last,
"delay must not decrease (L0={count})"
);
assert!(
suggested_delay < Duration::from_millis(10),
"below cap until stop"
);
last = suggested_delay;
}
}
#[test]
fn slowdown_delay_is_max_of_both_axes() {
let t = thresholds();
let Backpressure::Slowdown { suggested_delay } = Backpressure::compute(9, 3_900, &t) else {
panic!("expected Slowdown");
};
assert!(suggested_delay > Duration::from_millis(8));
}
#[test]
fn slowdown_without_stop_threshold_sits_at_cap() {
let t = BackpressureThresholds {
l0_slowdown: Some(4),
l0_stop: None,
bytes_slowdown: None,
bytes_stop: None,
max_slowdown: Some(Duration::from_millis(5)),
};
assert_eq!(
Backpressure::compute(100, 0, &t),
Backpressure::Slowdown {
suggested_delay: Duration::from_millis(5)
}
);
}
#[test]
fn stop_without_slowdown_threshold_jumps_straight_to_stop() {
let t = BackpressureThresholds {
l0_slowdown: None,
l0_stop: Some(10),
bytes_slowdown: None,
bytes_stop: None,
max_slowdown: None,
};
assert_eq!(Backpressure::compute(9, 0, &t), Backpressure::None);
assert_eq!(Backpressure::compute(10, 0, &t), Backpressure::Stop);
}
#[test]
fn scale_ramp_stays_proportional_for_huge_cap_and_span() {
let cap = Duration::MAX;
let den = u64::MAX;
let num = den / 2;
let got = scale(cap, num, den);
assert!(got <= cap, "a ramped delay never exceeds the cap");
let half_secs = cap.as_secs() / 2;
assert!(
got.as_secs() >= half_secs - 1,
"a half ramp stays proportional (got {}s, expected ~{}s)",
got.as_secs(),
half_secs,
);
}
#[test]
fn is_throttled_reflects_tier() {
assert!(!Backpressure::None.is_throttled());
assert!(
Backpressure::Slowdown {
suggested_delay: Duration::ZERO
}
.is_throttled()
);
assert!(Backpressure::Stop.is_throttled());
}