use std::io;
use bytes::Bytes;
use super::clear_deser_failures_after;
use crate::{
app::{Envelope, frame_handling::FrameOutcome},
message_assembler::MessageKey,
request::RequestParts,
};
const STARTING_FAILURES: u32 = 3;
fn failures_after(outcome: &FrameOutcome) -> u32 {
let mut failures = STARTING_FAILURES;
clear_deser_failures_after(outcome, &mut failures);
failures
}
fn assert_count_kept(outcome: &FrameOutcome, reason: &str) {
assert_eq!(failures_after(outcome), STARTING_FAILURES, "{reason}");
}
fn assert_count_cleared(outcome: &FrameOutcome, reason: &str) {
assert_eq!(failures_after(outcome), 0, "{reason}");
}
fn stream_chunk(is_last: bool) -> FrameOutcome {
FrameOutcome::StreamChunk {
key: MessageKey(7),
chunk: Bytes::from_static(b"cd"),
is_last,
}
}
#[test]
fn a_terminal_streaming_failure_keeps_the_deserialization_failure_count() {
assert_count_kept(
&FrameOutcome::StreamFailed {
key: MessageKey(7),
error: io::Error::new(io::ErrorKind::InvalidData, "declared total mismatch"),
},
"the recorded rejection must survive the frame",
);
}
#[test]
fn a_pending_outcome_keeps_the_deserialization_failure_count() {
assert_count_kept(
&FrameOutcome::Pending,
"a partial must not mask earlier failures",
);
}
#[test]
fn an_incomplete_streaming_start_keeps_the_deserialization_failure_count() {
assert_count_kept(
&FrameOutcome::StreamStarted {
key: None,
assembly: None,
parts: RequestParts::new(1, None, Vec::new()),
chunk: Bytes::from_static(b"ab"),
is_last: false,
},
"an unfinished message must not end the failure run",
);
}
#[test]
fn an_incomplete_streaming_chunk_keeps_the_deserialization_failure_count() {
assert_count_kept(
&stream_chunk(false),
"a validated continuation is not a completed message",
);
}
#[test]
fn a_final_streaming_chunk_clears_the_deserialization_failure_count() {
assert_count_cleared(&stream_chunk(true), "the message arrived whole");
}
#[test]
fn a_streaming_fallback_clears_the_deserialization_failure_count() {
assert_count_cleared(
&FrameOutcome::StreamFallback {
parts: RequestParts::new(1, None, Vec::new()),
body: Bytes::from_static(b"whole"),
},
"the assembled message arrived whole",
);
}
#[test]
fn a_discarded_continuation_keeps_the_deserialization_failure_count() {
assert_count_kept(
&FrameOutcome::Discarded,
"discarding a continuation establishes no completion",
);
}
#[test]
fn an_accepted_frame_clears_the_deserialization_failure_count() {
assert_count_cleared(
&FrameOutcome::Buffered(Envelope::new(9, None, Vec::new())),
"a well-formed frame ends the failure run",
);
}