Skip to content

Commit b2f8685

Browse files
committed
chore: fix all examples and tests
1 parent e7c6b5a commit b2f8685

File tree

7 files changed

+15
-107
lines changed

7 files changed

+15
-107
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ path = "examples/state.rs"
182182
required-features = ["full"]
183183

184184
[[example]]
185-
name = "tower_server"
186-
path = "examples/tower_server.rs"
185+
name = "server"
186+
path = "examples/server.rs"
187187
required-features = ["full"]
188188

189189
[[example]]

examples/tower_server.rs renamed to examples/server.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#![deny(warnings)]
22

33
use std::net::SocketAddr;
4-
use std::task::{Context, Poll};
54

65
use futures_util::future;
76
use hyper::server::conn::Http;
87
use hyper::service::Service;
98
use hyper::{Body, Request, Response};
109
use tokio::net::TcpListener;
11-
use tower::Service;
1210

1311
const ROOT: &str = "/";
1412

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

23-
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
24-
Ok(()).into()
25-
}
26-
2721
fn call(&mut self, req: Request<Body>) -> Self::Future {
2822
let rsp = Response::builder();
2923

examples/service_struct_impl.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ use hyper::server::conn::Http;
22
use hyper::service::Service;
33
use hyper::{Body, Request, Response};
44
use tokio::net::TcpListener;
5-
use tower::Service;
65

76
use std::future::Future;
87
use std::net::SocketAddr;
98
use std::pin::Pin;
10-
use std::task::{Context, Poll};
119

1210
type Counter = i32;
1311

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

44-
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
45-
Poll::Ready(Ok(()))
46-
}
47-
4842
fn call(&mut self, req: Request<Body>) -> Self::Future {
4943
fn mk_response(s: String) -> Result<Response<Body>, hyper::Error> {
5044
Ok(Response::builder().body(Body::from(s)).unwrap())

src/server/conn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,11 @@ impl<E> Http<E> {
579579
/// # use hyper::{Body, Request, Response};
580580
/// # use hyper::server::conn::Http;
581581
/// # use tokio::io::{AsyncRead, AsyncWrite};
582-
/// # use tower::Service;
582+
/// # use hyper::service::Service;
583583
/// # async fn run<I, S>(some_io: I, some_service: S)
584584
/// # where
585585
/// # I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
586-
/// # S: Service<hyper::Request<Body>, Response=hyper::Response<Body>> + Send + 'static,
586+
/// # S: Service<Request<Body>, Response=Response<Body>> + Send + 'static,
587587
/// # S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
588588
/// # S::Future: Send,
589589
/// # {

src/service/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ mod util;
2727

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

3133
pub use self::util::service_fn;

src/service/util.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::fmt;
33
use std::marker::PhantomData;
44

55
use crate::body::HttpBody;
6-
use crate::common::{task, Future, Poll};
6+
use crate::common::Future;
77
use crate::{Request, Response};
8+
use crate::service::service::Service;
89

910
/// Create a `Service` from a function.
1011
///
@@ -41,7 +42,7 @@ pub struct ServiceFn<F, R> {
4142
_req: PhantomData<fn(R)>,
4243
}
4344

44-
impl<F, ReqBody, Ret, ResBody, E> tower_service::Service<crate::Request<ReqBody>>
45+
impl<F, ReqBody, Ret, ResBody, E> Service<Request<ReqBody>>
4546
for ServiceFn<F, ReqBody>
4647
where
4748
F: FnMut(Request<ReqBody>) -> Ret,
@@ -54,10 +55,6 @@ where
5455
type Error = E;
5556
type Future = Ret;
5657

57-
fn poll_ready(&mut self, _cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
58-
Poll::Ready(Ok(()))
59-
}
60-
6158
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
6259
(self.f)(req)
6360
}

tests/server.rs

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use tokio::net::{TcpListener as TkTcpListener, TcpListener, TcpStream as TkTcpSt
2727

2828
use hyper::body::HttpBody;
2929
use hyper::server::conn::Http;
30-
use hyper::service::service_fn;
30+
use hyper::service::{Service, service_fn};
3131
use hyper::{Body, Method, Request, Response, StatusCode, Uri, Version};
3232

3333
mod support;
@@ -2305,77 +2305,6 @@ fn http2_body_user_error_sends_reset_reason() {
23052305
assert_eq!(h2_err.reason(), Some(h2::Reason::INADEQUATE_SECURITY));
23062306
}
23072307

2308-
struct Http2ReadyErrorSvc;
2309-
2310-
impl tower_service::Service<Request<Body>> for Http2ReadyErrorSvc {
2311-
type Response = Response<Body>;
2312-
type Error = h2::Error;
2313-
type Future = Box<
2314-
dyn futures_core::Future<Output = Result<Self::Response, Self::Error>>
2315-
+ Send
2316-
+ Sync
2317-
+ Unpin,
2318-
>;
2319-
2320-
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
2321-
Poll::Ready(Err::<(), _>(h2::Error::from(
2322-
h2::Reason::INADEQUATE_SECURITY,
2323-
)))
2324-
}
2325-
2326-
fn call(&mut self, _: hyper::Request<Body>) -> Self::Future {
2327-
unreachable!("poll_ready error should have shutdown conn");
2328-
}
2329-
}
2330-
2331-
#[tokio::test]
2332-
#[ignore] // sometimes ECONNRESET wins the race
2333-
async fn http2_service_poll_ready_error_sends_goaway() {
2334-
use std::error::Error;
2335-
2336-
let _ = pretty_env_logger::try_init();
2337-
2338-
let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
2339-
.await
2340-
.unwrap();
2341-
2342-
let addr_str = format!("http://{}", listener.local_addr().unwrap());
2343-
2344-
tokio::task::spawn(async move {
2345-
loop {
2346-
tokio::select! {
2347-
res = listener.accept() => {
2348-
let (stream, _) = res.unwrap();
2349-
2350-
tokio::task::spawn(async move {
2351-
let mut http = Http::new();
2352-
http.http2_only(true);
2353-
2354-
let service = Http2ReadyErrorSvc;
2355-
http.serve_connection(stream, service).await.unwrap();
2356-
});
2357-
}
2358-
}
2359-
}
2360-
});
2361-
2362-
let uri = addr_str.parse().expect("server addr should parse");
2363-
let err = dbg!(TestClient::new()
2364-
.http2_only()
2365-
.get(uri)
2366-
.await
2367-
.expect_err("client.get should fail"));
2368-
2369-
// client request should have gotten the specific GOAWAY error...
2370-
let h2_err = err
2371-
.source()
2372-
.expect("source")
2373-
.downcast_ref::<h2::Error>()
2374-
.expect("downcast");
2375-
2376-
assert_eq!(h2_err.reason(), Some(h2::Reason::INADEQUATE_SECURITY));
2377-
}
2378-
23792308
#[test]
23802309
fn skips_content_length_for_304_responses() {
23812310
let server = serve();
@@ -2781,15 +2710,11 @@ enum Msg {
27812710
End,
27822711
}
27832712

2784-
impl tower_service::Service<Request<Body>> for TestService {
2713+
impl Service<Request<Body>> for TestService {
27852714
type Response = Response<ReplyBody>;
27862715
type Error = BoxError;
27872716
type Future = BoxFuture;
27882717

2789-
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2790-
Ok(()).into()
2791-
}
2792-
27932718
fn call(&mut self, mut req: Request<Body>) -> Self::Future {
27942719
let tx = self.tx.clone();
27952720
let replies = self.reply.clone();
@@ -2848,24 +2773,20 @@ const HELLO: &str = "hello";
28482773

28492774
struct HelloWorld;
28502775

2851-
impl tower_service::Service<Request<Body>> for HelloWorld {
2776+
impl Service<Request<Body>> for HelloWorld {
28522777
type Response = Response<Body>;
28532778
type Error = hyper::Error;
28542779
type Future = future::Ready<Result<Response<Body>, Self::Error>>;
28552780

2856-
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2857-
Ok(()).into()
2858-
}
2859-
28602781
fn call(&mut self, _req: Request<Body>) -> Self::Future {
28612782
let response = Response::new(HELLO.into());
28622783
future::ok(response)
28632784
}
28642785
}
28652786

2866-
fn unreachable_service() -> impl tower_service::Service<
2867-
http::Request<hyper::Body>,
2868-
Response = http::Response<ReplyBody>,
2787+
fn unreachable_service() -> impl Service<
2788+
Request<Body>,
2789+
Response = Response<ReplyBody>,
28692790
Error = BoxError,
28702791
Future = BoxFuture,
28712792
> {

0 commit comments

Comments
 (0)