Skip to content

Commit 4533a6e

Browse files
conradludgatedavidpdrsn
andauthoredApr 20, 2022
fix(build): Reduce Default bound requirement (#974)
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
1 parent b4f9634 commit 4533a6e

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
 

‎tests/integration_tests/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ http-body = "0.4"
2323
hyper = "0.14"
2424
tokio-stream = {version = "0.1.5", features = ["net"]}
2525
tower = {version = "0.4", features = []}
26+
tower-http = { version = "0.2", features = ["set-header", "trace"] }
2627
tower-service = "0.3"
2728
tracing-subscriber = {version = "0.3", features = ["env-filter"]}
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::time::Duration;
2+
3+
use futures::{channel::oneshot, FutureExt};
4+
use http::{header::HeaderName, HeaderValue};
5+
use integration_tests::pb::{test_client::TestClient, test_server, Input, Output};
6+
use tonic::{
7+
transport::{Endpoint, Server},
8+
Request, Response, Status,
9+
};
10+
use tower::ServiceBuilder;
11+
use tower_http::{set_header::SetRequestHeaderLayer, trace::TraceLayer};
12+
13+
#[tokio::test]
14+
async fn connect_supports_standard_tower_layers() {
15+
struct Svc;
16+
17+
#[tonic::async_trait]
18+
impl test_server::Test for Svc {
19+
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
20+
match req.metadata().get("x-test") {
21+
Some(_) => Ok(Response::new(Output {})),
22+
None => Err(Status::internal("user-agent header is missing")),
23+
}
24+
}
25+
}
26+
27+
let (tx, rx) = oneshot::channel();
28+
let svc = test_server::TestServer::new(Svc);
29+
30+
// Start the server now, second call should succeed
31+
let jh = tokio::spawn(async move {
32+
Server::builder()
33+
.add_service(svc)
34+
.serve_with_shutdown("127.0.0.1:1340".parse().unwrap(), rx.map(drop))
35+
.await
36+
.unwrap();
37+
});
38+
39+
let channel = Endpoint::from_static("http://127.0.0.1:1340").connect_lazy();
40+
41+
// prior to https://github.com/hyperium/tonic/pull/974
42+
// this would not compile. (specifically the `TraceLayer`)
43+
let mut client = TestClient::new(
44+
ServiceBuilder::new()
45+
.layer(SetRequestHeaderLayer::overriding(
46+
HeaderName::from_static("x-test"),
47+
HeaderValue::from_static("test-header"),
48+
))
49+
.layer(TraceLayer::new_for_grpc())
50+
.service(channel),
51+
);
52+
53+
tokio::time::sleep(Duration::from_millis(100)).await;
54+
client.unary_call(Request::new(Input {})).await.unwrap();
55+
56+
tx.send(()).unwrap();
57+
jh.await.unwrap();
58+
}

‎tonic-build/src/client.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn generate<T: Service>(
5858
where
5959
T: tonic::client::GrpcService<tonic::body::BoxBody>,
6060
T::Error: Into<StdError>,
61-
T::ResponseBody: Default + Body<Data = Bytes> + Send + 'static,
61+
T::ResponseBody: Body<Data = Bytes> + Send + 'static,
6262
<T::ResponseBody as Body>::Error: Into<StdError> + Send,
6363
{
6464
pub fn new(inner: T) -> Self {
@@ -69,6 +69,7 @@ pub fn generate<T: Service>(
6969
pub fn with_interceptor<F>(inner: T, interceptor: F) -> #service_ident<InterceptedService<T, F>>
7070
where
7171
F: tonic::service::Interceptor,
72+
T::ResponseBody: Default,
7273
T: tonic::codegen::Service<
7374
http::Request<tonic::body::BoxBody>,
7475
Response = http::Response<<T as tonic::client::GrpcService<tonic::body::BoxBody>>::ResponseBody>

0 commit comments

Comments
 (0)
Please sign in to comment.