servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Real pools expose local gauges and retain cleanup ownership if registration fails.
use super::support::{config, settings};
use servlet::{App, ecosystem::tokio, metrics::Registry, sql::PgReadiness, sync::oneshot};
use std::time::Duration;

#[tokio::test]
#[ignore = "requires isolated PostgreSQL; python3 -B scripts/postgres_test.py"]
async fn named_pool_metrics_follow_acquire_return_and_managed_close() {
    let metrics = Registry::new();
    let (sender, pools) = oneshot::channel();
    let running = App::new("pool-metrics")
        .config(settings())
        .metrics(metrics.clone())
        .setup(move |mut ctx| async move {
            let policy = config(false).with_readiness(PgReadiness::NotRequired)?;
            let first = ctx.postgres("primary", policy.clone()).await?;
            let second = ctx.postgres("secondary", policy).await?;
            sender.send((first, second)).unwrap();
            Ok(())
        })
        .start()
        .await
        .unwrap();
    let (first, second) = pools.await.unwrap();
    let held = first.acquire().await.unwrap();
    servlet::time::timeout(Duration::from_secs(1), async {
        while second.num_idle() != 1 {
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    let output = scrape(running.handle().admin_addr().unwrap()).await;
    assert!(output.starts_with("HTTP/1.1 200"));
    for sample in [
        "servlet_sql_pool_connections{pool=\"primary\"} 1\n",
        "servlet_sql_pool_idle_connections{pool=\"primary\"} 0\n",
        "servlet_sql_pool_in_use_connections{pool=\"primary\"} 1\n",
        "servlet_sql_pool_idle_connections{pool=\"secondary\"} 1\n",
        "servlet_sql_pool_in_use_connections{pool=\"secondary\"} 0\n",
    ] {
        assert!(output.contains(sample), "{output}");
    }
    drop(held);
    servlet::time::timeout(Duration::from_secs(1), async {
        while first.num_idle() != 1 {
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    assert!(
        metrics
            .encode()
            .unwrap()
            .contains("servlet_sql_pool_idle_connections{pool=\"primary\"} 1\n")
    );
    running.handle().shutdown();
    running.wait().await.unwrap();
    assert!(first.is_closed() && second.is_closed());
    let output = metrics.encode().unwrap();
    for pool in ["primary", "secondary"] {
        assert!(output.contains(&format!(
            "servlet_sql_pool_connections{{pool=\"{pool}\"}} 0\n"
        )));
    }
}

#[tokio::test]
#[ignore = "requires isolated PostgreSQL; python3 -B scripts/postgres_test.py"]
async fn metric_capacity_failure_rolls_back_previously_connected_pool() {
    let metrics = Registry::new();
    for index in 0..124 {
        metrics
            .counter(&format!("application_{index}"), "Application instrument")
            .unwrap();
    }
    let (sender, first) = oneshot::channel();
    let result = App::new("full-metrics")
        .config(settings())
        .metrics(metrics.clone())
        .setup(move |mut ctx| async move {
            let policy = config(false).with_readiness(PgReadiness::NotRequired)?;
            let pool = ctx.postgres("first", policy.clone()).await?;
            sender.send(pool).unwrap();
            // First pool consumes three definitions; another three exceed 128.
            ctx.postgres("second", policy).await?;
            Ok(())
        })
        .start()
        .await;
    assert!(result.is_err());
    assert!(first.await.unwrap().is_closed());
    let output = metrics.encode().unwrap();
    assert!(output.contains("servlet_sql_pool_connections{pool=\"first\"} 0\n"));
    assert!(!output.contains("pool=\"second\""));
}

async fn scrape(address: std::net::SocketAddr) -> String {
    use servlet::io::{AsyncReadExt, AsyncWriteExt};
    servlet::time::timeout(Duration::from_secs(1), async {
        let mut stream = servlet::net::TcpStream::connect(address).await.unwrap();
        stream
            .write_all(b"GET /metrics HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
            .await
            .unwrap();
        let mut output = String::new();
        stream.read_to_string(&mut output).await.unwrap();
        output
    })
    .await
    .unwrap()
}