Skip to content

Commit 76bb1aa

Browse files
committed
Add regression test
Signed-off-by: Jiahao XU <[email protected]>
1 parent 9b28a94 commit 76bb1aa

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/unix.rs

+44-24
Original file line numberDiff line numberDiff line change
@@ -558,55 +558,75 @@ mod test {
558558

559559
use crate::{test::run_named_fifo_try_acquire_tests, Client};
560560

561-
use std::sync::Arc;
561+
use std::{
562+
fs::File,
563+
io::{self, Write},
564+
os::unix::io::AsRawFd,
565+
sync::Arc,
566+
};
562567

563568
fn from_imp_client(imp: ClientImp) -> Client {
564569
Client {
565570
inner: Arc::new(imp),
566571
}
567572
}
568573

569-
#[test]
570-
fn test_try_acquire_named_fifo() {
574+
fn new_client_from_fifo() -> (Client, String) {
571575
let file = tempfile::NamedTempFile::new().unwrap();
572576
let fifo_path = file.path().to_owned();
573577
file.close().unwrap(); // Remove the NamedTempFile to create fifo
574578

575579
nix::unistd::mkfifo(&fifo_path, nix::sys::stat::Mode::S_IRWXU).unwrap();
576580

577-
let client = ClientImp::from_fifo(&format!("fifo:{}", fifo_path.to_str().unwrap()))
578-
.unwrap()
579-
.map(from_imp_client)
580-
.unwrap();
581+
let arg = format!("fifo:{}", fifo_path.to_str().unwrap());
581582

582-
run_named_fifo_try_acquire_tests(&client);
583+
(
584+
ClientImp::from_fifo(&arg)
585+
.unwrap()
586+
.map(from_imp_client)
587+
.unwrap(),
588+
arg,
589+
)
583590
}
584591

585-
#[cfg(not(target_os = "linux"))]
586-
#[test]
587-
fn test_try_acquire_annoymous_pipe_linux_specific_optimization() {
588-
use std::{
589-
fs::File,
590-
io::{self, Write},
591-
os::unix::io::AsRawFd,
592-
};
593-
592+
fn new_client_from_pipe() -> (Client, String) {
594593
let (read, write) = nix::unistd::pipe().unwrap();
595594
let read = File::from(read);
596595
let mut write = File::from(write);
597596

598597
write.write_all(b"1").unwrap();
599598

600-
let client = unsafe {
601-
ClientImp::from_pipe(&format!("{},{}", read.as_raw_fd(), write.as_raw_fd()), true)
602-
}
603-
.unwrap()
604-
.map(from_imp_client)
605-
.unwrap();
599+
let arg = format!("{},{}", read.as_raw_fd(), write.as_raw_fd());
606600

601+
(
602+
unsafe { ClientImp::from_pipe(&arg, true) }
603+
.unwrap()
604+
.map(from_imp_client)
605+
.unwrap(),
606+
arg,
607+
)
608+
}
609+
610+
#[test]
611+
fn test_try_acquire_named_fifo() {
612+
run_named_fifo_try_acquire_tests(&new_client_from_fifo().0);
613+
}
614+
615+
#[cfg(not(target_os = "linux"))]
616+
#[test]
617+
fn test_try_acquire_annoymous_pipe_linux_specific_optimization() {
607618
assert_eq!(
608-
client.try_acquire().unwrap_err().kind(),
619+
new_client_from_pipe().0.try_acquire().unwrap_err().kind(),
609620
io::ErrorKind::Unsupported
610621
);
611622
}
623+
624+
#[test]
625+
fn test_string_arg() {
626+
let (client, arg) = new_client_from_fifo();
627+
assert_eq!(client.inner.string_arg(), arg);
628+
629+
let (client, arg) = new_client_from_pipe();
630+
assert_eq!(client.inner.string_arg(), arg);
631+
}
612632
}

0 commit comments

Comments
 (0)