use std::convert::Infallible;
use crate::{
app::Envelope,
middleware::{HandlerService, Service, ServiceRequest, ServiceResponse},
};
const PAYLOAD_BYTES: usize = 64 * 1024;
const FILL: u8 = 0xab;
#[derive(Clone, Copy)]
struct EchoService;
#[async_trait::async_trait]
impl Service for EchoService {
type Error = Infallible;
async fn call(&self, request: ServiceRequest) -> Result<ServiceResponse, Self::Error> {
let correlation_id = request.correlation_id();
Ok(ServiceResponse::new(request.into_inner(), correlation_id))
}
}
#[tokio::test]
async fn the_handler_service_hands_back_the_envelope_buffer() {
let payload = vec![FILL; PAYLOAD_BYTES];
let arrival = payload.as_ptr();
let request = ServiceRequest::new(payload, Some(3));
assert_eq!(
request.frame().as_ptr(),
arrival,
"building a request from the envelope payload must not copy it"
);
let service = HandlerService::<Envelope>::from_service(7, EchoService);
let response = service
.call(request)
.await
.expect("the echo service is infallible");
assert_eq!(
response.correlation_id(),
Some(3),
"the request's correlation identifier must survive dispatch"
);
assert_eq!(
response.frame().as_ptr(),
arrival,
"dispatching through the handler service must not copy the payload"
);
let returned = response.into_inner();
assert_eq!(
returned.len(),
PAYLOAD_BYTES,
"the handler must receive the whole payload"
);
assert!(
returned.iter().all(|byte| *byte == FILL),
"the handler must receive the payload unchanged"
);
}