use std::{
sync::{Arc, OnceLock},
time::Duration,
};
use tokio::time::timeout;
use tokio_util::sync::CancellationToken;
use wireframe_testing::logger;
use super::test_actor;
use crate::{
app::Envelope,
connection::{ResponseOutcome, UnobservedCause},
hooks::ProtocolHooks,
response::{FrameStream, Response, WireframeError},
};
#[rstest::rstest]
#[case::half_close(UnobservedCause::HalfClose, "a half-close", 9_031)]
#[case::buffered_request(UnobservedCause::BufferedRequest, "a buffered request", 9_032)]
#[tokio::test(start_paused = true)]
async fn an_idle_grace_cancellation_warns_with_the_recorded_cause(
#[case] cause: UnobservedCause,
#[case] expected_cause: &str,
#[case] correlation: u64,
) {
let mut actor = test_actor(ProtocolHooks::default()).expect("actor should build");
actor.set_peer_closed_grace(Duration::from_secs(5));
let peer_closed = CancellationToken::new();
let recorded_cause = Arc::new(OnceLock::new());
recorded_cause
.set(cause)
.expect("the cause cell starts empty");
actor.set_peer_observation(peer_closed.clone(), Arc::clone(&recorded_cause));
let stalled: FrameStream<Envelope, ()> = Box::pin(futures::stream::pending::<
Result<Envelope, WireframeError<()>>,
>());
let mut sink = Vec::new();
let mut log = logger();
log.clear();
peer_closed.cancel();
let outcome = timeout(
Duration::from_secs(10),
actor.install_response(Response::Stream(stalled), Some(correlation), &mut sink),
)
.await
.expect("the idle grace must bound the wait")
.expect("a cancelled response is not an error");
assert_eq!(outcome, ResponseOutcome::Cancelled);
let warnings: Vec<String> = std::iter::from_fn(|| log.pop())
.map(|record| record.args().to_owned())
.collect();
let correlation_field = format!("correlation=Some({correlation})");
let warning = warnings
.iter()
.find(|message| {
message.contains("could not be observed") && message.contains(&correlation_field)
})
.unwrap_or_else(|| panic!("the grace cancellation must warn; captured: {warnings:#?}"));
for expected in [
expected_cause,
correlation_field.as_str(),
"cancelling",
"accepted no frame for 5s",
] {
assert!(
warning.contains(expected),
"the cancellation warning must name {expected}; captured: {warning}"
);
}
assert!(
!warning.contains("cause unknown"),
"the warning must report the recorded cause rather than an unknown one; captured: \
{warning}"
);
}