Skip to content

Commit d9c22a6

Browse files
committed
Add regression test
Signed-off-by: Jiahao XU <[email protected]>
1 parent 423c058 commit d9c22a6

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ mod test {
644644
client.try_acquire().unwrap().unwrap();
645645
}
646646

647-
#[cfg(any(windows, target_os = "linux"))]
647+
#[cfg(windows)]
648648
#[test]
649649
fn test_try_acquire() {
650650
let client = Client::new(0).unwrap();

src/unix.rs

+51-24
Original file line numberDiff line numberDiff line change
@@ -558,55 +558,82 @@ 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());
600+
601+
(
602+
unsafe { ClientImp::from_pipe(&arg, true) }
603+
.unwrap()
604+
.map(from_imp_client)
605+
.unwrap(),
606+
arg,
607+
)
608+
}
606609

610+
#[test]
611+
fn test_try_acquire_named_fifo() {
612+
run_named_fifo_try_acquire_tests(&new_client_from_fifo().0);
613+
}
614+
615+
#[test]
616+
fn test_try_acquire_annoymous_pipe_linux_specific_optimization() {
617+
#[cfg(not(target_os = "linux"))]
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
);
622+
623+
#[cfg(target_os = "linux")]
624+
{
625+
let client = new_client_from_pipe().0;
626+
client.acquire().unwrap().drop_without_releasing();
627+
run_named_fifo_try_acquire_tests(&client);
628+
}
629+
}
630+
631+
#[test]
632+
fn test_string_arg() {
633+
let (client, arg) = new_client_from_fifo();
634+
assert_eq!(client.inner.string_arg(), arg);
635+
636+
let (client, arg) = new_client_from_pipe();
637+
assert_eq!(client.inner.string_arg(), arg);
611638
}
612639
}

0 commit comments

Comments
 (0)