fluvio_future/
test_util.rs

1/// run async expression and assert based on result value
2#[macro_export]
3macro_rules! assert_async_block {
4    ($ft_exp:expr) => {{
5        let ft = $ft_exp;
6
7        #[cfg(not(target_arch = "wasm32"))]
8        match fluvio_future::task::run_block_on(ft) {
9            Ok(_) => debug!("finished run"),
10            Err(err) => assert!(false, "error {:?}", err),
11        }
12        #[cfg(target_arch = "wasm32")]
13        fluvio_future::task::run_block_on(ft)
14    }};
15}
16
17#[cfg(test)]
18mod test {
19
20    use std::io::Error;
21    use std::pin::Pin;
22    use std::task::Context;
23    use std::task::Poll;
24
25    use futures_lite::Future;
26    use futures_lite::future::poll_fn;
27    use tracing::debug;
28
29    use crate::test_async;
30
31    // actual test run
32
33    #[test_async]
34    async fn async_derive_test() -> Result<(), Error> {
35        debug!("I am live");
36        Ok(())
37    }
38
39    #[test_async(ignore)]
40    async fn async_derive_test_ignore() -> Result<(), Error> {
41        debug!("I am live");
42        Ok(())
43    }
44
45    #[fluvio_future::test]
46    async fn simple_test() {
47        assert_eq!(1, "x".len());
48    }
49
50    #[fluvio_future::test(ignore)]
51    async fn simple_test_ignore() {
52        assert_eq!(1, "x".len());
53    }
54
55    #[test]
56    fn test_1_sync_example() {
57        async fn test_1() -> Result<(), Error> {
58            debug!("works");
59            Ok(())
60        }
61
62        let ft = async { test_1().await };
63
64        assert_async_block!(ft);
65    }
66
67    struct TestFuture {}
68
69    impl Future for TestFuture {
70        type Output = u16;
71
72        fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
73            Poll::Ready(2)
74        }
75    }
76
77    #[test_async]
78    async fn test_future() -> Result<(), Error> {
79        let t = TestFuture {};
80        let v: u16 = t.await;
81        assert_eq!(v, 2);
82        Ok(())
83    }
84
85    fn test_poll(_cx: &mut Context) -> Poll<u16> {
86        Poll::Ready(4)
87    }
88
89    #[test_async]
90    async fn test_future_with_poll() -> Result<(), Error> {
91        assert_eq!(poll_fn(test_poll).await, 4);
92        Ok(())
93    }
94}