use super::*;
#[test]
fn silent_health_listener_has_zero_application_wakes() -> Result<(), Box<dyn std::error::Error>> {
let server = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let deadline = Instant::now() + Duration::from_secs(2);
while server.accept_attempts() < 1 && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
let armed = server.accept_attempts();
assert_eq!(
armed, 1,
"the blocking accept is issued exactly once when parked"
);
thread::sleep(Duration::from_millis(200));
assert_eq!(
server.accept_attempts(),
armed,
"a silent health listener must not wake or re-accept"
);
assert_eq!(
server.shed_count(),
0,
"a silent health listener sheds nothing"
);
let response = get(server.local_addr(), "/health")?;
assert_status(&response, 200);
let body = json_body(&response)?;
assert_eq!(body["status"], "healthy");
server.shutdown()?;
Ok(())
}
#[test]
fn health_accept_source_has_no_wouldblock_sleep_poll() {
const SOURCE: &str = concat!(
include_str!("../endpoint.rs"),
include_str!("serve.rs"),
include_str!("routes.rs"),
include_str!("responses.rs")
);
let production = SOURCE;
for forbidden in [
"set_nonblocking(true)",
"ErrorKind::WouldBlock",
"thread::sleep",
] {
assert!(
!production.contains(forbidden),
"retired health accept-path source `{forbidden}` reappeared"
);
}
}
#[test]
fn health_shutdown_interrupts_accept_wait() -> Result<(), Box<dyn std::error::Error>> {
let server = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let start = Instant::now();
server.shutdown()?;
assert!(
start.elapsed() < Duration::from_secs(2),
"shutdown before arming must interrupt promptly, not sleep-poll"
);
let server = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let deadline = Instant::now() + Duration::from_secs(2);
while server.accept_attempts() < 1 && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
assert_eq!(
server.accept_attempts(),
1,
"the worker parked before shutdown"
);
let parked_addr = server.local_addr();
let start = Instant::now();
server.shutdown()?;
assert!(
start.elapsed() < Duration::from_secs(2),
"shutdown of a parked accept must interrupt promptly"
);
assert!(
TcpStream::connect(parked_addr).is_err(),
"the listener descriptor was released; further connects are refused"
);
let server = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let deadline = Instant::now() + Duration::from_secs(2);
while server.accept_attempts() < 1 && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
let _pending = TcpStream::connect(server.local_addr())?;
let start = Instant::now();
server.shutdown()?;
assert!(
start.elapsed() < Duration::from_millis(500),
"shutdown concurrent with a pending accept must interrupt promptly (TOLD), \
not defer by a request read deadline: elapsed {:?}",
start.elapsed()
);
let server = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let response = get(server.local_addr(), "/health")?;
assert_status(&response, 200);
let start = Instant::now();
server.shutdown()?;
assert!(
start.elapsed() < Duration::from_secs(2),
"shutdown after a served request must interrupt the next parked accept promptly"
);
Ok(())
}
#[test]
fn shutdown_interrupts_in_flight_silent_request_read() -> Result<(), Box<dyn std::error::Error>> {
let server = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let _silent = TcpStream::connect(server.local_addr())?;
let deadline = Instant::now() + Duration::from_secs(2);
while server.requests_entered() < 1 && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
assert_eq!(
server.requests_entered(),
1,
"the worker entered the in-flight silent request read"
);
let start = Instant::now();
server.shutdown()?;
assert!(
start.elapsed() < Duration::from_millis(500),
"shutdown must interrupt an in-flight silent request read promptly (TOLD), \
not defer by the read's admitted 2s deadline: elapsed {:?}",
start.elapsed()
);
Ok(())
}
#[test]
fn health_idle_grows_unrelated_counters_while_accept_stays_flat()
-> Result<(), Box<dyn std::error::Error>> {
let idle = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let busy = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let deadline = Instant::now() + Duration::from_secs(2);
while (idle.accept_attempts() < 1 || busy.accept_attempts() < 1) && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
let idle_armed = idle.accept_attempts();
assert_eq!(idle_armed, 1, "the idle listener parks exactly one accept");
let busy_before = busy.accept_attempts();
for _ in 0..5 {
let response = get(busy.local_addr(), "/health")?;
assert_status(&response, 200);
}
let deadline = Instant::now() + Duration::from_secs(2);
while busy.accept_attempts() <= busy_before && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
assert!(
busy.accept_attempts() > busy_before,
"an unrelated served request grows the busy listener's accept counter"
);
assert_eq!(
idle.accept_attempts(),
idle_armed,
"the silent listener's accept counter stays flat during the workload"
);
assert_eq!(idle.shed_count(), 0, "the silent listener sheds nothing");
idle.shutdown()?;
busy.shutdown()?;
Ok(())
}
fn sweep_health_response(address: SocketAddr) -> Result<String, Box<dyn std::error::Error>> {
let mut stream = TcpStream::connect(address)?;
stream.write_all(b"GET /health HTTP/1.1\r\nHost: localhost\r\n\r\n")?;
let mut response = String::new();
stream.read_to_string(&mut response)?;
assert_status(&response, 200);
Ok(response)
}
#[test]
fn startup_sweep_counts_are_absent_until_installed_and_never_replaced()
-> Result<(), Box<dyn std::error::Error>> {
let health = start_health_server(loopback_ephemeral()?, SharedReadinessState::default())?;
let before = sweep_health_response(health.local_addr())?;
assert!(before.contains("\"ephemeral_sweep\":null"), "{before}");
let counts = liminal::durability::EphemeralSweepCounts {
removed_count: 3,
removed_bytes: 123,
held_count: 1,
};
health.install_ephemeral_sweep_report(counts)?;
assert!(
health
.install_ephemeral_sweep_report(liminal::durability::EphemeralSweepCounts::default())
.is_err()
);
for _ in 0..2 {
let response = sweep_health_response(health.local_addr())?;
let (_, body) = response
.split_once("\r\n\r\n")
.ok_or("health body absent")?;
let value: serde_json::Value = serde_json::from_str(body)?;
assert_eq!(
value["ephemeral_sweep"],
serde_json::json!({"removed_count":3,"removed_bytes":123,"held_count":1})
);
}
health.shutdown()?;
Ok(())
}