Skip to content

Commit

Permalink
feat!: remove codegen dependency on compression feature (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoeinstein committed Jun 13, 2022
1 parent d9f5c75 commit a585a72
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 388 deletions.
4 changes: 2 additions & 2 deletions examples/Cargo.toml
Expand Up @@ -200,7 +200,7 @@ futures = { version = "0.3", default-features = false, features = ["alloc"] }
prost = "0.10"
tokio = { version = "1.0", features = [ "rt-multi-thread", "time", "fs", "macros", "net",] }
tokio-stream = { version = "0.1", features = ["net"] }
tonic = { path = "../tonic", features = ["tls", "compression"] }
tonic = { path = "../tonic", features = ["tls", "gzip"] }
tower = { version = "0.4" }
# Required for routeguide
rand = "0.8"
Expand Down Expand Up @@ -237,4 +237,4 @@ tower-http = { version = "0.3", features = ["add-extension", "util"] }


[build-dependencies]
tonic-build = { path = "../tonic-build", features = ["prost", "compression"] }
tonic-build = { path = "../tonic-build", features = ["prost"] }
5 changes: 4 additions & 1 deletion examples/src/compression/client.rs
@@ -1,5 +1,6 @@
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use tonic::codec::CompressionEncoding;
use tonic::transport::Channel;

pub mod hello_world {
Expand All @@ -13,7 +14,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await
.unwrap();

let mut client = GreeterClient::new(channel).send_gzip().accept_gzip();
let mut client = GreeterClient::new(channel)
.send_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Gzip);

let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
Expand Down
5 changes: 4 additions & 1 deletion examples/src/compression/server.rs
Expand Up @@ -2,6 +2,7 @@ use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use tonic::codec::CompressionEncoding;

pub mod hello_world {
tonic::include_proto!("helloworld");
Expand Down Expand Up @@ -32,7 +33,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("GreeterServer listening on {}", addr);

let service = GreeterServer::new(greeter).send_gzip().accept_gzip();
let service = GreeterServer::new(greeter)
.send_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Gzip);

Server::builder().add_service(service).serve(addr).await?;

Expand Down
4 changes: 2 additions & 2 deletions tests/compression/Cargo.toml
Expand Up @@ -16,9 +16,9 @@ pin-project = "1.0"
prost = "0.10"
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]}
tokio-stream = {version = "0.1.5", features = ["net"]}
tonic = {path = "../../tonic", features = ["compression"]}
tonic = {path = "../../tonic", features = ["gzip"]}
tower = {version = "0.4", features = []}
tower-http = {version = "0.3", features = ["map-response-body", "map-request-body"]}

[build-dependencies]
tonic-build = {path = "../../tonic-build", features = ["compression"]}
tonic-build = {path = "../../tonic-build" }
9 changes: 5 additions & 4 deletions tests/compression/src/bidirectional_stream.rs
@@ -1,12 +1,13 @@
use super::*;
use tonic::codec::CompressionEncoding;

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_enabled() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default())
.accept_gzip()
.send_gzip();
.accept_compressed(CompressionEncoding::Gzip)
.send_compressed(CompressionEncoding::Gzip);

let request_bytes_counter = Arc::new(AtomicUsize::new(0));
let response_bytes_counter = Arc::new(AtomicUsize::new(0));
Expand Down Expand Up @@ -43,8 +44,8 @@ async fn client_enabled_server_enabled() {
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.send_gzip()
.accept_gzip();
.send_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Gzip);

let data = [0_u8; UNCOMPRESSED_MIN_BODY_SIZE].to_vec();
let stream = futures::stream::iter(vec![SomeData { data: data.clone() }, SomeData { data }]);
Expand Down
19 changes: 13 additions & 6 deletions tests/compression/src/client_stream.rs
@@ -1,11 +1,13 @@
use super::*;
use http_body::Body as _;
use tonic::codec::CompressionEncoding;

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_enabled() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).accept_gzip();
let svc =
test_server::TestServer::new(Svc::default()).accept_compressed(CompressionEncoding::Gzip);

let request_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand Down Expand Up @@ -33,7 +35,8 @@ async fn client_enabled_server_enabled() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).send_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.send_compressed(CompressionEncoding::Gzip);

let data = [0_u8; UNCOMPRESSED_MIN_BODY_SIZE].to_vec();
let stream = futures::stream::iter(vec![SomeData { data: data.clone() }, SomeData { data }]);
Expand All @@ -49,7 +52,8 @@ async fn client_enabled_server_enabled() {
async fn client_disabled_server_enabled() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).accept_gzip();
let svc =
test_server::TestServer::new(Svc::default()).accept_compressed(CompressionEncoding::Gzip);

let request_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand Down Expand Up @@ -103,7 +107,8 @@ async fn client_enabled_server_disabled() {
.unwrap();
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).send_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.send_compressed(CompressionEncoding::Gzip);

let data = [0_u8; UNCOMPRESSED_MIN_BODY_SIZE].to_vec();
let stream = futures::stream::iter(vec![SomeData { data: data.clone() }, SomeData { data }]);
Expand All @@ -122,7 +127,8 @@ async fn client_enabled_server_disabled() {
async fn compressing_response_from_client_stream() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).send_gzip();
let svc =
test_server::TestServer::new(Svc::default()).send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand All @@ -147,7 +153,8 @@ async fn compressing_response_from_client_stream() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

let stream = futures::stream::iter(vec![]);
let req = Request::new(Box::pin(stream));
Expand Down
15 changes: 10 additions & 5 deletions tests/compression/src/compressing_request.rs
@@ -1,11 +1,13 @@
use super::*;
use http_body::Body as _;
use tonic::codec::CompressionEncoding;

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_enabled() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).accept_gzip();
let svc =
test_server::TestServer::new(Svc::default()).accept_compressed(CompressionEncoding::Gzip);

let request_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand Down Expand Up @@ -35,7 +37,8 @@ async fn client_enabled_server_enabled() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).send_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.send_compressed(CompressionEncoding::Gzip);

for _ in 0..3 {
client
Expand Down Expand Up @@ -63,7 +66,8 @@ async fn client_enabled_server_disabled() {
.unwrap();
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).send_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.send_compressed(CompressionEncoding::Gzip);

let status = client
.compress_input_unary(SomeData {
Expand All @@ -88,7 +92,8 @@ async fn client_enabled_server_disabled() {
async fn client_mark_compressed_without_header_server_enabled() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).accept_gzip();
let svc =
test_server::TestServer::new(Svc::default()).accept_compressed(CompressionEncoding::Gzip);

tokio::spawn({
async move {
Expand All @@ -107,7 +112,7 @@ async fn client_mark_compressed_without_header_server_enabled() {
Ok(req)
},
)
.send_gzip();
.send_compressed(CompressionEncoding::Gzip);

let status = client
.compress_input_unary(SomeData {
Expand Down
34 changes: 22 additions & 12 deletions tests/compression/src/compressing_response.rs
@@ -1,4 +1,5 @@
use super::*;
use tonic::codec::CompressionEncoding;

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_enabled() {
Expand Down Expand Up @@ -31,7 +32,8 @@ async fn client_enabled_server_enabled() {
}
}

let svc = test_server::TestServer::new(Svc::default()).send_gzip();
let svc =
test_server::TestServer::new(Svc::default()).send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand All @@ -57,7 +59,8 @@ async fn client_enabled_server_enabled() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

for _ in 0..3 {
let res = client.compress_output_unary(()).await.unwrap();
Expand Down Expand Up @@ -97,7 +100,8 @@ async fn client_enabled_server_disabled() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

let res = client.compress_output_unary(()).await.unwrap();

Expand Down Expand Up @@ -135,7 +139,8 @@ async fn client_disabled() {
}
}

let svc = test_server::TestServer::new(Svc::default()).send_gzip();
let svc =
test_server::TestServer::new(Svc::default()).send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand Down Expand Up @@ -175,7 +180,8 @@ async fn client_disabled() {
async fn server_replying_with_unsupported_encoding() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).send_gzip();
let svc =
test_server::TestServer::new(Svc::default()).send_compressed(CompressionEncoding::Gzip);

fn add_weird_content_encoding<B>(mut response: http::Response<B>) -> http::Response<B> {
response
Expand All @@ -197,7 +203,8 @@ async fn server_replying_with_unsupported_encoding() {
.unwrap();
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);
let status: Status = client.compress_output_unary(()).await.unwrap_err();

assert_eq!(status.code(), tonic::Code::Unimplemented);
Expand All @@ -214,7 +221,7 @@ async fn disabling_compression_on_single_response() {
let svc = test_server::TestServer::new(Svc {
disable_compressing_on_response: true,
})
.send_gzip();
.send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand All @@ -239,7 +246,8 @@ async fn disabling_compression_on_single_response() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

let res = client.compress_output_unary(()).await.unwrap();
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), "gzip");
Expand All @@ -254,7 +262,7 @@ async fn disabling_compression_on_response_but_keeping_compression_on_stream() {
let svc = test_server::TestServer::new(Svc {
disable_compressing_on_response: true,
})
.send_gzip();
.send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand All @@ -279,7 +287,8 @@ async fn disabling_compression_on_response_but_keeping_compression_on_stream() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

let res = client.compress_output_server_stream(()).await.unwrap();

Expand Down Expand Up @@ -309,7 +318,7 @@ async fn disabling_compression_on_response_from_client_stream() {
let svc = test_server::TestServer::new(Svc {
disable_compressing_on_response: true,
})
.send_gzip();
.send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand All @@ -334,7 +343,8 @@ async fn disabling_compression_on_response_from_client_stream() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

let stream = futures::stream::iter(vec![]);
let req = Request::new(Box::pin(stream));
Expand Down
13 changes: 9 additions & 4 deletions tests/compression/src/server_stream.rs
@@ -1,11 +1,13 @@
use super::*;
use tonic::codec::CompressionEncoding;
use tonic::Streaming;

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_enabled() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).send_gzip();
let svc =
test_server::TestServer::new(Svc::default()).send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand All @@ -30,7 +32,8 @@ async fn client_enabled_server_enabled() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

let res = client.compress_output_server_stream(()).await.unwrap();

Expand All @@ -57,7 +60,8 @@ async fn client_enabled_server_enabled() {
async fn client_disabled_server_enabled() {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default()).send_gzip();
let svc =
test_server::TestServer::new(Svc::default()).send_compressed(CompressionEncoding::Gzip);

let response_bytes_counter = Arc::new(AtomicUsize::new(0));

Expand Down Expand Up @@ -127,7 +131,8 @@ async fn client_enabled_server_disabled() {
}
});

let mut client = test_client::TestClient::new(mock_io_channel(client).await).accept_gzip();
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip);

let res = client.compress_output_server_stream(()).await.unwrap();

Expand Down
1 change: 0 additions & 1 deletion tonic-build/Cargo.toml
Expand Up @@ -22,7 +22,6 @@ quote = "1.0"
syn = "1.0"

[features]
compression = []
default = ["transport", "prost"]
prost = ["prost-build"]
transport = []
Expand Down

0 comments on commit a585a72

Please sign in to comment.