use std::collections::HashMap;
use bytes::Bytes;
use futures::StreamExt;
use tokio::time::Instant;
use super::{
PrefilledOffer,
deliver_prefilled,
lookup_streaming_handler,
refuse_prefilled,
refuse_unrouted,
};
use crate::{
app::inbound_handler::{
stream_handles::ChunkBudget,
stream_outcome_support::{counted, paused_block_on, reading_route, recorded, wait},
},
metrics::StreamingBodyOutcome,
request::{QueuedBytes, accounted_body_channel},
};
const PARKED_MESSAGE_ID: u32 = 10;
const fn offer() -> PrefilledOffer {
PrefilledOffer {
body: Bytes::from_static(b"abcd"),
message_id: PARKED_MESSAGE_ID,
}
}
fn deadline() -> Instant { Instant::now() + wait() }
const fn open_budget() -> ChunkBudget {
ChunkBudget {
other_retained: 0,
limit: usize::MAX,
}
}
const fn tight_budget() -> ChunkBudget {
ChunkBudget {
other_retained: 0,
limit: 6,
}
}
#[test]
fn an_affordable_prefilled_body_is_counted_as_delivered() {
let (sender, mut body) = accounted_body_channel(1, QueuedBytes::new());
let (observed, counts) = recorded(|| {
paused_block_on(async {
deliver_prefilled(sender, offer(), open_budget(), deadline()).await;
body.next().await
})
});
let chunk = observed
.expect("the delivery runs on a paused runtime")
.expect("the whole body should arrive as one chunk")
.expect("the chunk is not an error");
assert_eq!(chunk.as_ref(), b"abcd", "the whole body is delivered");
assert_eq!(
counts.count(StreamingBodyOutcome::Delivered),
1,
"the delivered body is counted once"
);
}
#[test]
fn a_prefilled_body_refused_by_a_dropped_handler_is_abandoned() {
let (sender, body) = accounted_body_channel(1, QueuedBytes::new());
drop(body);
let (_, abandoned) = counted(StreamingBodyOutcome::Abandoned, || {
paused_block_on(deliver_prefilled(
sender,
offer(),
open_budget(),
deadline(),
))
});
assert_eq!(abandoned, 1, "the body the handler refused is counted once");
}
#[test]
fn an_unaffordable_prefilled_body_is_counted_as_an_admission_timeout() {
let (observed, counts) = recorded(|| {
paused_block_on(async {
let queued = QueuedBytes::new();
let (holder, _held) = accounted_body_channel(1, queued.clone());
holder
.send_chunk(Bytes::from_static(b"held"), 0, usize::MAX)
.await
.expect("an empty queue admits one chunk");
let (sender, mut body) = accounted_body_channel(1, queued);
deliver_prefilled(sender, offer(), tight_budget(), deadline()).await;
body.next().await
})
});
let item = observed
.expect("the delivery runs on a paused runtime")
.expect("the refused body still yields a terminal item");
assert!(item.is_err(), "the handler reads an error, not a body");
assert_eq!(
counts.count(StreamingBodyOutcome::AdmissionTimeout),
1,
"the expired admission is counted once"
);
assert_eq!(
counts.count(StreamingBodyOutcome::Abandoned),
0,
"the handler did not abandon a body it never received"
);
}
#[test]
fn a_refused_body_whose_failure_is_not_delivered_is_also_abandoned() {
let (sender, body) = accounted_body_channel(1, QueuedBytes::new());
drop(body);
let (_, counts) = recorded(|| paused_block_on(refuse_prefilled(&sender, PARKED_MESSAGE_ID)));
assert_eq!(
counts.count(StreamingBodyOutcome::AdmissionTimeout),
1,
"the body that could not be retained is counted once"
);
assert_eq!(
counts.count(StreamingBodyOutcome::Abandoned),
1,
"the undeliverable terminal error is counted once"
);
}
#[test]
fn a_missing_parked_handler_is_counted_as_no_handler() {
let routes: HashMap<u32, _> = HashMap::new();
let (missing, counts) = recorded(|| {
refuse_unrouted(PARKED_MESSAGE_ID);
lookup_streaming_handler(&routes, PARKED_MESSAGE_ID)
});
assert!(missing.is_none(), "no handler was registered");
assert_eq!(
counts.count(StreamingBodyOutcome::NoHandler),
1,
"the refusal is counted once"
);
}
#[test]
fn the_lookup_alone_does_not_count_a_miss() {
let mut routes = HashMap::new();
let (missing, counts) = recorded(|| lookup_streaming_handler(&routes, PARKED_MESSAGE_ID));
assert!(missing.is_none(), "no handler was registered");
assert_eq!(
counts.count(StreamingBodyOutcome::NoHandler),
0,
"a lookup must not record the outcome its caller owns"
);
routes.insert(PARKED_MESSAGE_ID, reading_route());
let (found, resolved) = recorded(|| lookup_streaming_handler(&routes, PARKED_MESSAGE_ID));
assert!(found.is_some(), "the registered handler is resolved");
assert_eq!(
resolved.count(StreamingBodyOutcome::NoHandler),
0,
"a resolved handler is not a missing one"
);
}