Skip to content

Commit

Permalink
chore: fix all examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkarw committed Aug 19, 2022
1 parent e7c6b5a commit b2f8685
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 107 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -182,8 +182,8 @@ path = "examples/state.rs"
required-features = ["full"]

[[example]]
name = "tower_server"
path = "examples/tower_server.rs"
name = "server"
path = "examples/server.rs"
required-features = ["full"]

[[example]]
Expand Down
6 changes: 0 additions & 6 deletions examples/tower_server.rs → examples/server.rs
@@ -1,14 +1,12 @@
#![deny(warnings)]

use std::net::SocketAddr;
use std::task::{Context, Poll};

use futures_util::future;
use hyper::server::conn::Http;
use hyper::service::Service;
use hyper::{Body, Request, Response};
use tokio::net::TcpListener;
use tower::Service;

const ROOT: &str = "/";

Expand All @@ -20,10 +18,6 @@ impl Service<Request<Body>> for Svc {
type Error = hyper::Error;
type Future = future::Ready<Result<Self::Response, Self::Error>>;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ok(()).into()
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
let rsp = Response::builder();

Expand Down
6 changes: 0 additions & 6 deletions examples/service_struct_impl.rs
Expand Up @@ -2,12 +2,10 @@ use hyper::server::conn::Http;
use hyper::service::Service;
use hyper::{Body, Request, Response};
use tokio::net::TcpListener;
use tower::Service;

use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::task::{Context, Poll};

type Counter = i32;

Expand Down Expand Up @@ -41,10 +39,6 @@ impl Service<Request<Body>> for Svc {
type Error = hyper::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
fn mk_response(s: String) -> Result<Response<Body>, hyper::Error> {
Ok(Response::builder().body(Body::from(s)).unwrap())
Expand Down
4 changes: 2 additions & 2 deletions src/server/conn.rs
Expand Up @@ -579,11 +579,11 @@ impl<E> Http<E> {
/// # use hyper::{Body, Request, Response};
/// # use hyper::server::conn::Http;
/// # use tokio::io::{AsyncRead, AsyncWrite};
/// # use tower::Service;
/// # use hyper::service::Service;
/// # async fn run<I, S>(some_io: I, some_service: S)
/// # where
/// # I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
/// # S: Service<hyper::Request<Body>, Response=hyper::Response<Body>> + Send + 'static,
/// # S: Service<Request<Body>, Response=Response<Body>> + Send + 'static,
/// # S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
/// # S::Future: Send,
/// # {
Expand Down
2 changes: 2 additions & 0 deletions src/service/mod.rs
Expand Up @@ -27,5 +27,7 @@ mod util;

#[cfg(all(any(feature = "http1", feature = "http2"), feature = "server"))]
pub(super) use self::http::HttpService;
#[cfg(all(any(feature = "http1", feature = "http2"), feature = "server"))]
pub use self::service::Service;

pub use self::util::service_fn;
9 changes: 3 additions & 6 deletions src/service/util.rs
Expand Up @@ -3,8 +3,9 @@ use std::fmt;
use std::marker::PhantomData;

use crate::body::HttpBody;
use crate::common::{task, Future, Poll};
use crate::common::Future;
use crate::{Request, Response};
use crate::service::service::Service;

/// Create a `Service` from a function.
///
Expand Down Expand Up @@ -41,7 +42,7 @@ pub struct ServiceFn<F, R> {
_req: PhantomData<fn(R)>,
}

impl<F, ReqBody, Ret, ResBody, E> tower_service::Service<crate::Request<ReqBody>>
impl<F, ReqBody, Ret, ResBody, E> Service<Request<ReqBody>>
for ServiceFn<F, ReqBody>
where
F: FnMut(Request<ReqBody>) -> Ret,
Expand All @@ -54,10 +55,6 @@ where
type Error = E;
type Future = Ret;

fn poll_ready(&mut self, _cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
(self.f)(req)
}
Expand Down
91 changes: 6 additions & 85 deletions tests/server.rs
Expand Up @@ -27,7 +27,7 @@ use tokio::net::{TcpListener as TkTcpListener, TcpListener, TcpStream as TkTcpSt

use hyper::body::HttpBody;
use hyper::server::conn::Http;
use hyper::service::service_fn;
use hyper::service::{Service, service_fn};
use hyper::{Body, Method, Request, Response, StatusCode, Uri, Version};

mod support;
Expand Down Expand Up @@ -2305,77 +2305,6 @@ fn http2_body_user_error_sends_reset_reason() {
assert_eq!(h2_err.reason(), Some(h2::Reason::INADEQUATE_SECURITY));
}

struct Http2ReadyErrorSvc;

impl tower_service::Service<Request<Body>> for Http2ReadyErrorSvc {
type Response = Response<Body>;
type Error = h2::Error;
type Future = Box<
dyn futures_core::Future<Output = Result<Self::Response, Self::Error>>
+ Send
+ Sync
+ Unpin,
>;

fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Err::<(), _>(h2::Error::from(
h2::Reason::INADEQUATE_SECURITY,
)))
}

fn call(&mut self, _: hyper::Request<Body>) -> Self::Future {
unreachable!("poll_ready error should have shutdown conn");
}
}

#[tokio::test]
#[ignore] // sometimes ECONNRESET wins the race
async fn http2_service_poll_ready_error_sends_goaway() {
use std::error::Error;

let _ = pretty_env_logger::try_init();

let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
.await
.unwrap();

let addr_str = format!("http://{}", listener.local_addr().unwrap());

tokio::task::spawn(async move {
loop {
tokio::select! {
res = listener.accept() => {
let (stream, _) = res.unwrap();

tokio::task::spawn(async move {
let mut http = Http::new();
http.http2_only(true);

let service = Http2ReadyErrorSvc;
http.serve_connection(stream, service).await.unwrap();
});
}
}
}
});

let uri = addr_str.parse().expect("server addr should parse");
let err = dbg!(TestClient::new()
.http2_only()
.get(uri)
.await
.expect_err("client.get should fail"));

// client request should have gotten the specific GOAWAY error...
let h2_err = err
.source()
.expect("source")
.downcast_ref::<h2::Error>()
.expect("downcast");

assert_eq!(h2_err.reason(), Some(h2::Reason::INADEQUATE_SECURITY));
}

#[test]
fn skips_content_length_for_304_responses() {
let server = serve();
Expand Down Expand Up @@ -2781,15 +2710,11 @@ enum Msg {
End,
}

impl tower_service::Service<Request<Body>> for TestService {
impl Service<Request<Body>> for TestService {
type Response = Response<ReplyBody>;
type Error = BoxError;
type Future = BoxFuture;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ok(()).into()
}

fn call(&mut self, mut req: Request<Body>) -> Self::Future {
let tx = self.tx.clone();
let replies = self.reply.clone();
Expand Down Expand Up @@ -2848,24 +2773,20 @@ const HELLO: &str = "hello";

struct HelloWorld;

impl tower_service::Service<Request<Body>> for HelloWorld {
impl Service<Request<Body>> for HelloWorld {
type Response = Response<Body>;
type Error = hyper::Error;
type Future = future::Ready<Result<Response<Body>, Self::Error>>;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ok(()).into()
}

fn call(&mut self, _req: Request<Body>) -> Self::Future {
let response = Response::new(HELLO.into());
future::ok(response)
}
}

fn unreachable_service() -> impl tower_service::Service<
http::Request<hyper::Body>,
Response = http::Response<ReplyBody>,
fn unreachable_service() -> impl Service<
Request<Body>,
Response = Response<ReplyBody>,
Error = BoxError,
Future = BoxFuture,
> {
Expand Down

0 comments on commit b2f8685

Please sign in to comment.