implement last interop test

This commit is contained in:
Lucio Franco
2019-09-21 16:02:10 -06:00
parent 922e480cec
commit 782da6f0e5
5 changed files with 118 additions and 14 deletions
+4
View File
@@ -28,6 +28,10 @@ pub(crate) fn generate(service: &Service, proto_path: &str) -> TokenStream {
impl<T: #server_trait> #server_make_service<T> {
pub fn new(inner: T) -> Self {
let inner = Arc::new(inner);
Self::from_shared(inner)
}
pub fn from_shared(inner: Arc<T>) -> Self {
Self { inner }
}
}
+100 -3
View File
@@ -21,8 +21,6 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let addr = "127.0.0.1:10000".parse().unwrap();
let test_service = server::create();
let mut builder = Server::builder();
if matches.use_tls {
@@ -59,7 +57,106 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
}
});
builder.serve(addr, test_service).await?;
builder
.serve(
addr,
router::Router {
test_service: std::sync::Arc::new(server::TestService),
unimplemented_service: std::sync::Arc::new(server::UnimplementedService),
},
)
.await?;
Ok(())
}
mod router {
use futures_util::future;
use http::{Request, Response};
use std::sync::Arc;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tonic::{body::BoxBody, transport::Body};
use tonic_interop::server::{
TestService, TestServiceServer, UnimplementedService, UnimplementedServiceServer,
};
use tower::Service;
#[derive(Clone)]
pub struct Router {
pub test_service: Arc<TestService>,
pub unimplemented_service: Arc<UnimplementedService>,
}
impl Service<()> for Router {
type Response = Router;
type Error = Never;
type Future = future::Ready<Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ok(()).into()
}
fn call(&mut self, _req: ()) -> Self::Future {
future::ok(self.clone())
}
}
impl Service<Request<Body>> for Router {
type Response = Response<BoxBody>;
type Error = Never;
type Future =
Pin<Box<dyn Future<Output = Result<Response<BoxBody>, Never>> + Send + 'static>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Ok(()).into()
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
let mut segments = req.uri().path().split("/");
segments.next();
let service = segments.next().unwrap();
match service {
"grpc.testing.TestService" => {
let me = self.clone();
Box::pin(async move {
let mut svc = TestServiceServer::from_shared(me.test_service);
let mut svc = svc.call(()).await.unwrap();
let res = svc.call(req).await.unwrap();
Ok(res)
})
}
"grpc.testing.UnimplementedService" => {
let me = self.clone();
Box::pin(async move {
let mut svc =
UnimplementedServiceServer::from_shared(me.unimplemented_service);
let mut svc = svc.call(()).await.unwrap();
let res = svc.call(req).await.unwrap();
Ok(res)
})
}
_ => unimplemented!(),
}
}
}
#[derive(Debug)]
pub enum Never {}
impl std::fmt::Display for Never {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {}
}
}
impl std::error::Error for Never {}
}
+11 -8
View File
@@ -5,16 +5,10 @@ use std::pin::Pin;
use std::time::{Duration, Instant};
use tonic::{Code, Request, Response, Status};
pub fn create() -> pb::server::TestServiceServer<TestService> {
server::TestServiceServer::new(TestService {
data: String::new(),
})
}
pub use pb::server::{TestServiceServer, UnimplementedServiceServer};
#[derive(Default, Clone)]
pub struct TestService {
data: String,
}
pub struct TestService;
type Result<T> = std::result::Result<Response<T>, Status>;
type Streaming<T> = Request<tonic::Streaming<T>>;
@@ -156,3 +150,12 @@ impl pb::server::TestService for TestService {
Err(Status::unimplemented(""))
}
}
pub struct UnimplementedService;
#[tonic::async_trait]
impl pb::server::UnimplementedService for UnimplementedService {
async fn unimplemented_call(&self, _req: Request<Empty>) -> Result<Empty> {
Err(Status::unimplemented(""))
}
}
+2 -3
View File
@@ -49,6 +49,5 @@ sleep 1
./target/debug/client \
--test_case=empty_unary,large_unary,client_streaming,server_streaming,ping_pong,\
empty_stream,status_code_and_message,special_status_message,unimplemented_method,custom_metadata $ARG
# ,unimplemented_service,custom_metadata
empty_stream,status_code_and_message,special_status_message,unimplemented_method,\
unimplemented_service,custom_metadata $ARG
+1
View File
@@ -11,6 +11,7 @@ mod tls;
pub use self::channel::Channel;
pub use self::endpoint::Endpoint;
pub use self::server::Server;
pub use hyper::Body;
use std::{error, fmt};