diff options
| author | diogo464 <[email protected]> | 2025-07-16 10:46:41 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-07-16 10:46:41 +0100 |
| commit | f319d7ab5278a3cfb43d38875d81c28cc2dce1e1 (patch) | |
| tree | cb161fd990643e267bbc373fb09ccd7b689a23b5 /tests/tcp.rs | |
Initial commit - extracted urpc from monorepo
Diffstat (limited to 'tests/tcp.rs')
| -rw-r--r-- | tests/tcp.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/tcp.rs b/tests/tcp.rs new file mode 100644 index 0000000..a2f647d --- /dev/null +++ b/tests/tcp.rs | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #[tokio::test] | ||
| 2 | async fn test_hello_service() { | ||
| 3 | #[urpc::service] | ||
| 4 | trait Hello { | ||
| 5 | type Error = (); | ||
| 6 | |||
| 7 | async fn hello(name: String) -> String; | ||
| 8 | } | ||
| 9 | |||
| 10 | struct HelloServer; | ||
| 11 | |||
| 12 | impl Hello for HelloServer { | ||
| 13 | async fn hello(&self, _ctx: urpc::Context, name: String) -> Result<String, ()> { | ||
| 14 | Ok(format!("Hello, {name}!")) | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | let listener = urpc::tcp::bind("0.0.0.0:3000").await.unwrap(); | ||
| 19 | |||
| 20 | // spawn the server | ||
| 21 | tokio::spawn(async move { | ||
| 22 | urpc::Server::default() | ||
| 23 | .with_listener(listener) | ||
| 24 | .with_service(HelloServer.into_service()) | ||
| 25 | .serve() | ||
| 26 | .await | ||
| 27 | }); | ||
| 28 | |||
| 29 | // create a client | ||
| 30 | let channel = urpc::ClientChannel::new(urpc::tcp::connect("127.0.0.1:3000").await.unwrap()); | ||
| 31 | let client = HelloClient::new(channel); | ||
| 32 | let greeting = client.hello("World".into()).await.unwrap(); | ||
| 33 | assert_eq!(greeting, "Hello, World!"); | ||
| 34 | } | ||
