use super::{
ConnectionActor,
event::{Event, EventWatch},
output::{AvailableOutput, EventAvailability},
polling::EventSources,
state::ActorState,
};
use crate::{app::Packet, correlation::CorrelatableFrame, push::FrameLike};
impl<F, E, P> ConnectionActor<F, E, P>
where
F: FrameLike + CorrelatableFrame + Packet,
E: std::fmt::Debug,
P: FrameLike,
{
const fn compute_availability(&self, state: &ActorState) -> EventAvailability {
EventAvailability {
high: self.high_rx.is_some(),
low: self.low_rx.is_some(),
output: self
.active_output
.available_output(state.is_shutting_down()),
}
}
pub(super) async fn next_event(&mut self, state: &ActorState) -> Event<F, E, P> {
self.next_event_inner(
state,
EventWatch {
peer: false,
shutdown: true,
},
)
.await
}
const fn should_watch_peer(watch: EventWatch, state: &ActorState) -> bool {
watch.peer && state.is_active()
}
pub(super) async fn next_event_inner(
&mut self,
state: &ActorState,
watch: EventWatch,
) -> Event<F, E, P> {
let avail = self.compute_availability(state);
let can_poll_multi_packet = avail.output == AvailableOutput::MultiPacket;
let can_poll_response = avail.output == AvailableOutput::Response;
let EventSources {
high,
low,
multi_packet,
response,
shutdown,
peer_closed,
} = self.event_sources();
tokio::select! {
biased;
() = Self::wait_shutdown(shutdown), if watch.shutdown && state.is_active() => {
Event::Shutdown
}
() = Self::wait_peer_closed(peer_closed), if Self::should_watch_peer(watch, state) => {
Event::PeerClosed
}
res = Self::poll_queue(high), if avail.high => Event::High(res),
res = Self::poll_queue(low), if avail.low => Event::Low(res),
res = Self::poll_queue(multi_packet), if can_poll_multi_packet => Event::MultiPacket(res),
res = Self::poll_response(response), if can_poll_response => Event::Response(res),
else => Event::Idle,
}
}
}
#[cfg(all(test, not(loom)))]
mod tests {
use rstest::rstest;
use tokio_util::sync::CancellationToken;
use super::super::{
ConnectionActor,
output::{AvailableOutput, EventAvailability},
state::ActorState,
tests::installed_output::InstalledOutput,
};
use crate::{NoProtocolError, push::PushQueues};
fn assert_lanes(availability: EventAvailability, high: bool, low: bool) {
assert_eq!(
availability.high, high,
"the high-priority lane flag must match the actor's lane"
);
assert_eq!(
availability.low, low,
"the low-priority lane flag must match the actor's lane"
);
}
#[rstest]
#[case::response(InstalledOutput::Response)]
#[case::multi_packet(InstalledOutput::MultiPacket)]
#[test]
fn compute_availability_reports_and_suppresses_the_installed_output(
#[case] installed: InstalledOutput,
) {
let (queues, handle) = PushQueues::<u8>::builder()
.high_capacity(1)
.low_capacity(1)
.unlimited()
.build()
.expect("push queues should build");
let mut actor: ConnectionActor<u8, NoProtocolError> =
ConnectionActor::new(queues, handle, None, CancellationToken::new());
let mut keep_alive = Vec::new();
installed
.install_pending(&mut actor, &mut keep_alive)
.expect("output should install");
let mut state = installed.actor_state();
let active = actor.compute_availability(&state);
assert_lanes(active, true, true);
assert!(
active.output == installed.available(),
"the installed {installed:?} output must be pollable while active"
);
state.start_shutdown();
let shutting_down = actor.compute_availability(&state);
assert_lanes(shutting_down, true, true);
assert!(
matches!(shutting_down.output, AvailableOutput::None),
"shutdown must suppress the installed {installed:?} output"
);
assert!(
installed.is_retained(&actor),
"shutdown must retain the installed {installed:?} output for cleanup"
);
}
#[test]
fn compute_availability_reports_a_closed_lane() {
let (queues, handle) = PushQueues::<u8>::builder()
.high_capacity(1)
.low_capacity(1)
.unlimited()
.build()
.expect("push queues should build");
let mut actor: ConnectionActor<u8, NoProtocolError> =
ConnectionActor::new(queues, handle, None, CancellationToken::new());
actor.low_rx = None;
let state = ActorState::new(false, false);
let availability = actor.compute_availability(&state);
assert_lanes(availability, true, false);
assert!(
matches!(availability.output, AvailableOutput::None),
"no output source is installed"
);
}
}