Repository navigation
feat(client): decouple tokio/net and mio from client-legacy #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //! Integration test verifying that `client-legacy` can be compiled and used with | ||
| //! a custom connector (e.g. for custom in-memory streams, TLS, or non-TCP platforms | ||
| //! like WASM and Fuchsia) without enabling the `tcp` feature or pulling in `mio`. | ||
|
|
||
| #![cfg(all( | ||
| feature = "client-legacy", | ||
| feature = "http1", | ||
| feature = "tokio", | ||
| not(feature = "tcp") | ||
| ))] | ||
|
|
||
| use std::future::Future; | ||
| use std::pin::Pin; | ||
| use std::sync::{Arc, Mutex}; | ||
| use std::task::{Context, Poll}; | ||
|
|
||
| use bytes::Bytes; | ||
| use http::{Request, StatusCode, Uri}; | ||
| use http_body_util::{BodyExt, Empty}; | ||
| use hyper::rt::{Read, ReadBufCursor, Write}; | ||
| use hyper_util::client::legacy::Client; | ||
| use hyper_util::client::legacy::connect::{Connected, Connection}; | ||
| use hyper_util::rt::{TokioExecutor, TokioIo}; | ||
| use tower_service::Service; | ||
|
|
||
| struct MockStream(TokioIo<tokio_test::io::Mock>); | ||
|
|
||
| impl Read for MockStream { | ||
| fn poll_read( | ||
| mut self: Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| buf: ReadBufCursor<'_>, | ||
| ) -> Poll<std::io::Result<()>> { | ||
| Pin::new(&mut self.0).poll_read(cx, buf) | ||
| } | ||
| } | ||
|
|
||
| impl Write for MockStream { | ||
| fn poll_write( | ||
| mut self: Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| buf: &[u8], | ||
| ) -> Poll<Result<usize, std::io::Error>> { | ||
| Pin::new(&mut self.0).poll_write(cx, buf) | ||
| } | ||
|
|
||
| fn poll_flush( | ||
| mut self: Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| ) -> Poll<Result<(), std::io::Error>> { | ||
| Pin::new(&mut self.0).poll_flush(cx) | ||
| } | ||
|
|
||
| fn poll_shutdown( | ||
| mut self: Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| ) -> Poll<Result<(), std::io::Error>> { | ||
| Pin::new(&mut self.0).poll_shutdown(cx) | ||
| } | ||
| } | ||
|
|
||
| impl Connection for MockStream { | ||
| fn connected(&self) -> Connected { | ||
| Connected::new() | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| struct CustomConnector { | ||
| mock: Arc<Mutex<Option<tokio_test::io::Mock>>>, | ||
| } | ||
|
|
||
| impl Service<Uri> for CustomConnector { | ||
| type Response = MockStream; | ||
| type Error = std::io::Error; | ||
| type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; | ||
|
|
||
| fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| Poll::Ready(Ok(())) | ||
| } | ||
|
|
||
| fn call(&mut self, _dst: Uri) -> Self::Future { | ||
| let mock = self | ||
| .mock | ||
| .lock() | ||
| .unwrap() | ||
| .take() | ||
| .expect("mock connection only called once"); | ||
| Box::pin(async move { Ok(MockStream(TokioIo::new(mock))) }) | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_client_legacy_with_custom_connector_without_tcp() { | ||
| let mock_io = tokio_test::io::Builder::new() | ||
| .write(b"GET /test HTTP/1.1\r\nhost: example.com\r\n\r\n") | ||
| .read(b"HTTP/1.1 200 OK\r\ncontent-length: 5\r\n\r\nhello") | ||
| .build(); | ||
|
|
||
| let connector = CustomConnector { | ||
| mock: Arc::new(Mutex::new(Some(mock_io))), | ||
| }; | ||
|
|
||
| let client = Client::builder(TokioExecutor::new()) | ||
| .pool_max_idle_per_host(0) | ||
| .build(connector); | ||
|
|
||
| let req = Request::builder() | ||
| .uri("http://example.com/test") | ||
| .body(Empty::<Bytes>::new()) | ||
| .unwrap(); | ||
|
|
||
| let mut resp = client.request(req).await.expect("request succeeds"); | ||
| assert_eq!(resp.status(), StatusCode::OK); | ||
|
|
||
| let body = resp.body_mut().collect().await.unwrap().to_bytes(); | ||
| assert_eq!(&body[..], b"hello"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the intent of this file that is be run without the tcp feature flag active?
i should note that we run tests with
--all-features, so i am worried this this isn't exercising thetcpfeature flag in the way that we think it is.a comment here to explain the purpose of this test would be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
presuming this is intended to be run without the tcp feature flag, i'd suggest adding a step to the
testworkflow here:hyper-util/.github/workflows/CI.yml
Lines 58 to 60 in fb070b2
something like:
cargo test --features client-legacy,http1,tokio --test client_no_tcpthen, since this doesn't exercise anything meaningful when the tcp feature flag is active, we should refine this gate further like so:
that way, we don't bother with this test when the tcp flag is active.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great points, thank you @cratelyn! You're completely right — running --all-features\ enabled \ cp, so \client_no_tcp.rs\ was not exercising the no-tcp configuration in CI.
I've adopted your suggestions:
Ready for another look!