Skip to content

Commit 1b9d205

Browse files
committed
Merge #780
780: Add a test for ppoll r=Susurrus a=asomers
2 parents 00df3a6 + 5810b94 commit 1b9d205

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/test_poll.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
use nix::poll::*;
2+
use nix::sys::signal::SigSet;
3+
use nix::sys::time::{TimeSpec, TimeValLike};
24
use nix::unistd::{write, pipe};
35

46
#[test]
57
fn test_poll() {
68
let (r, w) = pipe().unwrap();
79
let mut fds = [PollFd::new(r, POLLIN)];
810

11+
// Poll an idle pipe. Should timeout
912
let nfds = poll(&mut fds, 100).unwrap();
1013
assert_eq!(nfds, 0);
1114
assert!(!fds[0].revents().unwrap().contains(POLLIN));
1215

1316
write(w, b".").unwrap();
1417

18+
// Poll a readable pipe. Should return an event.
1519
let nfds = poll(&mut fds, 100).unwrap();
1620
assert_eq!(nfds, 1);
1721
assert!(fds[0].revents().unwrap().contains(POLLIN));
1822
}
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

Comments
 (0)