use tracing_test::traced_test;
use super::{
abandoned_chunk,
admission_timed_out,
body_stalled,
final_chunk_refused,
final_chunk_timed_out,
handler_deadline_exceeded,
no_live_handler,
no_streaming_handler,
prefilled_failure_not_delivered,
prefilled_not_admitted,
prefilled_not_delivered,
read_timed_out,
terminal_delivery_timed_out,
terminal_receiver_gone,
};
use crate::{
app::inbound_handler::stream_active::BodyIdentity,
message_assembler::MessageKey,
request::{SendChunkError, SendClosed},
};
const EVENTS: [(&str, &str); 13] = [
(
"timed out delivering the request body failure",
"delivery_timeout",
),
(
"request body receiver gone before the failure was delivered",
"receiver_gone",
),
("pre-filled request body was not delivered", "closed"),
(
"pre-filled request body was not admitted",
"admission_timeout",
),
(
"pre-filled request body failure was not delivered",
"receiver_gone",
),
("no streaming handler for message id", "no_handler"),
(
"dropping chunk for abandoned streaming message",
"abandoned_body",
),
(
"streaming failure for a message with no live handler",
"no_handler",
),
(
"streaming handler exceeded the connection wait",
"reply_abandoned",
),
("final request-body chunk refused", "abandoned_body"),
(
"request body chunk waited for admission without settling",
"admission_timeout",
),
(
"request body admission made no progress",
"admission_timeout",
),
(
"final request body chunk was not admitted before the deadline",
"abandoned_body",
),
];
fn identity() -> BodyIdentity {
BodyIdentity {
message_id: 7,
correlation_id: Some(11),
}
}
fn emit_every_event() {
let identity = identity();
let key = MessageKey(62);
read_timed_out();
terminal_delivery_timed_out(identity);
terminal_receiver_gone(identity, SendClosed);
prefilled_not_delivered(1, SendChunkError::Closed);
prefilled_not_admitted(2);
prefilled_failure_not_delivered(3, SendClosed);
no_streaming_handler(4);
abandoned_chunk(key);
no_live_handler(key);
handler_deadline_exceeded(5);
final_chunk_refused(identity, SendChunkError::Closed);
admission_timed_out(identity, std::time::Duration::from_millis(250));
body_stalled(identity, std::time::Duration::from_secs(30));
final_chunk_timed_out(identity);
}
#[traced_test]
#[test]
fn every_diagnostic_carries_its_documented_message_and_category() {
emit_every_event();
logs_assert(|lines: &[&str]| {
let missing = EVENTS.iter().find(|(message, category)| {
!lines
.iter()
.any(|line| line.contains(message) && line.contains(category))
});
match missing {
None => Ok(()),
Some((message, category)) => {
Err(format!("no event carries {message:?} with {category:?}"))
}
}
});
}
#[traced_test]
#[test]
fn a_read_timeout_is_reported_without_a_body_category() {
read_timed_out();
logs_assert(|lines: &[&str]| {
let timeout = lines
.iter()
.find(|line| line.contains("DEBUG") && line.contains("read timeout elapsed"))
.ok_or_else(|| "the read timeout is not reported at debug level".to_owned())?;
if timeout.contains("error_category") {
return Err(format!(
"a read timeout has no body outcome to categorize: {timeout}"
));
}
Ok(())
});
}
struct Expected<'a> {
level: &'a str,
message: &'a str,
operation: &'a str,
category: &'a str,
}
fn reports(lines: &[&str], expected: &Expected<'_>) -> bool {
lines.iter().any(|line| {
line.contains(expected.level)
&& line.contains(expected.message)
&& line.contains(&format!("operation=\"{}\"", expected.operation))
&& line.contains(&format!("error_category=\"{}\"", expected.category))
})
}
#[traced_test]
#[test]
fn the_handler_deadline_warning_keeps_its_level_and_operation() {
handler_deadline_exceeded(7);
logs_assert(|lines: &[&str]| {
reports(
lines,
&Expected {
level: "WARN",
message: "streaming handler exceeded the connection wait",
operation: "terminal_body",
category: "reply_abandoned",
},
)
.then_some(())
.ok_or_else(|| "the abandoned reply is not a terminal-body warning".to_owned())
});
}
#[traced_test]
#[test]
fn the_final_chunk_refusal_keeps_its_level_and_operation() {
final_chunk_refused(identity(), SendChunkError::Closed);
logs_assert(|lines: &[&str]| {
reports(
lines,
&Expected {
level: "DEBUG",
message: "final request-body chunk refused",
operation: "chunk_delivery",
category: "abandoned_body",
},
)
.then_some(())
.ok_or_else(|| "the refused final chunk is not a chunk-delivery event".to_owned())
});
}