-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): Add optional default unimplemented stubs (#1344)
Co-authored-by: Lucio Franco <luciofranco14@gmail.com>
- Loading branch information
1 parent
35cf1f2
commit aff1daf
Showing
10 changed files
with
306 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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,20 @@ | ||
[package] | ||
authors = ["Jordan Singh <me@jordansingh.com>"] | ||
edition = "2021" | ||
license = "MIT" | ||
name = "default_stubs" | ||
publish = false | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
futures = "0.3" | ||
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]} | ||
tokio-stream = {version = "0.1", features = ["net"]} | ||
prost = "0.11" | ||
tonic = {path = "../../tonic"} | ||
|
||
[build-dependencies] | ||
tonic-build = {path = "../../tonic-build" } | ||
|
||
[package.metadata.cargo-machete] | ||
ignored = ["prost"] |
This file contains 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,9 @@ | ||
fn main() { | ||
tonic_build::configure() | ||
.compile(&["proto/test.proto"], &["proto"]) | ||
.unwrap(); | ||
tonic_build::configure() | ||
.generate_default_stubs(true) | ||
.compile(&["proto/test_default.proto"], &["proto"]) | ||
.unwrap(); | ||
} |
This file contains 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,12 @@ | ||
syntax = "proto3"; | ||
|
||
package test; | ||
|
||
import "google/protobuf/empty.proto"; | ||
|
||
service Test { | ||
rpc Unary(google.protobuf.Empty) returns (google.protobuf.Empty); | ||
rpc ServerStream(google.protobuf.Empty) returns (stream google.protobuf.Empty); | ||
rpc ClientStream(stream google.protobuf.Empty) returns (google.protobuf.Empty); | ||
rpc BidirectionalStream(stream google.protobuf.Empty) returns (stream google.protobuf.Empty); | ||
} |
This file contains 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,12 @@ | ||
syntax = "proto3"; | ||
|
||
package test_default; | ||
|
||
import "google/protobuf/empty.proto"; | ||
|
||
service TestDefault { | ||
rpc Unary(google.protobuf.Empty) returns (google.protobuf.Empty); | ||
rpc ServerStream(google.protobuf.Empty) returns (stream google.protobuf.Empty); | ||
rpc ClientStream(stream google.protobuf.Empty) returns (google.protobuf.Empty); | ||
rpc BidirectionalStream(stream google.protobuf.Empty) returns (stream google.protobuf.Empty); | ||
} |
This file contains 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,47 @@ | ||
#![allow(unused_imports)] | ||
|
||
mod test_defaults; | ||
|
||
use futures::{Stream, StreamExt}; | ||
use std::pin::Pin; | ||
use tonic::{Request, Response, Status, Streaming}; | ||
|
||
tonic::include_proto!("test"); | ||
tonic::include_proto!("test_default"); | ||
|
||
#[derive(Debug, Default)] | ||
struct Svc; | ||
|
||
#[tonic::async_trait] | ||
impl test_server::Test for Svc { | ||
type ServerStreamStream = Pin<Box<dyn Stream<Item = Result<(), Status>> + Send + 'static>>; | ||
type BidirectionalStreamStream = | ||
Pin<Box<dyn Stream<Item = Result<(), Status>> + Send + 'static>>; | ||
|
||
async fn unary(&self, _: Request<()>) -> Result<Response<()>, Status> { | ||
Err(Status::permission_denied("")) | ||
} | ||
|
||
async fn server_stream( | ||
&self, | ||
_: Request<()>, | ||
) -> Result<Response<Self::ServerStreamStream>, Status> { | ||
Err(Status::permission_denied("")) | ||
} | ||
|
||
async fn client_stream(&self, _: Request<Streaming<()>>) -> Result<Response<()>, Status> { | ||
Err(Status::permission_denied("")) | ||
} | ||
|
||
async fn bidirectional_stream( | ||
&self, | ||
_: Request<Streaming<()>>, | ||
) -> Result<Response<Self::BidirectionalStreamStream>, Status> { | ||
Err(Status::permission_denied("")) | ||
} | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl test_default_server::TestDefault for Svc { | ||
// Default unimplemented stubs provided here. | ||
} |
This file contains 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,112 @@ | ||
#![allow(unused_imports)] | ||
|
||
use crate::*; | ||
use std::net::SocketAddr; | ||
use tokio::net::TcpListener; | ||
use tonic::transport::Server; | ||
|
||
#[cfg(test)] | ||
fn echo_requests_iter() -> impl Stream<Item = ()> { | ||
tokio_stream::iter(1..usize::MAX).map(|_| ()) | ||
} | ||
|
||
#[tokio::test()] | ||
async fn test_default_stubs() { | ||
use tonic::Code; | ||
|
||
let addrs = run_services_in_background().await; | ||
|
||
// First validate pre-existing functionality (trait has no default implementation, we explicitly return PermissionDenied in lib.rs). | ||
let mut client = test_client::TestClient::connect(format!("http://{}", addrs.0)) | ||
.await | ||
.unwrap(); | ||
assert_eq!( | ||
client.unary(()).await.unwrap_err().code(), | ||
Code::PermissionDenied | ||
); | ||
assert_eq!( | ||
client.server_stream(()).await.unwrap_err().code(), | ||
Code::PermissionDenied | ||
); | ||
assert_eq!( | ||
client | ||
.client_stream(echo_requests_iter().take(5)) | ||
.await | ||
.unwrap_err() | ||
.code(), | ||
Code::PermissionDenied | ||
); | ||
assert_eq!( | ||
client | ||
.bidirectional_stream(echo_requests_iter().take(5)) | ||
.await | ||
.unwrap_err() | ||
.code(), | ||
Code::PermissionDenied | ||
); | ||
|
||
// Then validate opt-in new functionality (trait has default implementation of returning Unimplemented). | ||
let mut client_default_stubs = test_client::TestClient::connect(format!("http://{}", addrs.1)) | ||
.await | ||
.unwrap(); | ||
assert_eq!( | ||
client_default_stubs.unary(()).await.unwrap_err().code(), | ||
Code::Unimplemented | ||
); | ||
assert_eq!( | ||
client_default_stubs | ||
.server_stream(()) | ||
.await | ||
.unwrap_err() | ||
.code(), | ||
Code::Unimplemented | ||
); | ||
assert_eq!( | ||
client_default_stubs | ||
.client_stream(echo_requests_iter().take(5)) | ||
.await | ||
.unwrap_err() | ||
.code(), | ||
Code::Unimplemented | ||
); | ||
assert_eq!( | ||
client_default_stubs | ||
.bidirectional_stream(echo_requests_iter().take(5)) | ||
.await | ||
.unwrap_err() | ||
.code(), | ||
Code::Unimplemented | ||
); | ||
} | ||
|
||
#[cfg(test)] | ||
async fn run_services_in_background() -> (SocketAddr, SocketAddr) { | ||
let svc = test_server::TestServer::new(Svc {}); | ||
let svc_default_stubs = test_default_server::TestDefaultServer::new(Svc {}); | ||
|
||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
let addr = listener.local_addr().unwrap(); | ||
|
||
let listener_default_stubs = TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
let addr_default_stubs = listener_default_stubs.local_addr().unwrap(); | ||
|
||
tokio::spawn(async move { | ||
Server::builder() | ||
.add_service(svc) | ||
.serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener)) | ||
.await | ||
.unwrap(); | ||
}); | ||
|
||
tokio::spawn(async move { | ||
Server::builder() | ||
.add_service(svc_default_stubs) | ||
.serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new( | ||
listener_default_stubs, | ||
)) | ||
.await | ||
.unwrap(); | ||
}); | ||
|
||
(addr, addr_default_stubs) | ||
} |
This file contains 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 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
Oops, something went wrong.