Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: Add example for server_send_stream #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions example/async-stream-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ async fn main() {
let sc1 = sc.clone();
let t6 = tokio::spawn(echo_null_stream(sc1));

let t7 = tokio::spawn(echo_default_value(sc));
let sc1 = sc.clone();
let t7 = tokio::spawn(echo_default_value(sc1));

let t8 = tokio::spawn(server_send_stream(sc));

let _ = tokio::join!(t1, t2, t3, t4, t5, t6, t7);
let _ = tokio::join!(t1, t2, t3, t4, t5, t6, t7, t8);
}

fn default_ctx() -> Context {
Expand Down Expand Up @@ -201,3 +204,18 @@ async fn echo_default_value(cli: streaming_ttrpc::StreamingClient) {
assert_eq!(received.seq, 0);
assert_eq!(received.msg, "");
}

#[cfg(unix)]
async fn server_send_stream(cli: streaming_ttrpc::StreamingClient) {
let mut stream = cli
.server_send_stream(default_ctx(), &Default::default())
.await
.unwrap();

let mut seq = 0;
while let Some(received) = stream.recv().await.unwrap() {
assert_eq!(received.seq, seq);
assert_eq!(received.msg, "hello");
seq += 1;
}
}
19 changes: 19 additions & 0 deletions example/async-stream-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ impl streaming_ttrpc::Streaming for StreamingService {

Ok(())
}

async fn server_send_stream(
&self,
_ctx: &::ttrpc::r#async::TtrpcContext,
_: empty::Empty,
s: ::ttrpc::r#async::ServerStreamSender<streaming::EchoPayload>,
) -> ::ttrpc::Result<()> {
let mut seq = 0;
while seq < 10 {
sleep(std::time::Duration::from_secs(1)).await;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sleep 1 sec is too long to an example in my opinion. How about using 100ms instead? thanks.

let mut e = streaming::EchoPayload::new();
e.seq = seq;
e.msg = format!("hello");
s.send(&e).await.unwrap();
seq += 1;
}

Ok(())
}
}

#[cfg(windows)]
Expand Down
1 change: 1 addition & 0 deletions example/protocols/protos/streaming.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ service Streaming {
rpc EchoNull(stream EchoPayload) returns (google.protobuf.Empty);
rpc EchoNullStream(stream EchoPayload) returns (stream google.protobuf.Empty);
rpc EchoDefaultValue(EchoPayload) returns (stream EchoPayload);
rpc ServerSendStream(google.protobuf.Empty) returns (stream EchoPayload);
}

message EchoPayload {
Expand Down
Loading