aboutsummaryrefslogtreecommitdiff
path: root/tests/tcp.rs
blob: a2f647daa4f69b219ac859f3643bc772422e42a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#[tokio::test]
async fn test_hello_service() {
    #[urpc::service]
    trait Hello {
        type Error = ();

        async fn hello(name: String) -> String;
    }

    struct HelloServer;

    impl Hello for HelloServer {
        async fn hello(&self, _ctx: urpc::Context, name: String) -> Result<String, ()> {
            Ok(format!("Hello, {name}!"))
        }
    }

    let listener = urpc::tcp::bind("0.0.0.0:3000").await.unwrap();

    // spawn the server
    tokio::spawn(async move {
        urpc::Server::default()
            .with_listener(listener)
            .with_service(HelloServer.into_service())
            .serve()
            .await
    });

    // create a client
    let channel = urpc::ClientChannel::new(urpc::tcp::connect("127.0.0.1:3000").await.unwrap());
    let client = HelloClient::new(channel);
    let greeting = client.hello("World".into()).await.unwrap();
    assert_eq!(greeting, "Hello, World!");
}