wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Abandonment coverage for a push write that cannot complete.
//!
//! A write to a peer that has stopped reading must keep expiring the inbound
//! state the read loop would otherwise purge, and the shutdown path that
//! abandons a fast-path write must record the abandonment that the bounded path
//! records. Both cases need a transport whose write never completes, so they
//! live beside the transport rig rather than with the delivery tests.

use std::io;

use bytes::Bytes;
use futures::stream;
use tokio_util::sync::CancellationToken;

use super::{
    PushBatch,
    READ_TIMEOUT_MS,
    empty_inbound,
    transport::{TransportRig, blocked_transport},
};
use crate::{
    app::inbound_handler::{
        frames::{InboundExpiry, buffered_assemblies, incomplete_inbound},
        push_write::write_frame,
    },
    connection::PEER_CLOSED_RESPONSE_GRACE,
};

/// Read timeout the stalled-write case uses, shorter than the idle grace.
const STALLED_READ_TIMEOUT_MS: u64 = 1_000;

/// A stalled push write keeps the inbound expiry running.
///
/// While the write is pending the loop does not poll the reader, so the read
/// deadline would otherwise never be observed and a partial assembly could
/// outlive its timeout. The purge must not cancel or restart the frame: the
/// write keeps its place on the wire and is ended only by the idle grace.
#[rstest::rstest]
#[tokio::test(start_paused = true)]
async fn a_stalled_push_write_purges_expired_inbound_state(
    #[from(blocked_transport)] mut harness: TransportRig,
) {
    use crate::push::PushPriority;

    let shutdown = CancellationToken::new();
    let peer_eof = CancellationToken::new();
    let mut inbound = incomplete_inbound::<Bytes>();
    let mut reader = stream::pending::<io::Result<Bytes>>();
    let mut evicted = Vec::new();
    let mut batch = PushBatch {
        frames: Vec::new(),
        priority: Some(PushPriority::High),
        reader: &mut reader,
        pending_frame: &mut inbound.pending,
        expiry: InboundExpiry::new(
            &mut inbound.message_assembly,
            &mut inbound.read_deadline,
            STALLED_READ_TIMEOUT_MS,
            &mut evicted,
        ),
        peer_eof: &peer_eof,
        shutdown: &shutdown,
    };

    // The peer closed its write half, so the write is bounded by the grace from
    // its first poll. The read deadline is shorter still, so the purge happens
    // several times before the grace abandons the frame.
    let written = tokio::time::timeout(
        PEER_CLOSED_RESPONSE_GRACE * 2,
        write_frame(
            &mut harness.transport(),
            Bytes::from_static(b"stalled"),
            &mut batch,
            true,
        ),
    )
    .await
    .expect("the write must be bounded")
    .expect("an idle-grace abandonment is not an error");
    drop(batch);

    assert!(
        !written.may_continue(),
        "the grace, not a purge, must end the stalled write"
    );
    assert!(
        written.peer_closed(),
        "the abandonment must keep the peer-closed state it was given"
    );
    assert_eq!(
        buffered_assemblies(&inbound),
        Some(0),
        "the stalled write must not postpone the inbound expiry"
    );
}

/// A shutdown that abandons a fast-path write is counted, not just returned.
///
/// The first select prefers a ready write, so its shutdown arm handles only a
/// write that is still pending. That arm must record the abandonment like the
/// bounded path does; otherwise `reason="shutdown"` under-counts the frames a
/// stop actually dropped.
#[rstest::rstest]
#[tokio::test]
async fn a_shutdown_abandoned_fast_path_write_is_counted(
    #[from(blocked_transport)] mut harness: TransportRig,
) {
    use metrics_util::debugging::{DebugValue, DebuggingRecorder};

    use crate::{metrics::PUSH_WRITES_ABANDONED, push::PushPriority};

    let recorder = DebuggingRecorder::new();
    let snapshotter = recorder.snapshotter();
    let shutdown = CancellationToken::new();
    shutdown.cancel();
    let peer_eof = CancellationToken::new();
    let mut inbound = empty_inbound();
    let mut reader = stream::pending::<io::Result<Bytes>>();
    let frame = Bytes::from_static(b"abandoned");
    let mut evicted = Vec::new();
    let mut batch = PushBatch {
        frames: Vec::new(),
        // A single lane, so the diagnostics would name `high` in a warning.
        // The counter keeps its fixed `reason` label and grows no priority
        // label from it.
        priority: Some(PushPriority::High),
        reader: &mut reader,
        pending_frame: &mut inbound.pending,
        expiry: InboundExpiry::new(
            &mut inbound.message_assembly,
            &mut inbound.read_deadline,
            READ_TIMEOUT_MS,
            &mut evicted,
        ),
        peer_eof: &peer_eof,
        shutdown: &shutdown,
    };

    let outcome = {
        let _recorder = ::metrics::set_default_local_recorder(&recorder);
        write_frame(&mut harness.transport(), frame, &mut batch, false).await
    }
    .expect("a shutdown abandonment is not an error");
    assert!(!outcome.may_continue(), "the connection must stop");
    assert!(
        !outcome.peer_closed(),
        "the peer did not close its write half"
    );

    let samples = snapshotter.snapshot().into_vec();
    let (key, _, _, value) = samples
        .iter()
        .find(|(key, ..)| key.key().name() == PUSH_WRITES_ABANDONED)
        .expect("the shutdown abandonment must be counted");
    assert!(
        matches!(value, DebugValue::Counter(1)),
        "the fast path must record exactly one abandonment"
    );
    assert!(
        key.key().labels().count() == 1
            && key
                .key()
                .labels()
                .any(|label| label.key() == "reason" && label.value() == "shutdown"),
        "the counter keeps its single reason label and adds no priority label"
    );
}

/// A blocked write to a silent peer is bounded by the idle grace.
///
/// The observation loop exists to notice a peer that leaves while the write is
/// pending. A peer that stays silent is still a peer that may never read, so
/// the loop must carry the same grace bound as the write that follows it:
/// without that arm the connection waits on the transport for as long as the
/// server lives. The frame is partly on the wire when the grace fires, so the
/// caller must be told to stop rather than to write behind it.
#[rstest::rstest]
#[tokio::test(start_paused = true)]
async fn a_blocked_write_to_a_silent_peer_is_bounded_by_the_grace(
    #[from(blocked_transport)] mut harness: TransportRig,
) {
    let shutdown = CancellationToken::new();
    let peer_eof = CancellationToken::new();
    let mut inbound = empty_inbound();
    let mut reader = stream::pending::<io::Result<Bytes>>();
    let mut evicted = Vec::new();
    let mut batch = PushBatch {
        frames: Vec::new(),
        priority: None,
        reader: &mut reader,
        pending_frame: &mut inbound.pending,
        expiry: InboundExpiry::new(
            &mut inbound.message_assembly,
            &mut inbound.read_deadline,
            READ_TIMEOUT_MS,
            &mut evicted,
        ),
        peer_eof: &peer_eof,
        shutdown: &shutdown,
    };

    let written = tokio::time::timeout(
        PEER_CLOSED_RESPONSE_GRACE * 2,
        write_frame(
            &mut harness.transport(),
            Bytes::from_static(b"stalled"),
            &mut batch,
            false,
        ),
    )
    .await
    .expect("the write must be bounded")
    .expect("an idle-grace abandonment is not an error");
    drop(batch);

    assert!(
        !written.may_continue(),
        "a frame abandoned under the grace must stop the connection"
    );
    assert!(
        !written.peer_closed(),
        "the silent peer never closed its write half"
    );
    assert!(
        !peer_eof.is_cancelled(),
        "a silent peer is not an EOF, so the connection-wide token stays open"
    );
}

/// A reader that reports end-of-stream once and fails if polled again.
///
/// A loop that treats EOF as "keep observing" polls an exhausted reader for
/// ever; counting the polls turns that hang into a failure that names it.
struct EofThenFail {
    /// Whether the end of the stream has been reported.
    reported: bool,
}

impl futures::Stream for EofThenFail {
    type Item = io::Result<Bytes>;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        assert!(
            !self.reported,
            "the write must leave the observation loop once the peer reaches EOF"
        );
        self.reported = true;
        std::task::Poll::Ready(None)
    }
}

/// A write that sees EOF leaves the observation loop.
///
/// The read half is at its end, so there is nothing left to observe. Leaving
/// the loop lets the shared bound finish the write and reports the peer as
/// closed; staying in it polls an exhausted reader without ever yielding.
#[rstest::rstest]
#[tokio::test(start_paused = true)]
async fn a_write_that_sees_eof_leaves_the_observation_loop(
    #[from(blocked_transport)] mut harness: TransportRig,
) {
    let shutdown = CancellationToken::new();
    let peer_eof = CancellationToken::new();
    let mut inbound = empty_inbound();
    let mut reader = EofThenFail { reported: false };
    let mut evicted = Vec::new();
    let mut batch = PushBatch {
        frames: Vec::new(),
        priority: None,
        reader: &mut reader,
        pending_frame: &mut inbound.pending,
        expiry: InboundExpiry::new(
            &mut inbound.message_assembly,
            &mut inbound.read_deadline,
            READ_TIMEOUT_MS,
            &mut evicted,
        ),
        peer_eof: &peer_eof,
        shutdown: &shutdown,
    };

    let written = tokio::time::timeout(
        PEER_CLOSED_RESPONSE_GRACE * 2,
        write_frame(
            &mut harness.transport(),
            Bytes::from_static(b"stalled"),
            &mut batch,
            false,
        ),
    )
    .await
    .expect("the write must be bounded after the peer reaches EOF")
    .expect("an idle-grace abandonment is not an error");
    drop(batch);

    assert!(
        peer_eof.is_cancelled(),
        "the EOF must be recorded for the connection"
    );
    assert!(written.peer_closed(), "the peer closed its write half");
    assert!(
        !written.may_continue(),
        "a write that never completed must stop the connection"
    );
}