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