1 use nix::{
2     errno::Errno,
3     poll::{PollFlags, poll, PollFd},
4     unistd::{write, pipe}
5 };
6 
7 macro_rules! loop_while_eintr {
8     ($poll_expr: expr) => {
9         loop {
10             match $poll_expr {
11                 Ok(nfds) => break nfds,
12                 Err(Errno::EINTR) => (),
13                 Err(e) => panic!("{}", e)
14             }
15         }
16     }
17 }
18 
19 #[test]
test_poll()20 fn test_poll() {
21     let (r, w) = pipe().unwrap();
22     let mut fds = [PollFd::new(r, PollFlags::POLLIN)];
23 
24     // Poll an idle pipe.  Should timeout
25     let nfds = loop_while_eintr!(poll(&mut fds, 100));
26     assert_eq!(nfds, 0);
27     assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));
28 
29     write(w, b".").unwrap();
30 
31     // Poll a readable pipe.  Should return an event.
32     let nfds = poll(&mut fds, 100).unwrap();
33     assert_eq!(nfds, 1);
34     assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
35 }
36 
37 // ppoll(2) is the same as poll except for how it handles timeouts and signals.
38 // Repeating the test for poll(2) should be sufficient to check that our
39 // bindings are correct.
40 #[cfg(any(target_os = "android",
41           target_os = "dragonfly",
42           target_os = "freebsd",
43           target_os = "linux"))]
44 #[test]
test_ppoll()45 fn test_ppoll() {
46     use nix::poll::ppoll;
47     use nix::sys::signal::SigSet;
48     use nix::sys::time::{TimeSpec, TimeValLike};
49 
50     let timeout = TimeSpec::milliseconds(1);
51     let (r, w) = pipe().unwrap();
52     let mut fds = [PollFd::new(r, PollFlags::POLLIN)];
53 
54     // Poll an idle pipe.  Should timeout
55     let sigset = SigSet::empty();
56     let nfds = loop_while_eintr!(ppoll(&mut fds, Some(timeout), sigset));
57     assert_eq!(nfds, 0);
58     assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));
59 
60     write(w, b".").unwrap();
61 
62     // Poll a readable pipe.  Should return an event.
63     let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap();
64     assert_eq!(nfds, 1);
65     assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
66 }
67 
68 #[test]
test_pollfd_fd()69 fn test_pollfd_fd() {
70     use std::os::unix::io::AsRawFd;
71 
72     let pfd = PollFd::new(0x1234, PollFlags::empty());
73     assert_eq!(pfd.as_raw_fd(), 0x1234);
74 }
75 
76 #[test]
test_pollfd_events()77 fn test_pollfd_events() {
78     let mut pfd = PollFd::new(-1, PollFlags::POLLIN);
79     assert_eq!(pfd.events(), PollFlags::POLLIN);
80     pfd.set_events(PollFlags::POLLOUT);
81     assert_eq!(pfd.events(), PollFlags::POLLOUT);
82 }
83