| Andrew Walbran | 12f6140 | 2020-10-14 11:10:53 +0100 | [diff] [blame^] | 1 | use nix::poll::{PollFlags, poll, PollFd}; |
| 2 | use nix::unistd::{write, pipe}; |
| 3 | |
| 4 | #[test] |
| 5 | fn test_poll() { |
| 6 | let (r, w) = pipe().unwrap(); |
| 7 | let mut fds = [PollFd::new(r, PollFlags::POLLIN)]; |
| 8 | |
| 9 | // Poll an idle pipe. Should timeout |
| 10 | let nfds = poll(&mut fds, 100).unwrap(); |
| 11 | assert_eq!(nfds, 0); |
| 12 | assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN)); |
| 13 | |
| 14 | write(w, b".").unwrap(); |
| 15 | |
| 16 | // Poll a readable pipe. Should return an event. |
| 17 | let nfds = poll(&mut fds, 100).unwrap(); |
| 18 | assert_eq!(nfds, 1); |
| 19 | assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN)); |
| 20 | } |
| 21 | |
| 22 | // ppoll(2) is the same as poll except for how it handles timeouts and signals. |
| 23 | // Repeating the test for poll(2) should be sufficient to check that our |
| 24 | // bindings are correct. |
| 25 | #[cfg(any(target_os = "android", |
| 26 | target_os = "dragonfly", |
| 27 | target_os = "freebsd", |
| 28 | target_os = "linux"))] |
| 29 | #[test] |
| 30 | fn test_ppoll() { |
| 31 | use nix::poll::ppoll; |
| 32 | use nix::sys::signal::SigSet; |
| 33 | use nix::sys::time::{TimeSpec, TimeValLike}; |
| 34 | |
| 35 | let timeout = TimeSpec::milliseconds(1); |
| 36 | let (r, w) = pipe().unwrap(); |
| 37 | let mut fds = [PollFd::new(r, PollFlags::POLLIN)]; |
| 38 | |
| 39 | // Poll an idle pipe. Should timeout |
| 40 | let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap(); |
| 41 | assert_eq!(nfds, 0); |
| 42 | assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN)); |
| 43 | |
| 44 | write(w, b".").unwrap(); |
| 45 | |
| 46 | // Poll a readable pipe. Should return an event. |
| 47 | let nfds = ppoll(&mut fds, Some(timeout), SigSet::empty()).unwrap(); |
| 48 | assert_eq!(nfds, 1); |
| 49 | assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN)); |
| 50 | } |