prost_stream/
lib.rs

1// Disallow warnings when running tests.
2#![cfg_attr(test, deny(warnings))]
3// Disallow warnings in examples.
4#![doc(test(attr(deny(warnings))))]
5
6#![cfg_attr(feature="async", doc=include_str!("../README.md"))]
7
8mod stream;
9pub use stream::*;
10
11// cargo test --all-features 
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16
17    #[derive(Clone, PartialEq, ::prost::Message)]
18    pub struct Ping {
19        #[prost(uint64, tag = "1")]
20        pub id: u64,
21    }
22
23    #[derive(Clone, PartialEq, ::prost::Message)]
24    pub struct Pong {
25        #[prost(uint64, tag = "1")]
26        pub id: u64,
27    }
28
29    #[test]
30    fn it_works() -> anyhow::Result<()> {
31        use std::net::TcpListener;
32        use std::net::TcpStream;
33
34        let listener = TcpListener::bind("127.0.0.1:0")?;
35        let addr = listener.local_addr()?;
36
37        std::thread::spawn(move || {
38            let (stream, _) = listener.accept()?;
39            let mut stream = Stream::new(stream);
40
41            let msg: Ping = stream.recv()?;
42            assert_eq!(msg, Ping{ id: 1234 });
43            stream.send(&Pong{ id: 9527 })?;
44
45            let msg: Ping = stream.recv()?;
46            assert_eq!(msg, Ping{ id: 4321 });
47            stream.send(&Pong{ id: 7259 })?;
48
49            anyhow::Result::<()>::Ok(())
50        });
51
52        let client = TcpStream::connect(addr)?;
53        let mut client = Stream::new(client);
54
55        client.send(&Ping{ id: 1234 })?;
56        let pong: Pong = client.recv()?;
57        assert_eq!(pong, Pong{ id: 9527 });
58
59        client.send(&Ping{ id: 4321 })?;
60        let pong: Pong = client.recv()?;
61        assert_eq!(pong, Pong{ id: 7259 });
62
63        Ok(())
64    }
65
66    #[cfg(feature = "async")]
67    #[tokio::test]
68    async fn test_async() -> anyhow::Result<()> {
69        use tokio::net::TcpListener;
70        use tokio::net::TcpStream;
71
72        let listener = TcpListener::bind("127.0.0.1:0").await?;
73        let addr = listener.local_addr()?;
74
75        tokio::spawn(async move {
76            let (stream, _) = listener.accept().await?;
77            let mut stream = AsyncStream::new(stream);
78
79            let msg: Ping = stream.recv().await?;
80            assert_eq!(msg, Ping{ id: 1234 });
81            stream.send(&Pong{ id: 9527 }).await?;
82
83            let msg: Ping = stream.recv().await?;
84            assert_eq!(msg, Ping{ id: 4321 });
85            stream.send(&Pong{ id: 7259 }).await?;
86
87            anyhow::Result::<()>::Ok(())
88        });
89
90        let client = TcpStream::connect(addr).await?;
91        let mut client = AsyncStream::new(client);
92
93        client.send(&Ping{ id: 1234 }).await?;
94        let pong: Pong = client.recv().await?;
95        assert_eq!(pong, Pong{ id: 9527 });
96
97        client.send(&Ping{ id: 4321 }).await?;
98        let pong: Pong = client.recv().await?;
99        assert_eq!(pong, Pong{ id: 7259 });
100
101        Ok(())
102    }
103}