wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Accounting and diagnostics for a delivery the shared deadline drops.
//!
//! A delivery that outlives the connection's wait never reaches its own timeout
//! arm, so the outcome it owed is recorded where the drop happens. A final
//! chunk is also named in the warning there, because no later record of that
//! body can exist; a terminal failure is only counted, since its own path
//! carries the diagnostic.

use std::future;

use rstest::rstest;
use tokio::time::Instant;

use super::{Delivery, HandlerWait, UnsettledDelivery, race_delivery};
use crate::{
    app::inbound_handler::{
        stream_active::{BodyIdentity, HandlerFuture},
        stream_outcome_support::{Expectation, assert_exchange, paused_block_on, recorded, wait},
    },
    metrics::StreamingBodyOutcome,
};

/// A delivery still pending at the connection's deadline is counted once.
///
/// The delivery future here never resolves, so the connection's own deadline is
/// necessarily the first to fire and the delivery is dropped unsettled. That
/// drop must still record the outcome the delivery owed, exactly once, rather
/// than leaving the body uncounted: the deadline and the delivery share one
/// instant, and nothing would report for the dropped future afterwards.
#[rstest]
#[test]
fn a_delivery_still_pending_at_the_deadline_is_counted_once() {
    let (settled, counts) = recorded(|| {
        paused_block_on(async {
            let mut handler: HandlerFuture = Box::pin(future::pending());
            let deadline = Instant::now() + wait();
            race_delivery(
                deadline,
                future::pending::<Delivery>(),
                &mut handler,
                UnsettledDelivery::counted(StreamingBodyOutcome::Abandoned),
            )
            .await
        })
    });

    assert_exchange(
        settled,
        &counts,
        &Expectation {
            reply: HandlerWait::Deadline,
            recorded: StreamingBodyOutcome::Abandoned,
            why: "a delivery dropped at the deadline must be counted exactly once",
        },
    );
}

/// A final chunk the outer deadline drops is named in a warning.
///
/// The delivery future here never resolves, so the connection's deadline is the
/// first to fire and the chunk is dropped unsettled. Its own timeout arm never
/// runs, so the warning has to be emitted where the drop happens; without it an
/// operator sees an abandoned body with no message it can be attributed to.
#[cfg(feature = "metrics")]
#[rstest]
#[test]
fn a_final_chunk_dropped_at_the_deadline_is_warned_about() {
    // A bespoke identifier: the log buffer is process-wide and sibling tests
    // emit the same message, so only a record naming this body counts.
    let identity = BodyIdentity {
        message_id: UNIQUE_MESSAGE_ID,
        correlation_id: Some(11),
    };
    let mut handler: HandlerFuture = Box::pin(future::pending());
    let deadline = Instant::now() + wait();
    let mut log = wireframe_testing::logger();
    log.clear();

    let (settled, counts) = recorded(|| {
        paused_block_on(race_delivery(
            deadline,
            future::pending::<Delivery>(),
            &mut handler,
            UnsettledDelivery::final_chunk(identity),
        ))
    });

    assert_exchange(
        settled,
        &counts,
        &Expectation {
            reply: HandlerWait::Deadline,
            recorded: StreamingBodyOutcome::Abandoned,
            why: "a final chunk dropped at the deadline must be counted once",
        },
    );
    let fingerprint = format!("message_id={UNIQUE_MESSAGE_ID}");
    let warnings: Vec<String> = std::iter::from_fn(|| log.pop())
        .map(|record| record.args().to_owned())
        .filter(|message| {
            message.contains("final request body chunk was not admitted")
                && message.contains(&fingerprint)
        })
        .collect();
    assert_eq!(
        warnings.len(),
        1,
        "exactly one warning must name the dropped final chunk; captured: {warnings:#?}"
    );
    let warning = warnings
        .first()
        .expect("the single captured warning must be inspectable");
    assert!(
        warning.contains(&format!("message_id={}", identity.message_id)),
        "the warning must name the message; captured: {warning}"
    );
    assert!(
        warning.contains("error_category=\"abandoned_body\""),
        "the warning must carry the body's category; captured: {warning}"
    );
}

/// Message identifier that only this module's case reports under.
const UNIQUE_MESSAGE_ID: u32 = 9_991;