hala_io_test/
lib.rs

1use std::{io, sync::Once};
2
3use futures::{
4    executor::{LocalPool, LocalSpawner},
5    future::BoxFuture,
6    Future,
7};
8
9use hala_io_driver::*;
10
11pub use futures;
12
13pub use hala_io_test_derive::*;
14
15static START: Once = Once::new();
16
17pub fn socket_tester<T, Fut>(label: &'static str, test: T)
18where
19    T: FnOnce() -> Fut + 'static,
20    Fut: Future<Output = ()> + 'static,
21{
22    START.call_once(|| {
23        _ = register_driver(mio_driver());
24    });
25
26    let _guard = PollGuard::new(None).unwrap();
27
28    let mut local_pool = LocalPool::new();
29
30    register_local_io_spawner(TesterIoSpawner {
31        spawner: local_pool.spawner(),
32    });
33
34    log::trace!("start io test(st,{})", label);
35
36    local_pool.run_until(test());
37}
38
39struct TesterIoSpawner {
40    spawner: LocalSpawner,
41}
42
43impl IoSpawner for TesterIoSpawner {
44    fn spawn(&self, fut: BoxFuture<'static, std::io::Result<()>>) -> std::io::Result<()> {
45        use futures::task::SpawnExt;
46
47        self.spawner
48            .spawn(async move {
49                match fut.await {
50                    Ok(_) => {}
51                    Err(err) => {
52                        log::error!("IoSpawner catch err={}", err);
53                    }
54                }
55            })
56            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))
57    }
58}