servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Missing context fails closed; elapsed time is never replaced with a fresh budget.
use super::RequestDeadline;
use axum::{
    extract::FromRequestParts,
    http::{Request, StatusCode},
};
use std::time::Duration;
use tokio::time::{Instant, advance};

#[tokio::test(start_paused = true)]
async fn copies_and_repeated_extraction_preserve_one_absolute_cutoff() {
    let cutoff = Instant::now() + Duration::from_secs(1);
    let (mut parts, _) = Request::new(()).into_parts();
    parts.extensions.insert(RequestDeadline(cutoff));
    let first = RequestDeadline::from_request_parts(&mut parts, &())
        .await
        .unwrap();
    advance(Duration::from_millis(400)).await;
    let second = RequestDeadline::from_request_parts(&mut parts, &())
        .await
        .unwrap();
    assert_eq!(first.instant(), cutoff);
    assert_eq!(second.instant(), cutoff);
    assert_eq!(second.remaining(), Duration::from_millis(600));
    advance(Duration::from_secs(1)).await;
    assert_eq!(first.remaining(), Duration::ZERO);
    assert_eq!(second.remaining(), Duration::ZERO);
}

#[tokio::test]
async fn a_foreign_router_or_removed_extension_does_not_invent_a_deadline() {
    let (mut parts, _) = Request::builder()
        .header("x-request-timeout", "3600")
        .body(())
        .unwrap()
        .into_parts();
    assert_eq!(
        RequestDeadline::from_request_parts(&mut parts, &())
            .await
            .unwrap_err(),
        StatusCode::INTERNAL_SERVER_ERROR
    );
}