use std::{
io,
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
use futures::Stream;
use tokio_util::sync::CancellationToken;
use super::{
PushBatch,
READ_TIMEOUT_MS,
transport::{TransportRig, blocked_transport},
};
use crate::{
app::{
codec_driver::FramePipeline,
inbound_handler::{
frames::{InboundExpiry, InboundFrames},
push_write::write_frame,
},
recovery_codec::{DecodeFailure, DecodeOrigin, DecodedItem},
},
codec::{CodecError, LengthDelimitedFrameCodec, ProtocolError},
};
fn buffered_failure() -> DecodedItem<Bytes> {
let error = CodecError::Protocol(ProtocolError::UnknownMessageType { type_id: 9 });
DecodedItem::Failure(DecodeFailure::new(error.into(), DecodeOrigin::Decode, 4))
}
struct ItemsThenStop<'a> {
items: std::vec::IntoIter<io::Result<DecodedItem<Bytes>>>,
shutdown: &'a CancellationToken,
}
impl Stream for ItemsThenStop<'_> {
type Item = io::Result<DecodedItem<Bytes>>;
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let next = self.items.next();
if self.items.len() == 0 {
self.shutdown.cancel();
}
Poll::Ready(next)
}
}
fn empty_inbound() -> InboundFrames<DecodedItem<Bytes>, Bytes> {
InboundFrames {
pending: None,
pipeline: FramePipeline::new(None),
message_assembly: None,
read_deadline: None,
evicted_streams: Vec::new(),
}
}
fn failure_then_frame(shutdown: &CancellationToken) -> ItemsThenStop<'_> {
ItemsThenStop {
items: vec![
Ok(buffered_failure()),
Ok(DecodedItem::Frame(Bytes::from_static(b"later frame"))),
]
.into_iter(),
shutdown,
}
}
async fn write_stalled(
harness: &mut TransportRig,
batch: &mut PushBatch<'_, LengthDelimitedFrameCodec, ItemsThenStop<'_>, DecodedItem<Bytes>>,
) -> crate::app::inbound_handler::push_write::FrameWrite {
tokio::time::timeout(
crate::connection::PEER_CLOSED_RESPONSE_GRACE * 2,
write_frame(
&mut harness.transport(),
Bytes::from_static(b"stalled"),
batch,
false,
),
)
.await
.expect("the write must be bounded")
.expect("a shutdown abandonment is not an error")
}
#[rstest::rstest]
#[tokio::test(start_paused = true)]
async fn a_stalled_push_write_keeps_the_first_item_it_read(
#[from(blocked_transport)] mut harness: TransportRig,
) {
let shutdown = CancellationToken::new();
let peer_eof = CancellationToken::new();
let mut inbound = empty_inbound();
let mut reader = failure_then_frame(&shutdown);
let mut evicted = Vec::new();
let mut batch = PushBatch {
frames: Vec::new(),
priority: None,
reader: &mut reader,
pending_frame: &mut inbound.pending,
expiry: InboundExpiry::new(
&mut inbound.message_assembly,
&mut inbound.read_deadline,
READ_TIMEOUT_MS,
&mut evicted,
),
peer_eof: &peer_eof,
shutdown: &shutdown,
};
let written = write_stalled(&mut harness, &mut batch).await;
drop(batch);
assert!(!written.may_continue(), "the stop must end the write");
assert!(
!written.peer_closed(),
"the peer never closed its write half in this case"
);
match inbound.pending {
Some(DecodedItem::Failure(failure)) => {
let (_, origin, consumed) = failure.into_parts();
assert_eq!(
origin,
DecodeOrigin::Decode,
"the buffered item must still be the decoder failure"
);
assert_eq!(
consumed, 4,
"the failure must keep its own consumed-byte count"
);
}
Some(DecodedItem::Frame(frame)) => {
panic!("a later frame overwrote the buffered failure: {frame:?}");
}
None => panic!("the read item must be preserved for recovery"),
}
}