| Joel Galenson | 4727c11 | 2021-04-02 12:22:24 -0700 | [diff] [blame] | 1 | use nix::{ |
| Joel Galenson | 4727c11 | 2021-04-02 12:22:24 -0700 | [diff] [blame] | 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, |
| Joel Galenson | 981b40a | 2021-08-09 10:34:35 -0700 | [diff] [blame] | 12 | Err(Errno::EINTR) => (), |
| Joel Galenson | e7950d9 | 2021-06-21 14:41:02 -0700 | [diff] [blame] | 13 | Err(e) => panic!("{}", e) |
| Joel Galenson | 4727c11 | 2021-04-02 12:22:24 -0700 | [diff] [blame] | 14 | } |
| 15 | } |
| 16 | } |
| 17 | } |
| Andrew Walbran | 12f6140 | 2020-10-14 11:10:53 +0100 | [diff] [blame] | 18 | |
| 19 | #[test] |
| 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 |
| Joel Galenson | 4727c11 | 2021-04-02 12:22:24 -0700 | [diff] [blame] | 25 | let nfds = loop_while_eintr!(poll(&mut fds, 100)); |
| Andrew Walbran | 12f6140 | 2020-10-14 11:10:53 +0100 | [diff] [blame] | 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] |
| 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 |
| Joel Galenson | 4727c11 | 2021-04-02 12:22:24 -0700 | [diff] [blame] | 55 | let sigset = SigSet::empty(); |
| 56 | let nfds = loop_while_eintr!(ppoll(&mut fds, Some(timeout), sigset)); |
| Andrew Walbran | 12f6140 | 2020-10-14 11:10:53 +0100 | [diff] [blame] | 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 | } |