use std::time::Duration;
use super::{test_support::*, *};
use crate::{
app::recovery_codec::{DecodeFailure, DecodedItem},
codec::{CodecError, FramingError},
};
fn oversized_error() -> io::Error {
CodecError::Framing(FramingError::OversizedFrame {
size: 129,
max: 128,
})
.into()
}
fn codec_error_in_returned(error: &io::Error) -> &CodecError {
error
.get_ref()
.and_then(|inner| inner.downcast_ref::<CodecError>())
.expect("the returned error must still carry its typed codec payload")
}
#[test]
fn a_decode_eof_failure_returns_the_original_typed_error() {
let hook = RecordingHook::new(RecoveryPolicy::Drop, Duration::from_millis(5));
let coordinator = RecoveryCoordinator::new(Some(&hook), tight_config());
let failure = DecodeFailure::new(oversized_error(), DecodeOrigin::DecodeEof, 4);
let item = DecodedItem::<u8>::Failure(failure);
let Err(error) = hold_or_end(item, &coordinator) else {
panic!("a decode_eof failure must end the connection");
};
assert_eq!(
error.kind(),
io::ErrorKind::InvalidData,
"the original kind must survive"
);
assert!(
matches!(
codec_error_in_returned(&error),
CodecError::Framing(FramingError::OversizedFrame {
size: 129,
max: 128
})
),
"the typed framing payload must survive, not be rebuilt from display text"
);
assert_eq!(
hook.applied_policies(),
vec![RecoveryPolicy::Disconnect],
"the failure must be reported once, as the disconnect it is"
);
assert_eq!(
hook.consultations(),
0,
"a connection that is already ending must not consult the hook"
);
}
#[test]
fn a_decode_failure_is_held_for_the_next_dispatch() {
let hook = RecordingHook::new(RecoveryPolicy::Drop, Duration::from_millis(5));
let coordinator = RecoveryCoordinator::new(Some(&hook), tight_config());
let item = DecodedItem::<u8>::Failure(protocol_failure());
let held = hold_or_end(item, &coordinator).expect("a decode failure is not terminal");
assert!(
matches!(held, DecodedItem::Failure(_)),
"the failure must be passed on unchanged"
);
assert!(
hook.applied_policies().is_empty(),
"nothing may be reported before the policy is applied"
);
}
#[test]
fn a_frame_is_passed_through() {
let hook = RecordingHook::new(RecoveryPolicy::Drop, Duration::from_millis(5));
let coordinator = RecoveryCoordinator::new(Some(&hook), tight_config());
let held =
hold_or_end(DecodedItem::Frame(7_u8), &coordinator).expect("a frame is not a failure");
assert!(
matches!(held, DecodedItem::Frame(7)),
"a decoded frame must reach the dispatch step unchanged"
);
assert_eq!(hook.applied_policies(), Vec::new());
}