use std::{
future::{self, poll_fn},
io,
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
task::Poll,
time::Duration,
};
use bytes::Bytes;
use futures::StreamExt;
use rstest::{fixture, rstest};
use tokio::time::Instant;
use super::{
super::{
stream_active::{ActiveStream, BodyIdentity, HandlerFuture, PendingEvent, PendingSend},
stream_terminal::{HandlerWait, TerminalFailure, await_handler, deliver_then_await},
},
ConnectionStreams,
};
use crate::{
app::inbound_handler::stream_handles::ChunkBudget,
request::{AccountedBodySender, QueuedBytes, RequestBodyStream, accounted_body_channel},
};
fn is_stalled(streams: &ConnectionStreams, bound: Duration) -> bool {
streams.stall_expired_at(bound, Instant::now())
}
fn identity() -> BodyIdentity {
BodyIdentity {
message_id: 1,
correlation_id: None,
}
}
fn failure() -> TerminalFailure {
TerminalFailure {
identity: identity(),
error: io::Error::new(io::ErrorKind::UnexpectedEof, "truncated body"),
}
}
#[fixture]
fn short_wait() -> Duration {
let millis = 50;
Duration::from_millis(millis)
}
async fn stalled_stream() -> (ActiveStream, RequestBodyStream) {
let (sender, body) = accounted_body_channel(2, QueuedBytes::new());
sender
.clone()
.send_chunk(Bytes::from_static(b"unread"), 0, usize::MAX)
.await
.expect("an empty channel admits one chunk");
let stream = ActiveStream {
key: None,
assembly: None,
route_id: 1,
correlation_id: None,
sender: None,
handler: Box::pin(future::pending()),
pending: Some(PendingSend {
sender,
chunk: Bytes::from_static(b"abcd"),
is_last: false,
}),
};
(stream, body)
}
#[fixture]
fn blocked_budget() -> ChunkBudget {
ChunkBudget {
other_retained: 0,
limit: 2,
}
}
#[fixture]
fn body_channel() -> (AccountedBodySender, RequestBodyStream) {
accounted_body_channel(1, QueuedBytes::new())
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_stalled_admission_returns_control_after_the_read_timeout(
short_wait: Duration,
blocked_budget: ChunkBudget,
) {
let (mut stream, _body) = stalled_stream().await;
let event = stream.drive_pending(short_wait, blocked_budget).await;
assert!(
matches!(event, PendingEvent::Idle),
"a stalled admission should hand control back to the loop"
);
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_stall_accumulates_across_drives_until_an_admission_settles(
short_wait: Duration,
blocked_budget: ChunkBudget,
) {
let (active, body) = stalled_stream().await;
let mut streams = ConnectionStreams::new(QueuedBytes::new());
streams.active = Some(active);
let one_and_a_half_waits = short_wait * 3 / 2;
assert!(
!is_stalled(&streams, short_wait),
"a body that has not waited yet cannot have stalled"
);
let first = streams.drive_pending(short_wait, blocked_budget).await;
assert!(
matches!(first, PendingEvent::Idle),
"the blocked chunk should hand control back to the loop"
);
assert!(
is_stalled(&streams, short_wait),
"the expired wait should count toward the body's stall"
);
assert!(
!is_stalled(&streams, one_and_a_half_waits),
"one expired wait alone must not reach a longer bound"
);
let second = streams.drive_pending(short_wait, blocked_budget).await;
assert!(
matches!(second, PendingEvent::Idle),
"each blocked drive should hand control back to the loop"
);
assert!(
is_stalled(&streams, one_and_a_half_waits),
"two stalled drives must accumulate into one longer interval"
);
drop(body);
let settled = streams.drive_pending(short_wait, blocked_budget).await;
assert!(
matches!(settled, PendingEvent::Send(Err(_))),
"a dropped body refuses the queued chunk"
);
assert!(
!is_stalled(&streams, one_and_a_half_waits),
"a settled admission should clear the accumulated interval"
);
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_resolved_handler_wins_over_a_stalled_admission(blocked_budget: ChunkBudget) {
let (mut stream, _body) = stalled_stream().await;
stream.handler = Box::pin(async { Some(Bytes::from_static(b"reply")) });
match stream
.drive_pending(Duration::from_secs(1), blocked_budget)
.await
{
PendingEvent::Handler(outcome) => {
assert_eq!(outcome.as_deref(), Some(b"reply".as_slice()));
}
_ => panic!("the handler outcome should win over a stalled admission"),
}
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_completed_admission_wins_over_a_stalled_handler() {
let (mut stream, _body) = stalled_stream().await;
let pending = stream
.pending
.as_mut()
.expect("the fixture queues a pending admission");
pending.chunk = Bytes::new();
pending.is_last = true;
match stream
.drive_pending(
Duration::from_secs(1),
ChunkBudget {
other_retained: 0,
limit: usize::MAX,
},
)
.await
{
PendingEvent::Send(result) => assert!(result.is_ok()),
_ => panic!("the admission result should win over a stalled handler"),
}
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_terminal_error_reaches_a_handler_whose_channel_is_full(
body_channel: (AccountedBodySender, RequestBodyStream),
) {
let (sender, body) = body_channel;
queue_one_chunk(&sender).await;
let mut handler: HandlerFuture = Box::pin(read_until_terminal(body));
let outcome = deliver_then_await(Duration::from_secs(1), failure(), sender, &mut handler).await;
assert_eq!(
outcome,
HandlerWait::Replied(Some(Bytes::from_static(b"error"))),
"the handler should observe the failure, not a clean end of stream"
);
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn terminal_delivery_is_bounded_when_the_handler_never_reads(
body_channel: (AccountedBodySender, RequestBodyStream),
short_wait: Duration,
) {
let (sender, body) = body_channel;
queue_one_chunk(&sender).await;
let mut handler: HandlerFuture = Box::pin(future::pending());
let outcome = deliver_then_await(short_wait, failure(), sender, &mut handler).await;
drop(body);
assert_eq!(
outcome,
HandlerWait::Deadline,
"an unresponsive handler should still be bounded by the wait"
);
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_delivery_timeout_stops_polling_the_handler(
body_channel: (AccountedBodySender, RequestBodyStream),
short_wait: Duration,
) {
let (sender, body) = body_channel;
queue_one_chunk(&sender).await;
let polls = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&polls);
let mut handler: HandlerFuture = Box::pin(poll_fn(move |_context| {
if counter.fetch_add(1, Ordering::SeqCst) == 0 {
Poll::Pending
} else {
panic!("a timed-out delivery must abandon the handler");
}
}));
let outcome = deliver_then_await(short_wait, failure(), sender, &mut handler).await;
drop(body);
assert_eq!(
outcome,
HandlerWait::Deadline,
"a delivery the channel refused must end the exchange"
);
assert_eq!(
polls.load(Ordering::SeqCst),
1,
"the abandoned handler must not be polled again"
);
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_handler_reply_is_reported_as_replied() {
let mut handler: HandlerFuture = Box::pin(async { Some(Bytes::from_static(b"reply")) });
let outcome = await_handler(Duration::from_secs(1), &mut handler).await;
assert_eq!(
outcome,
HandlerWait::Replied(Some(Bytes::from_static(b"reply"))),
"a completed handler must not be reported as a deadline"
);
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_handler_without_a_reply_is_reported_as_replied() {
let mut handler: HandlerFuture = Box::pin(async { None });
let outcome = await_handler(Duration::from_secs(1), &mut handler).await;
assert_eq!(
outcome,
HandlerWait::Replied(None),
"choosing not to reply must not look like a deadline"
);
}
#[rstest]
#[tokio::test(start_paused = true)]
async fn a_handler_outliving_the_wait_is_reported_as_a_deadline(short_wait: Duration) {
let mut handler: HandlerFuture = Box::pin(future::pending());
let outcome = await_handler(short_wait, &mut handler).await;
assert_eq!(
outcome,
HandlerWait::Deadline,
"a hung handler must be observable as a deadline"
);
}
async fn queue_one_chunk(sender: &AccountedBodySender) {
sender
.clone()
.send_chunk(Bytes::from_static(b"chunk"), 0, usize::MAX)
.await
.expect("an empty queue admits one chunk");
}
async fn read_until_terminal(mut body: RequestBodyStream) -> Option<Bytes> {
while let Some(item) = body.next().await {
if item.is_err() {
return Some(Bytes::from_static(b"error"));
}
}
Some(Bytes::from_static(b"clean"))
}