blob: 0395512ba72ef383b6f20df7d9fd6e40cf32a4df [file] [log] [blame]
Joel Galenson4727c112021-04-02 12:22:24 -07001use nix::{
Joel Galenson4727c112021-04-02 12:22:24 -07002 errno::Errno,
3 poll::{PollFlags, poll, PollFd},
4 unistd::{write, pipe}
5};
6
7macro_rules! loop_while_eintr {
8 ($poll_expr: expr) => {
9 loop {
10 match $poll_expr {
11 Ok(nfds) => break nfds,
Joel Galenson981b40a2021-08-09 10:34:35 -070012 Err(Errno::EINTR) => (),
Joel Galensone7950d92021-06-21 14:41:02 -070013 Err(e) => panic!("{}", e)
Joel Galenson4727c112021-04-02 12:22:24 -070014 }
15 }
16 }
17}
Andrew Walbran12f61402020-10-14 11:10:53 +010018
19#[test]
20fn 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 Galenson4727c112021-04-02 12:22:24 -070025 let nfds = loop_while_eintr!(poll(&mut fds, 100));
Andrew Walbran12f61402020-10-14 11:10:53 +010026 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]
45fn 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 Galenson4727c112021-04-02 12:22:24 -070055 let sigset = SigSet::empty();
56 let nfds = loop_while_eintr!(ppoll(&mut fds, Some(timeout), sigset));
Andrew Walbran12f61402020-10-14 11:10:53 +010057 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}