Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
+193
@@ -0,0 +1,193 @@
|
||||
use crate::BoxError;
|
||||
use http_body::Body;
|
||||
use pin_project_lite::pin_project;
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{ready, Context, Poll},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::time::{sleep, Sleep};
|
||||
|
||||
pin_project! {
|
||||
/// Middleware that applies a timeout to request and response bodies.
|
||||
///
|
||||
/// Wrapper around a [`Body`][`http_body::Body`] to time out if data is not ready within the specified duration.
|
||||
/// The timeout is enforced between consecutive [`Frame`][`http_body::Frame`] polls, and it
|
||||
/// resets after each poll.
|
||||
/// The total time to produce a [`Body`][`http_body::Body`] could exceed the timeout duration without
|
||||
/// timing out, as long as no single interval between polls exceeds the timeout.
|
||||
///
|
||||
/// If the [`Body`][`http_body::Body`] does not produce a requested data frame within the timeout period, it will return a [`TimeoutError`].
|
||||
///
|
||||
/// # Differences from [`Timeout`][crate::timeout::Timeout]
|
||||
///
|
||||
/// [`Timeout`][crate::timeout::Timeout] applies a timeout to the request future, not body.
|
||||
/// That timeout is not reset when bytes are handled, whether the request is active or not.
|
||||
/// Bodies are handled asynchronously outside of the tower stack's future and thus needs an additional timeout.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use http::{Request, Response};
|
||||
/// use bytes::Bytes;
|
||||
/// use http_body_util::Full;
|
||||
/// use std::time::Duration;
|
||||
/// use tower::ServiceBuilder;
|
||||
/// use tower_http::timeout::RequestBodyTimeoutLayer;
|
||||
///
|
||||
/// async fn handle(_: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, std::convert::Infallible> {
|
||||
/// // ...
|
||||
/// # todo!()
|
||||
/// }
|
||||
///
|
||||
/// # #[tokio::main]
|
||||
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// let svc = ServiceBuilder::new()
|
||||
/// // Timeout bodies after 30 seconds of inactivity
|
||||
/// .layer(RequestBodyTimeoutLayer::new(Duration::from_secs(30)))
|
||||
/// .service_fn(handle);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub struct TimeoutBody<B> {
|
||||
timeout: Duration,
|
||||
#[pin]
|
||||
sleep: Option<Sleep>,
|
||||
#[pin]
|
||||
body: B,
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> TimeoutBody<B> {
|
||||
/// Creates a new [`TimeoutBody`].
|
||||
pub fn new(timeout: Duration, body: B) -> Self {
|
||||
TimeoutBody {
|
||||
timeout,
|
||||
sleep: None,
|
||||
body,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> Body for TimeoutBody<B>
|
||||
where
|
||||
B: Body,
|
||||
B::Error: Into<BoxError>,
|
||||
{
|
||||
type Data = B::Data;
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
|
||||
let mut this = self.project();
|
||||
|
||||
// Start the `Sleep` if not active.
|
||||
let sleep_pinned = if let Some(some) = this.sleep.as_mut().as_pin_mut() {
|
||||
some
|
||||
} else {
|
||||
this.sleep.set(Some(sleep(*this.timeout)));
|
||||
this.sleep.as_mut().as_pin_mut().unwrap()
|
||||
};
|
||||
|
||||
// Error if the timeout has expired.
|
||||
if let Poll::Ready(()) = sleep_pinned.poll(cx) {
|
||||
return Poll::Ready(Some(Err(Box::new(TimeoutError(())))));
|
||||
}
|
||||
|
||||
// Check for body data.
|
||||
let frame = ready!(this.body.poll_frame(cx));
|
||||
// A frame is ready. Reset the `Sleep`...
|
||||
this.sleep.set(None);
|
||||
|
||||
Poll::Ready(frame.transpose().map_err(Into::into).transpose())
|
||||
}
|
||||
}
|
||||
|
||||
/// Error for [`TimeoutBody`].
|
||||
#[derive(Debug)]
|
||||
pub struct TimeoutError(());
|
||||
|
||||
impl std::error::Error for TimeoutError {}
|
||||
|
||||
impl std::fmt::Display for TimeoutError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "data was not received within the designated timeout")
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use bytes::Bytes;
|
||||
use http_body::Frame;
|
||||
use http_body_util::BodyExt;
|
||||
use pin_project_lite::pin_project;
|
||||
use std::{error::Error, fmt::Display};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MockError;
|
||||
|
||||
impl Error for MockError {}
|
||||
|
||||
impl Display for MockError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "mock error")
|
||||
}
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
struct MockBody {
|
||||
#[pin]
|
||||
sleep: Sleep
|
||||
}
|
||||
}
|
||||
|
||||
impl Body for MockBody {
|
||||
type Data = Bytes;
|
||||
type Error = MockError;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
|
||||
let this = self.project();
|
||||
this.sleep
|
||||
.poll(cx)
|
||||
.map(|_| Some(Ok(Frame::data(vec![].into()))))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_body_available_within_timeout() {
|
||||
let mock_sleep = Duration::from_secs(1);
|
||||
let timeout_sleep = Duration::from_secs(2);
|
||||
|
||||
let mock_body = MockBody {
|
||||
sleep: sleep(mock_sleep),
|
||||
};
|
||||
let timeout_body = TimeoutBody::new(timeout_sleep, mock_body);
|
||||
|
||||
assert!(timeout_body
|
||||
.boxed()
|
||||
.frame()
|
||||
.await
|
||||
.expect("no frame")
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_body_unavailable_within_timeout_error() {
|
||||
let mock_sleep = Duration::from_secs(2);
|
||||
let timeout_sleep = Duration::from_secs(1);
|
||||
|
||||
let mock_body = MockBody {
|
||||
sleep: sleep(mock_sleep),
|
||||
};
|
||||
let timeout_body = TimeoutBody::new(timeout_sleep, mock_body);
|
||||
|
||||
assert!(timeout_body.boxed().frame().await.unwrap().is_err());
|
||||
}
|
||||
}
|
||||
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
//! Middleware that applies a timeout to requests.
|
||||
//!
|
||||
//! If the request does not complete within the specified timeout, it will be aborted and a
|
||||
//! response with an empty body and a custom status code will be returned.
|
||||
//!
|
||||
//! # Differences from `tower::timeout`
|
||||
//!
|
||||
//! tower's [`Timeout`](tower::timeout::Timeout) middleware uses an error to signal timeout, i.e.
|
||||
//! it changes the error type to [`BoxError`](tower::BoxError). For HTTP services that is rarely
|
||||
//! what you want as returning errors will terminate the connection without sending a response.
|
||||
//!
|
||||
//! This middleware won't change the error type and instead returns a response with an empty body
|
||||
//! and the specified status code. That means if your service's error type is [`Infallible`], it will
|
||||
//! still be [`Infallible`] after applying this middleware.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```
|
||||
//! use http::{Request, Response, StatusCode};
|
||||
//! use http_body_util::Full;
|
||||
//! use bytes::Bytes;
|
||||
//! use std::{convert::Infallible, time::Duration};
|
||||
//! use tower::ServiceBuilder;
|
||||
//! use tower_http::timeout::TimeoutLayer;
|
||||
//!
|
||||
//! async fn handle(_: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||
//! // ...
|
||||
//! # Ok(Response::new(Full::default()))
|
||||
//! }
|
||||
//!
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! let svc = ServiceBuilder::new()
|
||||
//! // Timeout requests after 30 seconds with the specified status code
|
||||
//! .layer(TimeoutLayer::with_status_code(StatusCode::REQUEST_TIMEOUT, Duration::from_secs(30)))
|
||||
//! .service_fn(handle);
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! [`Infallible`]: std::convert::Infallible
|
||||
|
||||
mod body;
|
||||
mod service;
|
||||
|
||||
pub use body::{TimeoutBody, TimeoutError};
|
||||
pub use service::{
|
||||
RequestBodyTimeout, RequestBodyTimeoutLayer, ResponseBodyTimeout, ResponseBodyTimeoutLayer,
|
||||
Timeout, TimeoutLayer,
|
||||
};
|
||||
+396
@@ -0,0 +1,396 @@
|
||||
use crate::timeout::body::TimeoutBody;
|
||||
use http::{Request, Response, StatusCode};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{ready, Context, Poll},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::time::Sleep;
|
||||
use tower_layer::Layer;
|
||||
use tower_service::Service;
|
||||
|
||||
/// Layer that applies the [`Timeout`] middleware which apply a timeout to requests.
|
||||
///
|
||||
/// See the [module docs](super) for an example.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TimeoutLayer {
|
||||
timeout: Duration,
|
||||
status_code: StatusCode,
|
||||
}
|
||||
|
||||
impl TimeoutLayer {
|
||||
/// Creates a new [`TimeoutLayer`].
|
||||
///
|
||||
/// By default, it will return a `408 Request Timeout` response if the request does not complete within the specified timeout.
|
||||
/// To customize the response status code, use the `with_status_code` method.
|
||||
#[deprecated(since = "0.6.7", note = "Use `TimeoutLayer::with_status_code` instead")]
|
||||
pub fn new(timeout: Duration) -> Self {
|
||||
Self::with_status_code(StatusCode::REQUEST_TIMEOUT, timeout)
|
||||
}
|
||||
|
||||
/// Creates a new [`TimeoutLayer`] with the specified status code for the timeout response.
|
||||
pub fn with_status_code(status_code: StatusCode, timeout: Duration) -> Self {
|
||||
Self {
|
||||
timeout,
|
||||
status_code,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Layer<S> for TimeoutLayer {
|
||||
type Service = Timeout<S>;
|
||||
|
||||
fn layer(&self, inner: S) -> Self::Service {
|
||||
Timeout::with_status_code(inner, self.status_code, self.timeout)
|
||||
}
|
||||
}
|
||||
|
||||
/// Middleware which apply a timeout to requests.
|
||||
///
|
||||
/// See the [module docs](super) for an example.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Timeout<S> {
|
||||
inner: S,
|
||||
timeout: Duration,
|
||||
status_code: StatusCode,
|
||||
}
|
||||
|
||||
impl<S> Timeout<S> {
|
||||
/// Creates a new [`Timeout`].
|
||||
///
|
||||
/// By default, it will return a `408 Request Timeout` response if the request does not complete within the specified timeout.
|
||||
/// To customize the response status code, use the `with_status_code` method.
|
||||
#[deprecated(since = "0.6.7", note = "Use `Timeout::with_status_code` instead")]
|
||||
pub fn new(inner: S, timeout: Duration) -> Self {
|
||||
Self::with_status_code(inner, StatusCode::REQUEST_TIMEOUT, timeout)
|
||||
}
|
||||
|
||||
/// Creates a new [`Timeout`] with the specified status code for the timeout response.
|
||||
pub fn with_status_code(inner: S, status_code: StatusCode, timeout: Duration) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
timeout,
|
||||
status_code,
|
||||
}
|
||||
}
|
||||
|
||||
define_inner_service_accessors!();
|
||||
|
||||
/// Returns a new [`Layer`] that wraps services with a `Timeout` middleware.
|
||||
///
|
||||
/// [`Layer`]: tower_layer::Layer
|
||||
#[deprecated(
|
||||
since = "0.6.7",
|
||||
note = "Use `Timeout::layer_with_status_code` instead"
|
||||
)]
|
||||
pub fn layer(timeout: Duration) -> TimeoutLayer {
|
||||
TimeoutLayer::with_status_code(StatusCode::REQUEST_TIMEOUT, timeout)
|
||||
}
|
||||
|
||||
/// Returns a new [`Layer`] that wraps services with a `Timeout` middleware with the specified status code.
|
||||
pub fn layer_with_status_code(status_code: StatusCode, timeout: Duration) -> TimeoutLayer {
|
||||
TimeoutLayer::with_status_code(status_code, timeout)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for Timeout<S>
|
||||
where
|
||||
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
|
||||
ResBody: Default,
|
||||
{
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = ResponseFuture<S::Future>;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.inner.poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
|
||||
let sleep = tokio::time::sleep(self.timeout);
|
||||
ResponseFuture {
|
||||
inner: self.inner.call(req),
|
||||
sleep,
|
||||
status_code: self.status_code,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
/// Response future for [`Timeout`].
|
||||
pub struct ResponseFuture<F> {
|
||||
#[pin]
|
||||
inner: F,
|
||||
#[pin]
|
||||
sleep: Sleep,
|
||||
status_code: StatusCode,
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, B, E> Future for ResponseFuture<F>
|
||||
where
|
||||
F: Future<Output = Result<Response<B>, E>>,
|
||||
B: Default,
|
||||
{
|
||||
type Output = Result<Response<B>, E>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
|
||||
if this.sleep.poll(cx).is_ready() {
|
||||
let mut res = Response::new(B::default());
|
||||
*res.status_mut() = *this.status_code;
|
||||
return Poll::Ready(Ok(res));
|
||||
}
|
||||
|
||||
this.inner.poll(cx)
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies a [`TimeoutBody`] to the request body.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RequestBodyTimeoutLayer {
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl RequestBodyTimeoutLayer {
|
||||
/// Creates a new [`RequestBodyTimeoutLayer`].
|
||||
pub fn new(timeout: Duration) -> Self {
|
||||
Self { timeout }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Layer<S> for RequestBodyTimeoutLayer {
|
||||
type Service = RequestBodyTimeout<S>;
|
||||
|
||||
fn layer(&self, inner: S) -> Self::Service {
|
||||
RequestBodyTimeout::new(inner, self.timeout)
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies a [`TimeoutBody`] to the request body.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RequestBodyTimeout<S> {
|
||||
inner: S,
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl<S> RequestBodyTimeout<S> {
|
||||
/// Creates a new [`RequestBodyTimeout`].
|
||||
pub fn new(service: S, timeout: Duration) -> Self {
|
||||
Self {
|
||||
inner: service,
|
||||
timeout,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a new [`Layer`] that wraps services with a [`RequestBodyTimeoutLayer`] middleware.
|
||||
///
|
||||
/// [`Layer`]: tower_layer::Layer
|
||||
pub fn layer(timeout: Duration) -> RequestBodyTimeoutLayer {
|
||||
RequestBodyTimeoutLayer::new(timeout)
|
||||
}
|
||||
|
||||
define_inner_service_accessors!();
|
||||
}
|
||||
|
||||
impl<S, ReqBody> Service<Request<ReqBody>> for RequestBodyTimeout<S>
|
||||
where
|
||||
S: Service<Request<TimeoutBody<ReqBody>>>,
|
||||
{
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.inner.poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
|
||||
let req = req.map(|body| TimeoutBody::new(self.timeout, body));
|
||||
self.inner.call(req)
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies a [`TimeoutBody`] to the response body.
|
||||
#[derive(Clone)]
|
||||
pub struct ResponseBodyTimeoutLayer {
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl ResponseBodyTimeoutLayer {
|
||||
/// Creates a new [`ResponseBodyTimeoutLayer`].
|
||||
pub fn new(timeout: Duration) -> Self {
|
||||
Self { timeout }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Layer<S> for ResponseBodyTimeoutLayer {
|
||||
type Service = ResponseBodyTimeout<S>;
|
||||
|
||||
fn layer(&self, inner: S) -> Self::Service {
|
||||
ResponseBodyTimeout::new(inner, self.timeout)
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies a [`TimeoutBody`] to the response body.
|
||||
#[derive(Clone)]
|
||||
pub struct ResponseBodyTimeout<S> {
|
||||
inner: S,
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for ResponseBodyTimeout<S>
|
||||
where
|
||||
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
|
||||
{
|
||||
type Response = Response<TimeoutBody<ResBody>>;
|
||||
type Error = S::Error;
|
||||
type Future = ResponseBodyTimeoutFuture<S::Future>;
|
||||
|
||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.inner.poll_ready(cx)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
|
||||
ResponseBodyTimeoutFuture {
|
||||
inner: self.inner.call(req),
|
||||
timeout: self.timeout,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ResponseBodyTimeout<S> {
|
||||
/// Creates a new [`ResponseBodyTimeout`].
|
||||
pub fn new(service: S, timeout: Duration) -> Self {
|
||||
Self {
|
||||
inner: service,
|
||||
timeout,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a new [`Layer`] that wraps services with a [`ResponseBodyTimeoutLayer`] middleware.
|
||||
///
|
||||
/// [`Layer`]: tower_layer::Layer
|
||||
pub fn layer(timeout: Duration) -> ResponseBodyTimeoutLayer {
|
||||
ResponseBodyTimeoutLayer::new(timeout)
|
||||
}
|
||||
|
||||
define_inner_service_accessors!();
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
/// Response future for [`ResponseBodyTimeout`].
|
||||
pub struct ResponseBodyTimeoutFuture<Fut> {
|
||||
#[pin]
|
||||
inner: Fut,
|
||||
timeout: Duration,
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut, ResBody, E> Future for ResponseBodyTimeoutFuture<Fut>
|
||||
where
|
||||
Fut: Future<Output = Result<Response<ResBody>, E>>,
|
||||
{
|
||||
type Output = Result<Response<TimeoutBody<ResBody>>, E>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let timeout = self.timeout;
|
||||
let this = self.project();
|
||||
let res = ready!(this.inner.poll(cx))?;
|
||||
Poll::Ready(Ok(res.map(|body| TimeoutBody::new(timeout, body))))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_helpers::Body;
|
||||
use http::{Request, Response, StatusCode};
|
||||
use std::time::Duration;
|
||||
use tower::{BoxError, ServiceBuilder, ServiceExt};
|
||||
|
||||
#[tokio::test]
|
||||
async fn request_completes_within_timeout() {
|
||||
let mut service = ServiceBuilder::new()
|
||||
.layer(TimeoutLayer::with_status_code(
|
||||
StatusCode::GATEWAY_TIMEOUT,
|
||||
Duration::from_secs(1),
|
||||
))
|
||||
.service_fn(fast_handler);
|
||||
|
||||
let request = Request::get("/").body(Body::empty()).unwrap();
|
||||
let res = service.ready().await.unwrap().call(request).await.unwrap();
|
||||
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn timeout_middleware_with_custom_status_code() {
|
||||
let timeout_service = Timeout::with_status_code(
|
||||
tower::service_fn(slow_handler),
|
||||
StatusCode::REQUEST_TIMEOUT,
|
||||
Duration::from_millis(10),
|
||||
);
|
||||
|
||||
let mut service = ServiceBuilder::new().service(timeout_service);
|
||||
|
||||
let request = Request::get("/").body(Body::empty()).unwrap();
|
||||
let res = service.ready().await.unwrap().call(request).await.unwrap();
|
||||
|
||||
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn timeout_response_has_empty_body() {
|
||||
let mut service = ServiceBuilder::new()
|
||||
.layer(TimeoutLayer::with_status_code(
|
||||
StatusCode::GATEWAY_TIMEOUT,
|
||||
Duration::from_millis(10),
|
||||
))
|
||||
.service_fn(slow_handler);
|
||||
|
||||
let request = Request::get("/").body(Body::empty()).unwrap();
|
||||
let res = service.ready().await.unwrap().call(request).await.unwrap();
|
||||
|
||||
assert_eq!(res.status(), StatusCode::GATEWAY_TIMEOUT);
|
||||
|
||||
// Verify the body is empty (default)
|
||||
use http_body_util::BodyExt;
|
||||
let body = res.into_body();
|
||||
let bytes = body.collect().await.unwrap().to_bytes();
|
||||
assert!(bytes.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn deprecated_new_method_compatibility() {
|
||||
#[allow(deprecated)]
|
||||
let layer = TimeoutLayer::new(Duration::from_millis(10));
|
||||
|
||||
let mut service = ServiceBuilder::new().layer(layer).service_fn(slow_handler);
|
||||
|
||||
let request = Request::get("/").body(Body::empty()).unwrap();
|
||||
let res = service.ready().await.unwrap().call(request).await.unwrap();
|
||||
|
||||
// Should use default 408 status code
|
||||
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
|
||||
}
|
||||
|
||||
async fn slow_handler(_req: Request<Body>) -> Result<Response<Body>, BoxError> {
|
||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(Body::empty())
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
async fn fast_handler(_req: Request<Body>) -> Result<Response<Body>, BoxError> {
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(Body::empty())
|
||||
.unwrap())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user