Skip to content

Commit

Permalink
fix(tonic): Remove Sync requirement for streams (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed Oct 25, 2021
1 parent 1f3df8d commit 23c1392
Show file tree
Hide file tree
Showing 31 changed files with 207 additions and 165 deletions.
47 changes: 21 additions & 26 deletions Cargo.toml
@@ -1,29 +1,24 @@
[workspace]
members = [
"tonic",
"tonic-build",
"tonic-health",
"tonic-types",
"tonic-reflection",
"tonic-web",

# Non-published crates
"examples",
"interop",

# Tests
"tests/included_service",
"tests/same_name",
"tests/service_named_service",
"tests/wellknown",
"tests/wellknown-compiled",
"tests/extern_path/uuid",
"tests/ambiguous_methods",
"tests/extern_path/my_application",
"tests/integration_tests",
"tests/stream_conflict",
"tests/root-crate-path",
"tests/compression",
"tonic-web/tests/integration"
"tonic",
"tonic-build",
"tonic-health",
"tonic-types",
"tonic-reflection",
"tonic-web", # Non-published crates
"examples",
"interop", # Tests
"tests/included_service",
"tests/same_name",
"tests/service_named_service",
"tests/wellknown",
"tests/wellknown-compiled",
"tests/extern_path/uuid",
"tests/ambiguous_methods",
"tests/extern_path/my_application",
"tests/integration_tests",
"tests/stream_conflict",
"tests/root-crate-path",
"tests/compression",
"tonic-web/tests/integration",
]

4 changes: 2 additions & 2 deletions examples/routeguide-tutorial.md
Expand Up @@ -300,7 +300,7 @@ impl RouteGuide for RouteGuideService {
unimplemented!()
}

type RouteChatStream = Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + Sync + 'static>>;
type RouteChatStream = Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + 'static>>;

async fn route_chat(
&self,
Expand Down Expand Up @@ -493,7 +493,7 @@ use std::collections::HashMap;

```rust
type RouteChatStream =
Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + Sync + 'static>>;
Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + 'static>>;


async fn route_chat(
Expand Down
2 changes: 1 addition & 1 deletion examples/src/authentication/server.rs
Expand Up @@ -8,7 +8,7 @@ use std::pin::Pin;
use tonic::{metadata::MetadataValue, transport::Server, Request, Response, Status, Streaming};

type EchoResult<T> = Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[derive(Default)]
pub struct EchoServer;
Expand Down
2 changes: 1 addition & 1 deletion examples/src/dynamic_load_balance/server.rs
Expand Up @@ -11,7 +11,7 @@ use tonic::{transport::Server, Request, Response, Status, Streaming};
use pb::{EchoRequest, EchoResponse};

type EchoResult<T> = Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[derive(Debug)]
pub struct EchoServer {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/hyper_warp_multiplex/server.rs
Expand Up @@ -35,7 +35,7 @@ use echo::{
EchoRequest, EchoResponse,
};

type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[derive(Default)]
pub struct MyGreeter {}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/load_balance/server.rs
Expand Up @@ -11,7 +11,7 @@ use tonic::{transport::Server, Request, Response, Status, Streaming};
use pb::{EchoRequest, EchoResponse};

type EchoResult<T> = Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[derive(Debug)]
pub struct EchoServer {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/multiplex/server.rs
Expand Up @@ -20,7 +20,7 @@ use echo::{
EchoRequest, EchoResponse,
};

type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
3 changes: 1 addition & 2 deletions examples/src/routeguide/server.rs
Expand Up @@ -102,8 +102,7 @@ impl RouteGuide for RouteGuideService {
Ok(Response::new(summary))
}

type RouteChatStream =
Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + Sync + 'static>>;
type RouteChatStream = Pin<Box<dyn Stream<Item = Result<RouteNote, Status>> + Send + 'static>>;

async fn route_chat(
&self,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/streaming/server.rs
Expand Up @@ -12,7 +12,7 @@ use tonic::{transport::Server, Request, Response, Status, Streaming};
use pb::{EchoRequest, EchoResponse};

type EchoResult<T> = Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[derive(Debug)]
pub struct EchoServer {}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/tls/server.rs
Expand Up @@ -14,7 +14,7 @@ use tonic::{
};

type EchoResult<T> = Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[derive(Default)]
pub struct EchoServer;
Expand Down
2 changes: 1 addition & 1 deletion examples/src/tls_client_auth/server.rs
Expand Up @@ -9,7 +9,7 @@ use tonic::transport::{Certificate, Identity, Server, ServerTlsConfig};
use tonic::{Request, Response, Status};

type EchoResult<T> = Result<Response<T>, Status>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send>>;

#[derive(Default)]
pub struct EchoServer;
Expand Down
5 changes: 2 additions & 3 deletions interop/src/server.rs
Expand Up @@ -18,9 +18,8 @@ pub struct TestService;

type Result<T> = std::result::Result<Response<T>, Status>;
type Streaming<T> = Request<tonic::Streaming<T>>;
type Stream<T> = Pin<
Box<dyn futures_core::Stream<Item = std::result::Result<T, Status>> + Send + Sync + 'static>,
>;
type Stream<T> =
Pin<Box<dyn futures_core::Stream<Item = std::result::Result<T, Status>> + Send + 'static>>;
type BoxFuture<T, E> = Pin<Box<dyn Future<Output = std::result::Result<T, E>> + Send + 'static>>;

#[tonic::async_trait]
Expand Down
4 changes: 2 additions & 2 deletions tests/compression/src/lib.rs
Expand Up @@ -69,7 +69,7 @@ impl test_server::Test for Svc {
}

type CompressOutputServerStreamStream =
Pin<Box<dyn Stream<Item = Result<SomeData, Status>> + Send + Sync + 'static>>;
Pin<Box<dyn Stream<Item = Result<SomeData, Status>> + Send + 'static>>;

async fn compress_output_server_stream(
&self,
Expand Down Expand Up @@ -110,7 +110,7 @@ impl test_server::Test for Svc {
}

type CompressInputOutputBidirectionalStreamStream =
Pin<Box<dyn Stream<Item = Result<SomeData, Status>> + Send + Sync + 'static>>;
Pin<Box<dyn Stream<Item = Result<SomeData, Status>> + Send + 'static>>;

async fn compress_input_output_bidirectional_stream(
&self,
Expand Down
3 changes: 2 additions & 1 deletion tests/integration_tests/Cargo.toml
Expand Up @@ -12,14 +12,15 @@ version = "0.1.0"
bytes = "1.0"
futures-util = "0.3"
prost = "0.9"
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]}
tonic = {path = "../../tonic"}

[dev-dependencies]
async-stream = "0.3"
futures = "0.3"
http = "0.2"
http-body = "0.4"
hyper = "0.14"
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]}
tokio-stream = {version = "0.1.5", features = ["net"]}
tower = {version = "0.4", features = []}
tower-service = "0.3"
Expand Down
51 changes: 51 additions & 0 deletions tests/integration_tests/src/lib.rs
Expand Up @@ -2,3 +2,54 @@ pub mod pb {
tonic::include_proto!("test");
tonic::include_proto!("stream");
}

pub mod mock {
use std::{
pin::Pin,
task::{Context, Poll},
};

use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tonic::transport::server::Connected;

#[derive(Debug)]
pub struct MockStream(pub tokio::io::DuplexStream);

impl Connected for MockStream {
type ConnectInfo = ();

/// Create type holding information about the connection.
fn connect_info(&self) -> Self::ConnectInfo {}
}

impl AsyncRead for MockStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}

impl AsyncWrite for MockStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_flush(cx)
}

fn poll_shutdown(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_shutdown(cx)
}
}
}
59 changes: 4 additions & 55 deletions tests/integration_tests/tests/status.rs
@@ -1,6 +1,7 @@
use bytes::Bytes;
use futures_util::FutureExt;
use http::Uri;
use integration_tests::mock::MockStream;
use integration_tests::pb::{
test_client, test_server, test_stream_client, test_stream_server, Input, InputStream, Output,
OutputStream,
Expand Down Expand Up @@ -125,9 +126,8 @@ async fn status_with_metadata() {
jh.await.unwrap();
}

type Stream<T> = std::pin::Pin<
Box<dyn futures::Stream<Item = std::result::Result<T, Status>> + Send + Sync + 'static>,
>;
type Stream<T> =
std::pin::Pin<Box<dyn futures::Stream<Item = std::result::Result<T, Status>> + Send + 'static>>;

#[tokio::test]
async fn status_from_server_stream() {
Expand Down Expand Up @@ -184,7 +184,7 @@ async fn status_from_server_stream_with_source() {
let channel = Endpoint::try_from("http://[::]:50051")
.unwrap()
.connect_with_connector_lazy(tower::service_fn(move |_: Uri| async move {
Err::<mock::MockStream, _>(std::io::Error::new(std::io::ErrorKind::Other, "WTF"))
Err::<MockStream, _>(std::io::Error::new(std::io::ErrorKind::Other, "WTF"))
}))
.unwrap();

Expand All @@ -201,54 +201,3 @@ fn trace_init() {
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.try_init();
}

mod mock {
use std::{
pin::Pin,
task::{Context, Poll},
};

use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tonic::transport::server::Connected;

#[derive(Debug)]
pub struct MockStream(pub tokio::io::DuplexStream);

impl Connected for MockStream {
type ConnectInfo = ();

/// Create type holding information about the connection.
fn connect_info(&self) -> Self::ConnectInfo {}
}

impl AsyncRead for MockStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}

impl AsyncWrite for MockStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_flush(cx)
}

fn poll_shutdown(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_shutdown(cx)
}
}
}
47 changes: 47 additions & 0 deletions tests/integration_tests/tests/streams.rs
@@ -0,0 +1,47 @@
use futures::FutureExt;
use integration_tests::pb::{test_stream_server, InputStream, OutputStream};
use tonic::{transport::Server, Request, Response, Status};

type Stream<T> =
std::pin::Pin<Box<dyn futures::Stream<Item = std::result::Result<T, Status>> + Send + 'static>>;

#[tokio::test]
async fn status_from_server_stream_with_source() {
struct Svc;

#[tonic::async_trait]
impl test_stream_server::TestStream for Svc {
type StreamCallStream = Stream<OutputStream>;

async fn stream_call(
&self,
_: Request<InputStream>,
) -> Result<Response<Self::StreamCallStream>, Status> {
let s = Unsync(0 as *mut ());

Ok(Response::new(Box::pin(s) as Self::StreamCallStream))
}
}

let svc = test_stream_server::TestStreamServer::new(Svc);

Server::builder()
.add_service(svc)
.serve("127.0.0.1:1339".parse().unwrap())
.now_or_never();
}

struct Unsync(*mut ());

unsafe impl Send for Unsync {}

impl futures::Stream for Unsync {
type Item = Result<OutputStream, Status>;

fn poll_next(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
unimplemented!()
}
}

0 comments on commit 23c1392

Please sign in to comment.