servlet 1.1.0

A small application runtime with an explicit operational contract
Documentation
//! Controlled ownership races on two- and four-thread executors.
use super::startup::launch;
use crate::{App, AppConfig, Error, FailureKind, Phase};
use std::{
    sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    },
    time::Duration,
};
use tokio::sync::{Barrier, oneshot};

#[test]
fn cross_thread_startup_shutdown_and_worker_failure() {
    for threads in [2, 4] {
        tokio::runtime::Builder::new_multi_thread()
            .worker_threads(threads)
            .enable_all()
            .build()
            .unwrap()
            .block_on(async {
                for _ in 0..16 {
                    late_readiness().await;
                    failure_during_shutdown().await;
                }
            });
    }
}

fn config() -> AppConfig {
    AppConfig {
        startup_timeout: Duration::from_secs(2),
        shutdown_timeout: Duration::from_secs(1),
        cleanup_reserve: Duration::from_millis(100),
        ..AppConfig::default()
    }
}

async fn late_readiness() {
    let (entered, entry) = oneshot::channel();
    let (release, released) = oneshot::channel();
    let cleaned = Arc::new(AtomicUsize::new(0));
    let cleanup = cleaned.clone();
    let app = App::new("late-readiness")
        .config(config())
        .worker("late", |ctx| async move {
            entered.send(()).unwrap();
            released.await.unwrap();
            ctx.ready();
            ctx.shutdown().cancelled().await;
            Ok(())
        })
        .cleanup("once", move || async move {
            cleanup.fetch_add(1, Ordering::SeqCst);
            Ok(())
        });
    let (running, ready) = launch(app.registration, app.router).unwrap();
    entry.await.unwrap();
    let handle = running.handle();
    handle.shutdown();
    release.send(()).unwrap();
    assert!(ready.await.is_err());
    let result = running.wait().await;
    assert!(result.is_err());
    assert!(!handle.is_ready());
    assert_eq!(handle.phase(), Phase::Stopped);
    assert_eq!(cleaned.load(Ordering::SeqCst), 1);
}

async fn failure_during_shutdown() {
    let barrier = Arc::new(Barrier::new(2));
    let gate = barrier.clone();
    let cleaned = Arc::new(AtomicUsize::new(0));
    let cleanup = cleaned.clone();
    let running = App::new("failure-race")
        .config(config())
        .worker("required", move |ctx| async move {
            ctx.ready();
            gate.wait().await;
            Err("worker failed during shutdown".into())
        })
        .cleanup("once", move || async move {
            cleanup.fetch_add(1, Ordering::SeqCst);
            Ok(())
        })
        .start()
        .await
        .unwrap();
    let handle = running.handle();
    let shutdown = tokio::spawn(async move {
        barrier.wait().await;
        handle.shutdown();
    });
    let Error::Lifecycle(failures) = running.wait().await.unwrap_err() else {
        panic!("typed lifecycle result");
    };
    shutdown.await.unwrap();
    assert!(
        failures
            .iter()
            .any(|failure| failure.component == "required"
                && matches!(failure.kind, FailureKind::Error(_)))
    );
    assert_eq!(cleaned.load(Ordering::SeqCst), 1);
}