Skip to content

Commit 9f20a09

Browse files
authored
feat(tls,fs): fix tests (#757)
1 parent 22cabd3 commit 9f20a09

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

compio-fs/tests/splice.rs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![cfg(linux_all)]
22

3-
use std::env::temp_dir;
3+
use std::{
4+
ops::{Deref, DerefMut},
5+
rc::Rc,
6+
};
47

58
use compio_fs::{
69
File,
@@ -10,25 +13,62 @@ use compio_io::{AsyncRead, AsyncReadExt, AsyncWriteAt, AsyncWriteExt};
1013
use compio_net::UnixStream;
1114
use compio_runtime::Runtime;
1215
use futures_util::future::join;
13-
use tempfile::NamedTempFile;
16+
use tempfile::{NamedTempFile, TempPath};
1417

1518
const 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]
2565
async 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]
4383
async 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();

compio-tls/tests/connect.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::io::ErrorKind;
2+
13
use compio_io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
24
use compio_net::TcpStream;
35
use compio_tls::TlsConnector;
@@ -16,7 +18,13 @@ async fn connect(connector: TlsConnector) {
1618
stream.flush().await.unwrap();
1719
let (_, res) = stream.read_to_end(vec![]).await.unwrap();
1820
println!("{}", String::from_utf8_lossy(&res));
19-
stream.shutdown().await.unwrap();
21+
// seems like badssl will shutdown tcp before us
22+
use ErrorKind::*;
23+
match stream.shutdown().await {
24+
Ok(_) => {}
25+
Err(e) if matches!(e.kind(), NotConnected) => {}
26+
res => res.expect("failed to shutdown"),
27+
}
2028
}
2129

2230
#[cfg(feature = "native-tls")]

0 commit comments

Comments
 (0)