-
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 'use_generic_streaming_requests'
Adds an option for server trait generation that causes client-streaming methods to receive a generic `impl IntoStreamingRequest` param instead of a concrete `Request<Streaming<...>>` param. This allows for testing trait implementions with any type implementing `Stream`, and not having to go through the trouble of encoding a `Streaming` object. Fixes #462
- Loading branch information
Showing
10 changed files
with
135 additions
and
5 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
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,17 @@ | ||
[package] | ||
authors = ["Yotam Ofek <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
name = "use_generic_streaming_requests" | ||
|
||
[dependencies] | ||
tokio-stream = "0.1" | ||
prost = "0.13" | ||
tonic = {path = "../../tonic"} | ||
tokio = {version = "1.0", features = ["macros"]} | ||
|
||
[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,19 @@ | ||
Copyright (c) 2020 Lucio Franco | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,6 @@ | ||
fn main() { | ||
tonic_build::configure() | ||
.use_generic_streaming_requests(true) | ||
.compile_protos(&["proto/test.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,9 @@ | ||
syntax = "proto3"; | ||
|
||
package test; | ||
|
||
service Test { | ||
rpc TestRequest(stream Message) returns (Message); | ||
} | ||
|
||
message Message {} |
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,37 @@ | ||
use std::pin::pin; | ||
|
||
use tokio_stream::StreamExt; | ||
use tonic::{IntoStreamingRequest, Response, Status}; | ||
|
||
tonic::include_proto!("test"); | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Svc; | ||
|
||
#[tonic::async_trait] | ||
impl test_server::Test for Svc { | ||
async fn test_request( | ||
&self, | ||
req: impl IntoStreamingRequest<Message = Result<Message, Status>, Stream: Unpin> + Send, | ||
) -> Result<Response<Message>, Status> { | ||
let mut req = pin!(req.into_streaming_request().into_inner()); | ||
while let Some(message) = req.try_next().await? { | ||
println!("Got message: {message:?}") | ||
} | ||
|
||
Ok(Response::new(Message {})) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::test_server::Test; | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_request_handler() { | ||
let incoming_messages = tokio_stream::iter([Message {}, Message {}].map(Ok)); | ||
let svc = Svc; | ||
svc.test_request(incoming_messages).await.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
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