11#![ cfg( linux_all) ]
22
3- use std:: env:: temp_dir;
3+ use std:: {
4+ ops:: { Deref , DerefMut } ,
5+ rc:: Rc ,
6+ } ;
47
58use compio_fs:: {
69 File ,
@@ -10,25 +13,62 @@ use compio_io::{AsyncRead, AsyncReadExt, AsyncWriteAt, AsyncWriteExt};
1013use compio_net:: UnixStream ;
1114use compio_runtime:: Runtime ;
1215use futures_util:: future:: join;
13- use tempfile:: NamedTempFile ;
16+ use tempfile:: { NamedTempFile , TempPath } ;
1417
1518const HELLO : & [ u8 ] = b"hello world..." ;
1619
17- async fn uds ( id : u8 ) -> ( UnixStream , UnixStream ) {
18- let path = temp_dir ( ) . join ( format ! ( "compio-{id}.sock" ) ) ;
19- let listener = compio_net:: UnixListener :: bind ( & path) . await . unwrap ( ) ;
20- let ( a, b) = join ( UnixStream :: connect ( & path) , listener. accept ( ) ) . await ;
21- ( a. unwrap ( ) , b. unwrap ( ) . 0 )
20+ struct Guard {
21+ stream : UnixStream ,
22+ _inner : Rc < TempPath > ,
23+ }
24+
25+ impl Deref for Guard {
26+ type Target = UnixStream ;
27+
28+ fn deref ( & self ) -> & Self :: Target {
29+ & self . stream
30+ }
31+ }
32+
33+ impl DerefMut for Guard {
34+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
35+ & mut self . stream
36+ }
37+ }
38+
39+ async fn uds ( ) -> ( Guard , Guard ) {
40+ let path: Rc < _ > = tempfile:: Builder :: new ( )
41+ . prefix ( "compio-" )
42+ . suffix ( ".sock" )
43+ . tempfile ( )
44+ . expect ( "failed to create random path for domain socket" )
45+ . into_temp_path ( )
46+ . into ( ) ;
47+
48+ _ = compio_fs:: remove_file ( & * path) . await ;
49+
50+ let listener = compio_net:: UnixListener :: bind ( & * path) . await . unwrap ( ) ;
51+ let ( a, b) = join ( UnixStream :: connect ( & * path) , listener. accept ( ) ) . await ;
52+ (
53+ Guard {
54+ stream : a. unwrap ( ) ,
55+ _inner : path. clone ( ) ,
56+ } ,
57+ Guard {
58+ stream : b. unwrap ( ) . 0 ,
59+ _inner : path,
60+ } ,
61+ )
2262}
2363
2464#[ compio_macros:: test]
2565async fn splice_uds_to_pipe ( ) {
26- let ( r, mut w) = uds ( 1 ) . await ;
66+ let ( r, mut w) = uds ( ) . await ;
2767 w. write_all ( HELLO ) . await . unwrap ( ) ;
2868
2969 let ( mut rx, tx) = anonymous ( ) . unwrap ( ) ;
3070
31- let n = splice ( & r, & tx, HELLO . len ( ) ) . await . unwrap ( ) ;
71+ let n = splice ( & * r, & tx, HELLO . len ( ) ) . await . unwrap ( ) ;
3272 assert_eq ! ( n, HELLO . len( ) ) ;
3373
3474 drop ( tx) ;
@@ -41,13 +81,13 @@ async fn splice_uds_to_pipe() {
4181
4282#[ compio_macros:: test]
4383async fn splice_pipe_to_uds ( ) {
44- let ( mut r, w) = uds ( 2 ) . await ;
84+ let ( mut r, w) = uds ( ) . await ;
4585 let ( rx, mut tx) = anonymous ( ) . unwrap ( ) ;
4686
4787 tx. write_all ( HELLO ) . await . unwrap ( ) ;
4888 drop ( tx) ;
4989
50- let n = splice ( & rx, & w, HELLO . len ( ) ) . await . unwrap ( ) ;
90+ let n = splice ( & rx, & * w, HELLO . len ( ) ) . await . unwrap ( ) ;
5191 assert_eq ! ( n, HELLO . len( ) ) ;
5292
5393 let ( len, contents) = r. read ( Vec :: with_capacity ( HELLO . len ( ) + 10 ) ) . await . unwrap ( ) ;
0 commit comments