use rstest::{fixture, rstest};
use tokio::time::{Duration, timeout};
use tokio_util::sync::CancellationToken;
use super::{
super::{
ConnectionActor,
event::{Event, EventWatch},
output::AvailableOutput,
},
installed_output::InstalledOutput,
};
use crate::{
NoProtocolError,
push::{PushConfigError, PushQueues},
};
const OBSERVATION_TIMEOUT: Duration = Duration::from_secs(5);
#[fixture]
fn availability_actor() -> Result<ConnectionActor<u8, NoProtocolError>, PushConfigError> {
let (queues, handle) = PushQueues::<u8>::builder()
.high_capacity(1)
.low_capacity(1)
.unlimited()
.build()?;
Ok(ConnectionActor::new(
queues,
handle,
None,
CancellationToken::new(),
))
}
const fn active_watch() -> EventWatch {
EventWatch {
peer: false,
shutdown: true,
}
}
fn drop_queues(actor: &mut ConnectionActor<u8, NoProtocolError>) {
actor.high_rx = None;
actor.low_rx = None;
}
#[rstest]
#[case::response(InstalledOutput::Response)]
#[case::multi_packet(InstalledOutput::MultiPacket)]
fn shutdown_suppresses_the_installed_output(
#[case] installed: InstalledOutput,
availability_actor: Result<ConnectionActor<u8, NoProtocolError>, PushConfigError>,
) {
let mut actor = availability_actor.expect("actor should build");
let mut keep_alive = Vec::new();
installed
.install_pending(&mut actor, &mut keep_alive)
.expect("output should install");
let mut state = installed.actor_state();
assert!(
actor.high_rx.is_some(),
"the high-priority lane must stay open before shutdown"
);
assert!(
actor.low_rx.is_some(),
"the low-priority lane must stay open before shutdown"
);
assert!(
actor
.active_output
.available_output(state.is_shutting_down())
== installed.available(),
"the installed output must be pollable before shutdown"
);
state.start_shutdown();
assert!(
installed.is_retained(&actor),
"shutdown must not clear the installed output"
);
assert!(
matches!(
actor
.active_output
.available_output(state.is_shutting_down()),
AvailableOutput::None
),
"{}",
installed.suppression_message()
);
assert!(
actor.high_rx.is_some(),
"the high-priority lane must stay open after shutdown"
);
assert!(
actor.low_rx.is_some(),
"the low-priority lane must stay open after shutdown"
);
}
#[rstest]
#[case::response(InstalledOutput::Response)]
#[case::multi_packet(InstalledOutput::MultiPacket)]
#[tokio::test]
async fn shutdown_stops_the_loop_observing_a_ready_output(
#[case] installed: InstalledOutput,
availability_actor: Result<ConnectionActor<u8, NoProtocolError>, PushConfigError>,
) {
let mut actor = availability_actor.expect("actor should build");
let shutdown = actor.shutdown_token();
installed
.install_ready(&mut actor)
.expect("ready output should install");
drop_queues(&mut actor);
let mut state = installed.actor_state();
state.start_shutdown();
shutdown.cancel();
assert!(
installed.is_retained(&actor),
"shutdown must not clear the installed output before the loop runs"
);
let event = timeout(
OBSERVATION_TIMEOUT,
actor.next_event_inner(&state, active_watch()),
)
.await
.expect("a suppressed output must not make the loop wait on it");
assert!(
matches!(event, Event::Idle),
"shutdown must leave the loop idle with the ready {installed:?} output; got {event:?}"
);
}