aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/tcp.rs34
-rw-r--r--tests/unix.rs34
2 files changed, 68 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]
2async 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}
diff --git a/tests/unix.rs b/tests/unix.rs
new file mode 100644
index 0000000..bfa5a69
--- /dev/null
+++ b/tests/unix.rs
@@ -0,0 +1,34 @@
1#[tokio::test]
2async 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::unix::bind("./hello.service").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::unix::connect("./hello.service").await.unwrap());
31 let client = HelloClient::new(channel);
32 let greeting = client.hello("World".into()).await.unwrap();
33 assert_eq!(greeting, "Hello, World!");
34}