use std::{
io,
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
};
use proptest::prelude::*;
use super::{counting_hooks, test_actor};
use crate::{
app::Envelope,
connection::{FrameSink, PushSink, ResponseOutcome},
response::{Response, WireframeError},
};
type FrameSpec = (u8, u8, Option<u64>);
struct FailingSink {
written: Vec<Envelope>,
fail_at: Option<usize>,
}
impl FailingSink {
const fn writable() -> Self {
Self {
written: Vec::new(),
fail_at: None,
}
}
const fn failing_at(position: usize) -> Self {
Self {
written: Vec::new(),
fail_at: Some(position),
}
}
fn deliver(&mut self, frame: Envelope) -> io::Result<()> {
if self.fail_at == Some(self.written.len()) {
return Err(io::Error::other("sink refused the write"));
}
self.written.push(frame);
Ok(())
}
}
impl FrameSink<Envelope> for FailingSink {
fn send_frame(&mut self, frame: Envelope) -> impl Future<Output = io::Result<()>> + Send {
std::future::ready(self.deliver(frame))
}
}
impl PushSink<Envelope> for FailingSink {
fn send_push(&mut self, frame: Envelope) -> impl Future<Output = io::Result<()>> + Send {
std::future::ready(self.deliver(frame))
}
}
fn frame_spec() -> impl Strategy<Value = FrameSpec> {
(any::<u8>(), any::<u8>(), prop::option::of(any::<u64>()))
}
const fn expected_correlation(
request_correlation: Option<u64>,
own_correlation: Option<u64>,
) -> Option<u64> {
match request_correlation {
Some(correlation) => Some(correlation),
None => own_correlation,
}
}
fn frames_from(specs: &[FrameSpec]) -> Vec<Envelope> {
specs
.iter()
.map(|&(id, payload, correlation)| Envelope::new(u32::from(id), correlation, vec![payload]))
.collect()
}
fn expected_frames(
specs: &[FrameSpec],
request_correlation: Option<u64>,
) -> Vec<(u32, Vec<u8>, Option<u64>)> {
specs
.iter()
.map(|&(id, payload, own_correlation)| {
(
u32::from(id),
vec![payload],
expected_correlation(request_correlation, own_correlation),
)
})
.collect()
}
fn observed(frame: &Envelope) -> (u32, Vec<u8>, Option<u64>) {
(frame.id, frame.payload.clone(), frame.correlation_id)
}
fn test_runtime() -> Result<tokio::runtime::Runtime, TestCaseError> {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|error| TestCaseError::fail(format!("the runtime should build: {error}")))
}
proptest! {
#[test]
fn delivery_preserves_order_and_inherits_correlation(
specs in prop::collection::vec(frame_spec(), 0..8),
request_correlation in prop::option::of(any::<u64>()),
) {
let runtime = test_runtime()?;
let (outcome, delivered, calls) = runtime.block_on(async {
let calls = Arc::new(AtomicUsize::new(0));
let mut actor = test_actor(counting_hooks(Arc::clone(&calls)))
.expect("the actor should build");
let mut sink = FailingSink::writable();
let outcome = actor
.install_response(
Response::Vec(frames_from(&specs)),
request_correlation,
&mut sink,
)
.await
.expect("a writable sink completes the response");
let delivered: Vec<_> = sink.written.iter().map(observed).collect();
(outcome, delivered, calls.load(Ordering::SeqCst))
});
prop_assert_eq!(outcome, ResponseOutcome::Completed);
prop_assert_eq!(
delivered,
expected_frames(&specs, request_correlation),
"every frame must arrive in order with the expected correlation",
);
prop_assert_eq!(
calls,
1,
"a completed response signals command completion exactly once",
);
}
#[test]
fn delivery_stops_at_the_first_sink_failure(
(specs, fail_at) in prop::collection::vec(frame_spec(), 1..8).prop_flat_map(|specs| {
let positions = 0..specs.len();
(Just(specs), positions)
}),
request_correlation in prop::option::of(any::<u64>()),
) {
let runtime = test_runtime()?;
let (is_io_error, delivered, calls) = runtime.block_on(async {
let calls = Arc::new(AtomicUsize::new(0));
let mut actor = test_actor(counting_hooks(Arc::clone(&calls)))
.expect("the actor should build");
let mut sink = FailingSink::failing_at(fail_at);
let result = actor
.install_response(
Response::Vec(frames_from(&specs)),
request_correlation,
&mut sink,
)
.await;
let is_io_error = matches!(result, Err(WireframeError::Io(_)));
let delivered: Vec<_> = sink.written.iter().map(observed).collect();
(is_io_error, delivered, calls.load(Ordering::SeqCst))
});
prop_assert!(is_io_error, "a refused write surfaces as an I/O error");
prop_assert_eq!(
delivered,
expected_frames(&specs, request_correlation)
.into_iter()
.take(fail_at)
.collect::<Vec<_>>(),
"exactly the frames before the refused write are delivered",
);
prop_assert_eq!(
calls,
0,
"a failed response must not signal command completion",
);
}
}