use std::future;
use bytes::Bytes;
use rstest::rstest;
use super::{
BodyClose,
FinalDelivery,
HandlerWait,
body_close,
deliver_final_chunk_then_await,
deliver_then_await,
};
use crate::{
app::inbound_handler::{
stream_active::{HandlerFuture, PendingSend},
stream_handles::ChunkBudget,
stream_outcome_support::{
Expectation,
assert_exchange,
identity,
paused_block_on,
queue_one_chunk,
reading_handler,
recorded,
truncated_failure,
wait,
},
},
metrics::StreamingBodyOutcome,
request::{QueuedBytes, accounted_body_channel},
};
fn unbounded_budget() -> ChunkBudget {
ChunkBudget {
other_retained: 0,
limit: usize::MAX,
}
}
#[rstest]
#[test]
fn a_delivered_failure_is_counted_as_terminal() {
let (sender, body) = accounted_body_channel(1, QueuedBytes::new());
let mut handler = reading_handler(body);
let (settled, counts) = recorded(|| {
paused_block_on(deliver_then_await(
wait(),
truncated_failure(),
sender,
&mut handler,
))
});
assert_exchange(
settled,
&counts,
&Expectation {
reply: HandlerWait::Replied(Some(Bytes::from_static(b"failed"))),
recorded: StreamingBodyOutcome::Terminal,
why: "the handler must observe the failure rather than a clean end of stream",
},
);
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum BodyEnd {
Queued,
Dropped,
}
#[rstest]
#[case::wait_expires(
Box::pin(future::pending()),
BodyEnd::Queued,
Expectation {
reply: HandlerWait::Deadline,
recorded: StreamingBodyOutcome::DeliveryTimeout,
why: "an unresponsive handler must be bounded by the wait",
},
)]
#[case::reply_wins(
Box::pin(future::ready(Some(Bytes::from_static(b"reply")))),
BodyEnd::Queued,
Expectation {
reply: HandlerWait::Replied(Some(Bytes::from_static(b"reply"))),
recorded: StreamingBodyOutcome::Abandoned,
why: "the reply settles before the full channel can accept the failure",
},
)]
#[case::receiver_gone(
Box::pin(future::ready(Some(Bytes::from_static(b"reply")))),
BodyEnd::Dropped,
Expectation {
reply: HandlerWait::Replied(Some(Bytes::from_static(b"reply"))),
recorded: StreamingBodyOutcome::Abandoned,
why: "a failure the channel refused was not delivered",
},
)]
#[test]
fn a_failure_competing_with_its_handler_is_counted_by_which_won(
#[case] mut handler: HandlerFuture,
#[case] end: BodyEnd,
#[case] expected: Expectation,
) {
let (sender, body) = accounted_body_channel(1, QueuedBytes::new());
let held_body = (end == BodyEnd::Queued).then_some(body);
let (settled, counts) = recorded(|| {
paused_block_on(async {
if end == BodyEnd::Queued {
queue_one_chunk(&sender).await;
}
deliver_then_await(wait(), truncated_failure(), sender, &mut handler).await
})
});
drop(held_body);
assert_exchange(settled, &counts, &expected);
}
#[rstest]
#[case::delivered(
true,
Expectation {
reply: HandlerWait::Replied(Some(Bytes::from_static(b"last"))),
recorded: StreamingBodyOutcome::Delivered,
why: "the handler must read the completed body, not a truncation",
},
)]
#[case::refused(
false,
Expectation {
reply: HandlerWait::Deadline,
recorded: StreamingBodyOutcome::Abandoned,
why: "a dropped body cannot take the final chunk",
},
)]
#[test]
fn a_completed_final_chunk_is_delivered_whole_or_abandoned(
#[case] reads_body: bool,
#[case] expected: Expectation,
) {
let (sender, body) = accounted_body_channel(2, QueuedBytes::new());
let mut handler: HandlerFuture = if reads_body {
reading_handler(body)
} else {
drop(body);
Box::pin(future::pending())
};
let pending = PendingSend {
sender,
chunk: Bytes::from_static(b"last"),
is_last: true,
};
let delivery = FinalDelivery {
identity: identity(),
pending,
budget: unbounded_budget(),
};
let (settled, counts) = recorded(|| {
paused_block_on(deliver_final_chunk_then_await(
wait(),
delivery,
&mut handler,
))
});
assert_exchange(settled, &counts, &expected);
}
fn assert_final_chunk_abandoned(capacity: usize, budget: ChunkBudget, why: &'static str) {
let (sender, body) = accounted_body_channel(capacity, QueuedBytes::new());
let mut handler: HandlerFuture = Box::pin(future::pending());
let (settled, counts) = recorded(|| {
paused_block_on(async {
queue_one_chunk(&sender).await;
let delivery = FinalDelivery {
identity: identity(),
pending: PendingSend {
sender,
chunk: Bytes::from_static(b"last"),
is_last: true,
},
budget,
};
deliver_final_chunk_then_await(wait(), delivery, &mut handler).await
})
});
drop(body);
assert_exchange(
settled,
&counts,
&Expectation {
reply: HandlerWait::Deadline,
recorded: StreamingBodyOutcome::Abandoned,
why,
},
);
}
#[rstest]
#[test]
fn a_final_chunk_that_cannot_be_delivered_is_counted_as_abandoned() {
assert_final_chunk_abandoned(
1,
unbounded_budget(),
"an unresponsive handler must be bounded by the wait",
);
}
#[rstest]
#[test]
fn a_final_chunk_that_does_not_fit_the_budget_is_abandoned() {
assert_final_chunk_abandoned(
2,
ChunkBudget {
other_retained: 0,
limit: 4,
},
"a final chunk over the budget was forced into the account",
);
}
#[rstest]
#[case::final_chunk(Bytes::from_static(b"last"), true, true)]
#[case::continuation(Bytes::from_static(b"more"), false, false)]
#[test]
fn a_pending_chunk_decides_what_the_close_still_owes(
#[case] chunk: Bytes,
#[case] is_last: bool,
#[case] ends_body: bool,
) {
let (sender, _body) = accounted_body_channel(1, QueuedBytes::new());
let pending = PendingSend {
sender: sender.clone(),
chunk,
is_last,
};
let close = body_close(
Some(pending),
Some(std::io::Error::other("peer went away")),
Some(sender),
identity(),
);
if ends_body {
assert!(
matches!(close, BodyClose::FinalChunk(_)),
"a chunk that ends the body leaves nothing to truncate"
);
} else {
assert!(
matches!(close, BodyClose::Fail { .. }),
"the handler is owed the reason its body stopped"
);
}
}
#[rstest]
#[test]
fn a_body_with_nothing_left_to_deliver_only_awaits_its_reply() {
let close = body_close(
None,
Some(std::io::Error::other("peer went away")),
None,
identity(),
);
assert!(
matches!(close, BodyClose::AwaitReply),
"no open body and no pending chunk means no delivery remains"
);
}