use rstest::rstest;
use super::test_actor;
use crate::{
app::Envelope,
hooks::{CustomProtocolHooks, ProtocolHooks},
response::{FrameStream, Response},
};
const ROUTE: u32 = 4;
const TERMINATOR_ROUTE: u32 = 9;
fn terminating_hooks() -> ProtocolHooks<Envelope, ()> {
CustomProtocolHooks {
stream_end: Some(Box::new(|_ctx| {
Some(Envelope::new(TERMINATOR_ROUTE, None, vec![]))
})),
..CustomProtocolHooks::default()
}
.into()
}
async fn drive_stream(
request_correlation: Option<u64>,
frame_correlation: Option<u64>,
) -> Result<Vec<Envelope>, String> {
let mut actor = test_actor(terminating_hooks()).map_err(|error| error.to_string())?;
let stream: FrameStream<Envelope, ()> = Box::pin(futures::stream::iter(vec![Ok(
Envelope::new(ROUTE, frame_correlation, vec![1]),
)]));
let mut sink = Vec::new();
actor
.install_response(Response::Stream(stream), request_correlation, &mut sink)
.await
.map_err(|error| error.to_string())?;
Ok(sink)
}
#[rstest]
#[case::request_stamps_both(Some(7), None, Some(7), Some(7))]
#[case::request_outranks_the_frame(Some(7), Some(3), Some(7), Some(7))]
#[case::absent_request_keeps_the_frame_id(None, Some(3), Some(3), None)]
#[case::neither_supplies_an_id(None, None, None, None)]
#[tokio::test]
async fn streamed_frames_inherit_the_request_correlation(
#[case] request_correlation: Option<u64>,
#[case] frame_correlation: Option<u64>,
#[case] expected_frame: Option<u64>,
#[case] expected_terminator: Option<u64>,
) -> Result<(), String> {
let frames = drive_stream(request_correlation, frame_correlation).await?;
assert_eq!(
frames.len(),
2,
"the stream and its terminator must be written"
);
let frame = frames
.first()
.expect("the streamed frame should be present");
assert_eq!(
frame.correlation_id, expected_frame,
"the streamed frame must carry the expected identifier"
);
assert_eq!(frame.id, ROUTE, "the frame identity must be preserved");
let terminator = frames.get(1).expect("the terminator should be present");
assert_eq!(
terminator.correlation_id, expected_terminator,
"the terminator must carry the expected identifier"
);
assert_eq!(
terminator.id, TERMINATOR_ROUTE,
"the terminator identity must be preserved"
);
Ok(())
}