use std::{
future::Future,
io,
sync::{Arc, Mutex, PoisonError},
};
use rstest::rstest;
use tokio::sync::mpsc;
use super::test_actor;
use crate::{
app::Envelope,
connection::{FrameSink, PushSink},
hooks::{CustomProtocolHooks, ProtocolHooks},
response::{FrameStream, Response},
};
struct RecordingSink {
events: Arc<Mutex<Vec<&'static str>>>,
}
impl FrameSink<Envelope> for RecordingSink {
fn send_frame(&mut self, _frame: Envelope) -> impl Future<Output = io::Result<()>> + Send {
self.events
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push("write");
std::future::ready(Ok(()))
}
}
impl PushSink<Envelope> for RecordingSink {
fn send_push(&mut self, _frame: Envelope) -> impl Future<Output = io::Result<()>> + Send {
self.events
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push("write");
std::future::ready(Ok(()))
}
}
struct FailingSink;
impl FrameSink<Envelope> for FailingSink {
fn send_frame(&mut self, _frame: Envelope) -> impl Future<Output = io::Result<()>> + Send {
std::future::ready(Err(io::Error::other("peer lost")))
}
}
impl PushSink<Envelope> for FailingSink {
fn send_push(&mut self, _frame: Envelope) -> impl Future<Output = io::Result<()>> + Send {
std::future::ready(Err(io::Error::other("peer lost")))
}
}
fn ordering_hooks(events: Arc<Mutex<Vec<&'static str>>>) -> ProtocolHooks<Envelope, ()> {
CustomProtocolHooks {
on_command_end: Some(Box::new(move |_ctx| {
events
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push("hook");
})),
stream_end: Some(Box::new(|_ctx| Some(Envelope::new(9, None, vec![])))),
..CustomProtocolHooks::default()
}
.into()
}
fn recorded_events(events: &Arc<Mutex<Vec<&'static str>>>) -> Vec<&'static str> {
events
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone()
}
async fn draining_response(is_streaming: bool) -> io::Result<Response<Envelope, ()>> {
if is_streaming {
let stream: FrameStream<Envelope, ()> = Box::pin(futures::stream::iter(vec![Ok(
Envelope::new(1, None, vec![1]),
)]));
return Ok(Response::Stream(stream));
}
let (sender, receiver) = mpsc::channel(4);
sender
.send(Envelope::new(1, None, vec![1]))
.await
.map_err(|error| io::Error::other(format!("channel should accept a frame: {error}")))?;
drop(sender);
Ok(Response::MultiPacket(receiver))
}
#[rstest]
#[case::stream(true)]
#[case::multi_packet(false)]
#[tokio::test]
async fn completion_hook_runs_after_the_terminator_is_written(#[case] is_streaming: bool) {
let events = Arc::new(Mutex::new(Vec::new()));
let mut actor = test_actor(ordering_hooks(Arc::clone(&events))).expect("actor should build");
let mut sink = RecordingSink {
events: Arc::clone(&events),
};
actor
.install_response(
draining_response(is_streaming)
.await
.expect("channel should accept a frame"),
None,
&mut sink,
)
.await
.expect("response should drive to completion");
assert_eq!(
recorded_events(&events),
vec!["write", "write", "hook"],
"the completion hook must run only after the terminator write"
);
}
#[tokio::test]
async fn completion_hook_is_skipped_when_the_terminator_write_fails() {
let events = Arc::new(Mutex::new(Vec::new()));
let mut actor = test_actor(ordering_hooks(Arc::clone(&events))).expect("actor should build");
let stream: FrameStream<Envelope, ()> = Box::pin(futures::stream::empty());
let mut sink = FailingSink;
actor
.install_response(Response::Stream(stream), None, &mut sink)
.await
.expect_err("a failed terminator write must be reported");
assert!(
recorded_events(&events).is_empty(),
"a failed write must not report command completion"
);
}