use bytes::Bytes;
use tracing_test::traced_test;
use super::{deliver_chunk, start_stream};
use crate::{
app::inbound_handler::{
stream_handles::{ChunkOffer, FirstChunk, LoopState},
stream_outcome_support::{counted, final_chunk, reading_route, recorded},
},
message_assembler::{MessageAssemblyState, MessageKey},
metrics::StreamingBodyOutcome,
request::QueuedBytes,
};
fn keyed_first_chunk() -> FirstChunk {
FirstChunk {
key: Some(MessageKey(1)),
is_last: false,
..final_chunk()
}
}
const fn continuation() -> ChunkOffer {
ChunkOffer {
key: MessageKey(1),
chunk: Bytes::from_static(b"more"),
is_last: true,
}
}
#[test]
fn a_start_without_a_handler_is_counted_as_no_handler() {
let mut state = LoopState::new(QueuedBytes::new());
let ((), no_handler) = counted(StreamingBodyOutcome::NoHandler, || {
start_stream(&mut state, None, final_chunk());
});
assert_eq!(no_handler, 1, "the missing handler is counted once");
assert!(
!state.streams.is_live(),
"a body with no handler must not occupy the slot"
);
}
#[test]
fn a_start_with_a_handler_registers_the_exchange() {
let handler = reading_route();
let mut state = LoopState::new(QueuedBytes::new());
let ((), counts) = recorded(|| {
start_stream(&mut state, Some(&handler), final_chunk());
});
assert!(
state.streams.is_live(),
"the handler's exchange should own the slot"
);
assert_eq!(
counts.count(StreamingBodyOutcome::NoHandler),
0,
"a resolved handler is not a missing one"
);
}
#[traced_test]
#[test]
fn a_continuation_for_an_abandoned_body_is_counted_as_abandoned() {
let mut state = LoopState::new(QueuedBytes::new());
let mut assembly: Option<MessageAssemblyState> = None;
let ((), abandoned) = counted(StreamingBodyOutcome::Abandoned, || {
deliver_chunk(&mut state, &mut assembly, continuation());
});
assert_eq!(abandoned, 1, "the dropped continuation is counted once");
logs_assert(|lines: &[&str]| {
lines
.iter()
.any(|line| {
line.contains("dropping chunk for abandoned streaming message")
&& line.contains("abandoned_body")
})
.then_some(())
.ok_or_else(|| "the dropped continuation is not reported".to_owned())
});
}
#[test]
fn a_continuation_for_the_live_body_is_not_counted() {
let handler = reading_route();
let mut state = LoopState::new(QueuedBytes::new());
start_stream(&mut state, Some(&handler), keyed_first_chunk());
if let Some(active) = state.streams.active.as_mut() {
active.pending = None;
}
let mut assembly: Option<MessageAssemblyState> = None;
let ((), counts) = recorded(|| {
deliver_chunk(&mut state, &mut assembly, continuation());
});
assert_eq!(
counts.count(StreamingBodyOutcome::Abandoned),
0,
"a continuation for the live body is not an abandoned chunk"
);
assert!(
state
.streams
.active
.as_ref()
.is_some_and(|active| active.pending.is_some()),
"the continuation should be waiting for admission"
);
}