|
| 1 | +mod gen; |
| 2 | + |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use async_trait::async_trait; |
| 6 | +use starpc::client::{OpenStream, PacketReceiver, SrpcClient}; |
| 7 | +use starpc::rpc::PacketWriter; |
| 8 | +use starpc::transport::create_packet_channel; |
| 9 | +use starpc::{Error, Result}; |
| 10 | +// Use Error::Remote for test assertion errors since there's no Error::Remote. |
| 11 | +use tokio::net::TcpStream; |
| 12 | + |
| 13 | +use gen::{EchoMsg, EchoerClient, EchoerClientImpl}; |
| 14 | + |
| 15 | +const BODY_TXT: &str = "hello world via starpc cross-language e2e test"; |
| 16 | + |
| 17 | +/// Opens a new TCP connection per RPC call. |
| 18 | +struct TcpStreamOpener { |
| 19 | + addr: String, |
| 20 | +} |
| 21 | + |
| 22 | +impl TcpStreamOpener { |
| 23 | + fn new(addr: String) -> Self { |
| 24 | + Self { addr } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[async_trait] |
| 29 | +impl OpenStream for TcpStreamOpener { |
| 30 | + async fn open_stream(&self) -> Result<(Arc<dyn PacketWriter>, PacketReceiver)> { |
| 31 | + let stream = TcpStream::connect(&self.addr).await?; |
| 32 | + let (read, write) = tokio::io::split(stream); |
| 33 | + Ok(create_packet_channel(read, write)) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[tokio::main] |
| 38 | +async fn main() { |
| 39 | + let addr = std::env::args() |
| 40 | + .nth(1) |
| 41 | + .expect("usage: integration-client <addr>"); |
| 42 | + |
| 43 | + let opener = TcpStreamOpener::new(addr); |
| 44 | + let client = SrpcClient::new(opener); |
| 45 | + let echo = EchoerClientImpl::new(client); |
| 46 | + |
| 47 | + if let Err(e) = test_unary(&echo).await { |
| 48 | + eprintln!("unary test failed: {}", e); |
| 49 | + std::process::exit(1); |
| 50 | + } |
| 51 | + |
| 52 | + if let Err(e) = test_server_stream(&echo).await { |
| 53 | + eprintln!("server stream test failed: {}", e); |
| 54 | + std::process::exit(1); |
| 55 | + } |
| 56 | + |
| 57 | + if let Err(e) = test_client_stream(&echo).await { |
| 58 | + eprintln!("client stream test failed: {}", e); |
| 59 | + std::process::exit(1); |
| 60 | + } |
| 61 | + |
| 62 | + if let Err(e) = test_bidi_stream(&echo).await { |
| 63 | + eprintln!("bidi stream test failed: {}", e); |
| 64 | + std::process::exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + println!("All tests passed."); |
| 68 | +} |
| 69 | + |
| 70 | +async fn test_unary(echo: &dyn EchoerClient) -> Result<()> { |
| 71 | + println!("Testing Unary RPC..."); |
| 72 | + let req = EchoMsg { |
| 73 | + body: BODY_TXT.to_string(), |
| 74 | + }; |
| 75 | + let resp = echo.echo(&req).await?; |
| 76 | + if resp.body != BODY_TXT { |
| 77 | + return Err(Error::Remote(format!( |
| 78 | + "expected {:?} got {:?}", |
| 79 | + BODY_TXT, resp.body |
| 80 | + ))); |
| 81 | + } |
| 82 | + println!(" PASSED"); |
| 83 | + Ok(()) |
| 84 | +} |
| 85 | + |
| 86 | +async fn test_server_stream(echo: &dyn EchoerClient) -> Result<()> { |
| 87 | + println!("Testing ServerStream RPC..."); |
| 88 | + let req = EchoMsg { |
| 89 | + body: BODY_TXT.to_string(), |
| 90 | + }; |
| 91 | + let stream = echo.echo_server_stream(&req).await?; |
| 92 | + let mut received = 0; |
| 93 | + loop { |
| 94 | + match stream.recv().await { |
| 95 | + Ok(msg) => { |
| 96 | + if msg.body != BODY_TXT { |
| 97 | + return Err(Error::Remote(format!( |
| 98 | + "expected {:?} got {:?}", |
| 99 | + BODY_TXT, msg.body |
| 100 | + ))); |
| 101 | + } |
| 102 | + received += 1; |
| 103 | + } |
| 104 | + Err(Error::StreamClosed) => break, |
| 105 | + Err(e) => return Err(e), |
| 106 | + } |
| 107 | + } |
| 108 | + if received != 5 { |
| 109 | + return Err(Error::Remote(format!( |
| 110 | + "expected 5 messages, got {}", |
| 111 | + received |
| 112 | + ))); |
| 113 | + } |
| 114 | + println!(" PASSED"); |
| 115 | + Ok(()) |
| 116 | +} |
| 117 | + |
| 118 | +async fn test_client_stream(echo: &dyn EchoerClient) -> Result<()> { |
| 119 | + println!("Testing ClientStream RPC..."); |
| 120 | + let stream = echo.echo_client_stream().await?; |
| 121 | + stream |
| 122 | + .send(&EchoMsg { |
| 123 | + body: BODY_TXT.to_string(), |
| 124 | + }) |
| 125 | + .await?; |
| 126 | + let resp = stream.close_and_recv().await?; |
| 127 | + if resp.body != BODY_TXT { |
| 128 | + return Err(Error::Remote(format!( |
| 129 | + "expected {:?} got {:?}", |
| 130 | + BODY_TXT, resp.body |
| 131 | + ))); |
| 132 | + } |
| 133 | + println!(" PASSED"); |
| 134 | + Ok(()) |
| 135 | +} |
| 136 | + |
| 137 | +async fn test_bidi_stream(echo: &dyn EchoerClient) -> Result<()> { |
| 138 | + println!("Testing BidiStream RPC..."); |
| 139 | + let stream = echo.echo_bidi_stream().await?; |
| 140 | + |
| 141 | + // Receive initial message from server. |
| 142 | + let msg = stream.recv().await?; |
| 143 | + if msg.body != "hello from server" { |
| 144 | + return Err(Error::Remote(format!( |
| 145 | + "expected {:?} got {:?}", |
| 146 | + "hello from server", msg.body |
| 147 | + ))); |
| 148 | + } |
| 149 | + |
| 150 | + // Send a message and expect echo. |
| 151 | + stream |
| 152 | + .send(&EchoMsg { |
| 153 | + body: BODY_TXT.to_string(), |
| 154 | + }) |
| 155 | + .await?; |
| 156 | + let resp = stream.recv().await?; |
| 157 | + if resp.body != BODY_TXT { |
| 158 | + return Err(Error::Remote(format!( |
| 159 | + "expected {:?} got {:?}", |
| 160 | + BODY_TXT, resp.body |
| 161 | + ))); |
| 162 | + } |
| 163 | + |
| 164 | + stream.close().await?; |
| 165 | + println!(" PASSED"); |
| 166 | + Ok(()) |
| 167 | +} |
0 commit comments