Skip to content
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

Add zstd compression support #1371

Closed
wants to merge 11 commits into from
3 changes: 2 additions & 1 deletion tests/compression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ futures = "0.3"
http = "0.2"
http-body = "0.4"
hyper = "0.14.3"
paste = "1.0.12"
pin-project = "1.0"
prost = "0.11"
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]}
tonic = {path = "../../tonic", features = ["gzip"]}
tonic = {path = "../../tonic", features = ["gzip", "zstd"]}
tower = {version = "0.4", features = []}
tower-http = {version = "0.4", features = ["map-response-body", "map-request-body"]}

Expand Down
50 changes: 39 additions & 11 deletions tests/compression/src/bidirectional_stream.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
use super::*;
use http_body::Body;
use tonic::codec::CompressionEncoding;

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_enabled() {
util::parametrized_tests! {
client_enabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
}

#[allow(dead_code)]
async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

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

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

fn assert_right_encoding<B>(req: http::Request<B>) -> http::Request<B> {
assert_eq!(req.headers().get("grpc-encoding").unwrap(), "gzip");
req
#[derive(Clone)]
pub struct AssertRightEncoding {
encoding: CompressionEncoding,
}

#[allow(dead_code)]
impl AssertRightEncoding {
pub fn new(encoding: CompressionEncoding) -> Self {
Self { encoding }
}

pub fn call<B: Body>(self, req: http::Request<B>) -> http::Request<B> {
assert_eq!(
req.headers().get("grpc-encoding").unwrap(),
self.encoding.as_str()
);

req
}
}

tokio::spawn({
Expand All @@ -24,7 +47,9 @@ async fn client_enabled_server_enabled() {
Server::builder()
.layer(
ServiceBuilder::new()
.map_request(assert_right_encoding)
.map_request(move |req| {
AssertRightEncoding::new(encoding).clone().call(req)
})
.layer(measure_request_body_size_layer(
request_bytes_counter.clone(),
))
Expand All @@ -44,8 +69,8 @@ async fn client_enabled_server_enabled() {
});

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

let data = [0_u8; UNCOMPRESSED_MIN_BODY_SIZE].to_vec();
let stream = futures::stream::iter(vec![SomeData { data: data.clone() }, SomeData { data }]);
Expand All @@ -56,7 +81,10 @@ async fn client_enabled_server_enabled() {
.await
.unwrap();

assert_eq!(res.metadata().get("grpc-encoding").unwrap(), "gzip");
assert_eq!(
res.metadata().get("grpc-encoding").unwrap(),
encoding.as_str()
);

let mut stream: Streaming<SomeData> = res.into_inner();

Expand Down
99 changes: 72 additions & 27 deletions tests/compression/src/client_stream.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
use super::*;
use http_body::Body as _;
use http_body::Body;
use tonic::codec::CompressionEncoding;

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_enabled() {
util::parametrized_tests! {
client_enabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
}

#[allow(dead_code)]
async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

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

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

fn assert_right_encoding<B>(req: http::Request<B>) -> http::Request<B> {
assert_eq!(req.headers().get("grpc-encoding").unwrap(), "gzip");
req
#[derive(Clone)]
pub struct AssertRightEncoding {
encoding: CompressionEncoding,
}

#[allow(dead_code)]
impl AssertRightEncoding {
pub fn new(encoding: CompressionEncoding) -> Self {
Self { encoding }
}

pub fn call<B: Body>(self, req: http::Request<B>) -> http::Request<B> {
assert_eq!(
req.headers().get("grpc-encoding").unwrap(),
self.encoding.as_str()
);

req
}
}

tokio::spawn({
Expand All @@ -22,7 +43,9 @@ async fn client_enabled_server_enabled() {
Server::builder()
.layer(
ServiceBuilder::new()
.map_request(assert_right_encoding)
.map_request(move |req| {
AssertRightEncoding::new(encoding).clone().call(req)
})
.layer(measure_request_body_size_layer(
request_bytes_counter.clone(),
))
Expand All @@ -35,8 +58,8 @@ async fn client_enabled_server_enabled() {
}
});

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

let data = [0_u8; UNCOMPRESSED_MIN_BODY_SIZE].to_vec();
let stream = futures::stream::iter(vec![SomeData { data: data.clone() }, SomeData { data }]);
Expand All @@ -48,12 +71,17 @@ async fn client_enabled_server_enabled() {
assert!(bytes_sent < UNCOMPRESSED_MIN_BODY_SIZE);
}

#[tokio::test(flavor = "multi_thread")]
async fn client_disabled_server_enabled() {
util::parametrized_tests! {
client_disabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
}

#[allow(dead_code)]
async fn client_disabled_server_enabled(encoding: CompressionEncoding) {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

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

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

Expand Down Expand Up @@ -93,8 +121,14 @@ async fn client_disabled_server_enabled() {
assert!(bytes_sent > UNCOMPRESSED_MIN_BODY_SIZE);
}

#[tokio::test(flavor = "multi_thread")]
async fn client_enabled_server_disabled() {
util::parametrized_tests! {
client_enabled_server_disabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
}

#[allow(dead_code)]
async fn client_enabled_server_disabled(encoding: CompressionEncoding) {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

let svc = test_server::TestServer::new(Svc::default());
Expand All @@ -107,8 +141,8 @@ async fn client_enabled_server_disabled() {
.unwrap();
});

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

let data = [0_u8; UNCOMPRESSED_MIN_BODY_SIZE].to_vec();
let stream = futures::stream::iter(vec![SomeData { data: data.clone() }, SomeData { data }]);
Expand All @@ -119,16 +153,24 @@ async fn client_enabled_server_disabled() {
assert_eq!(status.code(), tonic::Code::Unimplemented);
assert_eq!(
status.message(),
"Content is compressed with `gzip` which isn't supported"
format!(
"Content is compressed with `{}` which isn't supported",
encoding.as_str()
)
);
}

#[tokio::test(flavor = "multi_thread")]
async fn compressing_response_from_client_stream() {
util::parametrized_tests! {
compressing_response_from_client_stream,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
}

#[allow(dead_code)]
async fn compressing_response_from_client_stream(encoding: CompressionEncoding) {
let (client, server) = tokio::io::duplex(UNCOMPRESSED_MIN_BODY_SIZE * 10);

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

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

Expand All @@ -153,14 +195,17 @@ async fn compressing_response_from_client_stream() {
}
});

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

let stream = futures::stream::iter(vec![]);
let req = Request::new(Box::pin(stream));

let res = client.compress_output_client_stream(req).await.unwrap();
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), "gzip");
assert_eq!(
res.metadata().get("grpc-encoding").unwrap(),
encoding.as_str()
);
let bytes_sent = response_bytes_counter.load(SeqCst);
assert!(bytes_sent < UNCOMPRESSED_MIN_BODY_SIZE);
}
Loading