use std::sync::{Arc, Mutex};
use super::service_response::{DeferredBody, ServiceResponse};
use crate::{app::Envelope, response::Response};
fn deferred_body() -> DeferredBody {
Arc::new(Mutex::new(Some(Response::Vec(vec![Envelope::new(
1,
None,
vec![9],
)]))))
}
#[test]
fn clones_share_the_deferred_body_and_take_it_once() {
let response = ServiceResponse::with_deferred(Vec::new(), Some(3), deferred_body());
let clone = response.clone();
assert!(
response.take_deferred().is_some(),
"the first consumer takes the body"
);
assert!(
clone.take_deferred().is_none(),
"a clone sees the body already taken"
);
assert!(
response.take_deferred().is_none(),
"the body is taken exactly once"
);
}
#[test]
fn equality_ignores_the_deferred_body() {
let with_body = ServiceResponse::with_deferred(vec![1, 2], Some(3), deferred_body());
let without_body = ServiceResponse::new(vec![1, 2], Some(3));
assert_eq!(
with_body, without_body,
"the deferred body must be excluded from equality"
);
assert_ne!(
with_body,
ServiceResponse::new(vec![1, 2], Some(4)),
"the correlation id must still be compared"
);
}
#[test]
fn debug_reports_body_presence_without_its_contents() {
let with_body = ServiceResponse::with_deferred(Vec::new(), None, deferred_body());
let without_body = ServiceResponse::new(Vec::new(), None);
let rendered = format!("{with_body:?}");
assert!(
rendered.contains("has_deferred_body: true"),
"a present body must be reported: {rendered}"
);
assert!(
!rendered.contains("Envelope"),
"the body's contents must not be rendered: {rendered}"
);
assert!(
format!("{without_body:?}").contains("has_deferred_body: false"),
"an absent body must be reported"
);
}
#[test]
fn frame_mutation_does_not_change_the_deferred_body() {
let mut response = ServiceResponse::with_deferred(Vec::new(), None, deferred_body());
response.frame_mut().push(b'!');
let body = response.take_deferred().expect("the body is still present");
match body {
Response::Vec(frames) => {
assert_eq!(frames.len(), 1, "the body keeps its own frames");
let frame = frames.first().expect("the body holds one frame");
assert_eq!(
frame.payload,
vec![9],
"the streamed frame must be unaffected by a frame edit"
);
}
_ => panic!("unexpected response variant"),
}
}
#[test]
fn a_fresh_response_has_no_deferred_body() {
let response = ServiceResponse::new(vec![1], Some(2));
assert!(
response.take_deferred().is_none(),
"only a response route installs a deferred body"
);
}