|
| 1 | +// Regression test for per-stream frame ordering on the async client. |
| 2 | +// |
| 3 | +// A server-streaming handler sends one payload and returns, producing a DATA |
| 4 | +// frame immediately followed by a REMOTE_CLOSED frame on the wire. The client |
| 5 | +// calls this 1000 times on a multi-threaded runtime and asserts that every |
| 6 | +// iteration receives the payload before EOF. |
| 7 | + |
| 8 | +mod protocols; |
| 9 | + |
| 10 | +use std::sync::Arc; |
| 11 | + |
| 12 | +use async_trait::async_trait; |
| 13 | +use protocols::asynchronous::{empty, streaming, streaming_ttrpc}; |
| 14 | +use ttrpc::asynchronous::{Client, Server}; |
| 15 | + |
| 16 | +const SOCK: &str = "unix:///tmp/ttrpc-test-close-order"; |
| 17 | +const ITERATIONS: usize = 1000; |
| 18 | + |
| 19 | +struct Svc; |
| 20 | + |
| 21 | +#[async_trait] |
| 22 | +impl streaming_ttrpc::Streaming for Svc { |
| 23 | + async fn echo( |
| 24 | + &self, |
| 25 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 26 | + _req: streaming::EchoPayload, |
| 27 | + ) -> ::ttrpc::Result<streaming::EchoPayload> { |
| 28 | + unimplemented!() |
| 29 | + } |
| 30 | + |
| 31 | + async fn echo_stream( |
| 32 | + &self, |
| 33 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 34 | + _s: ::ttrpc::r#async::ServerStream<streaming::EchoPayload, streaming::EchoPayload>, |
| 35 | + ) -> ::ttrpc::Result<()> { |
| 36 | + unimplemented!() |
| 37 | + } |
| 38 | + |
| 39 | + async fn sum_stream( |
| 40 | + &self, |
| 41 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 42 | + _s: ::ttrpc::r#async::ServerStreamReceiver<streaming::Part>, |
| 43 | + ) -> ::ttrpc::Result<streaming::Sum> { |
| 44 | + unimplemented!() |
| 45 | + } |
| 46 | + |
| 47 | + async fn divide_stream( |
| 48 | + &self, |
| 49 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 50 | + _sum: streaming::Sum, |
| 51 | + _s: ::ttrpc::r#async::ServerStreamSender<streaming::Part>, |
| 52 | + ) -> ::ttrpc::Result<()> { |
| 53 | + unimplemented!() |
| 54 | + } |
| 55 | + |
| 56 | + async fn echo_null( |
| 57 | + &self, |
| 58 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 59 | + _s: ::ttrpc::r#async::ServerStreamReceiver<streaming::EchoPayload>, |
| 60 | + ) -> ::ttrpc::Result<empty::Empty> { |
| 61 | + unimplemented!() |
| 62 | + } |
| 63 | + |
| 64 | + async fn echo_null_stream( |
| 65 | + &self, |
| 66 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 67 | + _s: ::ttrpc::r#async::ServerStream<empty::Empty, streaming::EchoPayload>, |
| 68 | + ) -> ::ttrpc::Result<()> { |
| 69 | + unimplemented!() |
| 70 | + } |
| 71 | + |
| 72 | + async fn echo_default_value( |
| 73 | + &self, |
| 74 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 75 | + _req: streaming::EchoPayload, |
| 76 | + s: ::ttrpc::r#async::ServerStreamSender<streaming::EchoPayload>, |
| 77 | + ) -> ::ttrpc::Result<()> { |
| 78 | + s.send(&streaming::EchoPayload { |
| 79 | + seq: 1, |
| 80 | + msg: "hello".into(), |
| 81 | + ..Default::default() |
| 82 | + }) |
| 83 | + .await |
| 84 | + .unwrap(); |
| 85 | + Ok(()) |
| 86 | + } |
| 87 | + |
| 88 | + async fn server_send_stream( |
| 89 | + &self, |
| 90 | + _ctx: &::ttrpc::r#async::TtrpcContext, |
| 91 | + _req: empty::Empty, |
| 92 | + _s: ::ttrpc::r#async::ServerStreamSender<streaming::EchoPayload>, |
| 93 | + ) -> ::ttrpc::Result<()> { |
| 94 | + unimplemented!() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[tokio::main(flavor = "multi_thread")] |
| 99 | +async fn main() { |
| 100 | + let path = SOCK.strip_prefix("unix://").unwrap(); |
| 101 | + let _ = std::fs::remove_file(path); |
| 102 | + |
| 103 | + let service = streaming_ttrpc::create_streaming(Arc::new(Svc {})); |
| 104 | + let mut server = Server::new().bind(SOCK).unwrap().register_service(service); |
| 105 | + server.start().await.unwrap(); |
| 106 | + |
| 107 | + let c = Client::connect(SOCK).await.unwrap(); |
| 108 | + let sc = streaming_ttrpc::StreamingClient::new(c); |
| 109 | + |
| 110 | + let mut got_payload = 0usize; |
| 111 | + let mut eof_without_payload = 0usize; |
| 112 | + |
| 113 | + for _ in 0..ITERATIONS { |
| 114 | + let ctx = ttrpc::context::with_timeout(5_000_000_000); |
| 115 | + let mut stream = sc |
| 116 | + .echo_default_value(ctx, &streaming::EchoPayload::default()) |
| 117 | + .await |
| 118 | + .expect("failed to open stream"); |
| 119 | + |
| 120 | + match stream.recv().await { |
| 121 | + Ok(Some(_)) => got_payload += 1, |
| 122 | + Ok(None) => eof_without_payload += 1, |
| 123 | + Err(e) => panic!("unexpected error from recv: {:?}", e), |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + server.shutdown().await.unwrap(); |
| 128 | + |
| 129 | + eprintln!( |
| 130 | + "iterations={ITERATIONS} got_payload={got_payload} eof_without_payload={eof_without_payload}" |
| 131 | + ); |
| 132 | + assert_eq!( |
| 133 | + eof_without_payload, 0, |
| 134 | + "server sent a payload on every iteration but the client saw EOF \ |
| 135 | + {eof_without_payload}/{ITERATIONS} times (data frames dropped)" |
| 136 | + ); |
| 137 | +} |
0 commit comments