wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Shared selector for the output sources exercised by the shutdown tests.
//!
//! The gate cases in `availability` and the private-method cases beside
//! `compute_availability` both install the same two sources and ask the same
//! state, retention, and availability questions. Keeping that mapping in one
//! place stops the two test layers from drifting apart.
//!
//! This module is only compiled for unit tests; it is not part of any
//! production or `test-support` surface.

use std::error::Error;

use tokio::sync::mpsc;

use super::super::{
    ConnectionActor,
    ConnectionStateError,
    output::AvailableOutput,
    state::ActorState,
};
use crate::{
    NoProtocolError,
    response::{FrameStream, WireframeError},
};

/// Output source installed for one shutdown-suppression case.
#[derive(Clone, Copy, Debug)]
pub(crate) enum InstalledOutput {
    /// A streaming response.
    Response,
    /// A multi-packet channel whose sender stays open.
    MultiPacket,
}

impl InstalledOutput {
    /// Availability the gate must report while the actor is active.
    pub(crate) const fn available(self) -> AvailableOutput {
        match self {
            Self::Response => AvailableOutput::Response,
            Self::MultiPacket => AvailableOutput::MultiPacket,
        }
    }

    /// Failure message for the post-shutdown suppression assertion.
    pub(crate) const fn suppression_message(self) -> &'static str {
        match self {
            Self::Response => "shutdown should suppress the installed response output",
            Self::MultiPacket => "shutdown should suppress the installed multi-packet output",
        }
    }

    /// Build the actor state matching the installed source.
    pub(crate) fn actor_state(self) -> ActorState {
        match self {
            Self::Response => ActorState::new(true, false),
            Self::MultiPacket => ActorState::new(false, true),
        }
    }

    /// Whether the actor still retains the installed source.
    pub(crate) const fn is_retained(self, actor: &ConnectionActor<u8, NoProtocolError>) -> bool {
        match self {
            Self::Response => actor.active_output.is_response(),
            Self::MultiPacket => actor.active_output.is_multi_packet(),
        }
    }

    /// Install a source that never produces, for the gate cases.
    ///
    /// `keep_alive` retains the multi-packet sender, because a bounded
    /// channel's receiver stays open only while its sender lives.
    pub(crate) fn install_pending(
        self,
        actor: &mut ConnectionActor<u8, NoProtocolError>,
        keep_alive: &mut Vec<mpsc::Sender<u8>>,
    ) -> Result<(), ConnectionStateError> {
        match self {
            Self::Response => {
                let stream: FrameStream<u8, NoProtocolError> = Box::pin(futures::stream::pending());
                actor.set_response(Some(stream))
            }
            Self::MultiPacket => {
                let (tx, rx) = mpsc::channel::<u8>(1);
                keep_alive.push(tx);
                actor.set_multi_packet(Some(rx))
            }
        }
    }

    /// Install a source that is ready to poll, for the observation cases.
    ///
    /// An unsuppressed arm must return this source's event, so the loop proves
    /// the shutdown transition was applied rather than the source being idle.
    /// The send is fallible, so the helper propagates the error rather than
    /// panicking; the repository denies `expect_used` outside test bodies.
    pub(crate) fn install_ready(
        self,
        actor: &mut ConnectionActor<u8, NoProtocolError>,
    ) -> Result<(), Box<dyn Error>> {
        match self {
            Self::Response => {
                let frames = vec![Ok::<u8, WireframeError<NoProtocolError>>(7)];
                actor.set_response(Some(Box::pin(futures::stream::iter(frames))))?;
            }
            Self::MultiPacket => {
                let (tx, rx) = mpsc::channel::<u8>(1);
                // Dropping the sender closes the channel with the frame still
                // queued, so the first poll yields it and the next ends.
                tx.try_send(7)?;
                drop(tx);
                actor.set_multi_packet(Some(rx))?;
            }
        }
        Ok(())
    }
}