summaryrefslogtreecommitdiffstats
path: root/vendor/nix/test/test_poll.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/nix/test/test_poll.rs
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/nix/test/test_poll.rs')
-rw-r--r--vendor/nix/test/test_poll.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/vendor/nix/test/test_poll.rs b/vendor/nix/test/test_poll.rs
new file mode 100644
index 000000000..045ccd3df
--- /dev/null
+++ b/vendor/nix/test/test_poll.rs
@@ -0,0 +1,82 @@
+use nix::{
+ errno::Errno,
+ poll::{poll, PollFd, PollFlags},
+ unistd::{close, pipe, write},
+};
+use std::os::unix::io::{BorrowedFd, FromRawFd, OwnedFd};
+
+macro_rules! loop_while_eintr {
+ ($poll_expr: expr) => {
+ loop {
+ match $poll_expr {
+ Ok(nfds) => break nfds,
+ Err(Errno::EINTR) => (),
+ Err(e) => panic!("{}", e),
+ }
+ }
+ };
+}
+
+#[test]
+fn test_poll() {
+ let (r, w) = pipe().unwrap();
+ let r = unsafe { OwnedFd::from_raw_fd(r) };
+ let mut fds = [PollFd::new(&r, PollFlags::POLLIN)];
+
+ // Poll an idle pipe. Should timeout
+ let nfds = loop_while_eintr!(poll(&mut fds, 100));
+ assert_eq!(nfds, 0);
+ assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));
+
+ write(w, b".").unwrap();
+
+ // Poll a readable pipe. Should return an event.
+ let nfds = poll(&mut fds, 100).unwrap();
+ assert_eq!(nfds, 1);
+ assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
+ close(w).unwrap();
+}
+
+// ppoll(2) is the same as poll except for how it handles timeouts and signals.
+// Repeating the test for poll(2) should be sufficient to check that our
+// bindings are correct.
+#[cfg(any(
+ target_os = "android",
+ target_os = "dragonfly",
+ target_os = "freebsd",
+ target_os = "linux"
+))]
+#[test]
+fn test_ppoll() {
+ use nix::poll::ppoll;
+ use nix::sys::signal::SigSet;
+ use nix::sys::time::{TimeSpec, TimeValLike};
+
+ let timeout = TimeSpec::milliseconds(1);
+ let (r, w) = pipe().unwrap();
+ let r = unsafe { OwnedFd::from_raw_fd(r) };
+ let mut fds = [PollFd::new(&r, PollFlags::POLLIN)];
+
+ // Poll an idle pipe. Should timeout
+ let sigset = SigSet::empty();
+ let nfds = loop_while_eintr!(ppoll(&mut fds, Some(timeout), Some(sigset)));
+ assert_eq!(nfds, 0);
+ assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));
+
+ write(w, b".").unwrap();
+
+ // Poll a readable pipe. Should return an event.
+ let nfds = ppoll(&mut fds, Some(timeout), None).unwrap();
+ assert_eq!(nfds, 1);
+ assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
+ close(w).unwrap();
+}
+
+#[test]
+fn test_pollfd_events() {
+ let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
+ let mut pfd = PollFd::new(&fd_zero, PollFlags::POLLIN);
+ assert_eq!(pfd.events(), PollFlags::POLLIN);
+ pfd.set_events(PollFlags::POLLOUT);
+ assert_eq!(pfd.events(), PollFlags::POLLOUT);
+}