Skip to content

Commit e16aa7b

Browse files
committed
exmaple: Update stream example to verify #208
Add test for issues: #169, #207. Signed-off-by: Tim Zhang <[email protected]>
1 parent 092a9f8 commit e16aa7b

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

example/async-stream-client.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ async fn main() {
4141
let sc1 = sc.clone();
4242
let t5 = tokio::spawn(echo_null(sc1));
4343

44-
let t6 = tokio::spawn(echo_null_stream(sc));
44+
let sc1 = sc.clone();
45+
let t6 = tokio::spawn(echo_null_stream(sc1));
46+
47+
let t7 = tokio::spawn(echo_default_value(sc));
4548

46-
let _ = tokio::join!(t1, t2, t3, t4, t5, t6);
49+
let _ = tokio::join!(t1, t2, t3, t4, t5, t6, t7);
4750
}
4851

4952
fn default_ctx() -> Context {
@@ -185,3 +188,16 @@ async fn echo_null_stream(cli: streaming_ttrpc::StreamingClient) {
185188
.unwrap()
186189
.unwrap();
187190
}
191+
192+
#[cfg(unix)]
193+
async fn echo_default_value(cli: streaming_ttrpc::StreamingClient) {
194+
let mut stream = cli
195+
.echo_default_value(default_ctx(), &Default::default()) // send default value to verify #208
196+
.await
197+
.unwrap();
198+
199+
let received = stream.recv().await.unwrap().unwrap();
200+
201+
assert_eq!(received.seq, 0);
202+
assert_eq!(received.msg, "");
203+
}

example/async-stream-server.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use log::{info, LevelFilter};
1313
#[cfg(unix)]
1414
use protocols::r#async::{empty, streaming, streaming_ttrpc};
1515
#[cfg(unix)]
16-
use ttrpc::asynchronous::Server;
16+
use ttrpc::{asynchronous::Server, Error};
1717

1818
#[cfg(unix)]
1919
use async_trait::async_trait;
@@ -134,6 +134,24 @@ impl streaming_ttrpc::Streaming for StreamingService {
134134
}
135135
Ok(())
136136
}
137+
138+
// It verifies PR #208
139+
async fn echo_default_value(
140+
&self,
141+
_ctx: &::ttrpc::r#async::TtrpcContext,
142+
e: streaming::EchoPayload,
143+
s: ::ttrpc::r#async::ServerStreamSender<streaming::EchoPayload>,
144+
) -> ::ttrpc::Result<()> {
145+
if e.seq != 0 || !e.msg.is_empty() {
146+
return Err(Error::Others(
147+
"Expect a request with empty payload to verify #208".to_string(),
148+
));
149+
}
150+
151+
s.send(&e).await.unwrap();
152+
153+
Ok(())
154+
}
137155
}
138156

139157
#[cfg(windows)]

example/protocols/protos/streaming.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ service Streaming {
3232
rpc DivideStream(Sum) returns (stream Part);
3333
rpc EchoNull(stream EchoPayload) returns (google.protobuf.Empty);
3434
rpc EchoNullStream(stream EchoPayload) returns (stream google.protobuf.Empty);
35+
rpc EchoDefaultValue(EchoPayload) returns (stream EchoPayload);
3536
}
3637

3738
message EchoPayload {

0 commit comments

Comments
 (0)