servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Injected accept failures exercise retry timing without exhausting host resources.
use super::Acceptor;
use std::{io, time::Duration};
use tokio::net::TcpListener;
use tokio::time::timeout;

#[tokio::test(start_paused = true)]
async fn pressure_is_bounded_and_retry_survives_select_cancellation() {
    let mut listener = Acceptor::new(TcpListener::bind("127.0.0.1:0").await.unwrap());
    listener
        .faults
        .extend((0..100).map(|_| io::Error::from(io::ErrorKind::OutOfMemory)));
    for _ in 0..100 {
        assert!(
            timeout(Duration::from_millis(1), listener.accept())
                .await
                .is_err()
        );
    }
    assert!(
        (3..=5).contains(&listener.attempts),
        "attempts: {}",
        listener.attempts
    );
    let shutdown = tokio_util::sync::CancellationToken::new();
    shutdown.cancel();
    tokio::select! {
        biased;
        () = shutdown.cancelled() => {},
        _ = listener.accept() => panic!("pressure must not prevent shutdown"),
    }
}

#[tokio::test]
async fn unknown_listener_error_keeps_its_cause() {
    let mut listener = Acceptor::new(TcpListener::bind("127.0.0.1:0").await.unwrap());
    listener.faults.push_back(io::Error::new(
        io::ErrorKind::InvalidInput,
        "broken listener",
    ));
    let error = listener.accept().await.unwrap_err();
    assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
    assert_eq!(error.to_string(), "broken listener");
}

#[cfg(target_os = "linux")]
#[test]
fn pending_linux_network_errors_do_not_kill_a_healthy_listener() {
    for code in [
        libc::ENETDOWN,
        libc::EPROTO,
        libc::ENOPROTOOPT,
        libc::EHOSTDOWN,
        libc::ENONET,
        libc::EHOSTUNREACH,
        libc::EOPNOTSUPP,
        libc::ENETUNREACH,
    ] {
        assert!(matches!(
            super::classify(&io::Error::from_raw_os_error(code)),
            super::Retry::Connection
        ));
    }
    assert!(matches!(
        super::classify(&io::Error::from_raw_os_error(libc::EBADF)),
        super::Retry::Fatal
    ));
}