1#[cfg(unix)]
7use compio::net::{UnixListener, UnixStream};
8#[cfg(unix)]
9use std::path::Path;
10
11#[cfg(unix)]
12pub async fn connect<P: AsRef<Path>>(path: P) -> std::io::Result<UnixStream> {
26 UnixStream::connect(path).await
27}
28
29#[cfg(unix)]
30pub async fn bind<P: AsRef<Path>>(path: P) -> std::io::Result<UnixListener> {
46 let path_ref = path.as_ref();
48 if path_ref.exists() {
49 std::fs::remove_file(path_ref)?;
50 }
51
52 UnixListener::bind(path).await
53}
54
55#[cfg(unix)]
56pub async fn accept(listener: &UnixListener) -> std::io::Result<UnixStream> {
58 let (stream, _addr) = listener.accept().await?;
59 Ok(stream)
60}
61
62#[cfg(test)]
63#[cfg(unix)]
64mod tests {
65 use super::*;
66
67 #[compio::test]
68 async fn test_ipc_connect_bind() {
69 let path = "/tmp/monocoque_test_ipc.sock";
70
71 let _ = std::fs::remove_file(path);
73
74 let listener = bind(path).await.unwrap();
75
76 let accept_handle = compio::runtime::spawn(async move { accept(&listener).await });
78
79 compio::time::sleep(std::time::Duration::from_millis(10)).await;
81
82 let client = connect(path).await.unwrap();
84
85 let server = accept_handle.await.unwrap();
87
88 assert!(client.peer_addr().is_ok());
89 assert!(server.local_addr().is_ok());
90
91 drop(client);
93 drop(server);
94 let _ = std::fs::remove_file(path);
95 }
96}