summaryrefslogtreecommitdiff
path: root/test/test_poll.rs
diff options
context:
space:
mode:
authorZhouyu Qian <qzy@qzy.io>2018-04-19 18:02:30 -0700
committerZhouyu Qian <qzy@qzy.io>2018-04-19 19:38:20 -0700
commit5e2b582662b1c0ab28460629a846e9fe693120c5 (patch)
tree334fb41dcf1b8190cf46c8fb0565deda9c4c0ec2 /test/test_poll.rs
parent305141c6dede2ebb5e7b4c5d4c1bc547f52f3d48 (diff)
downloadnix-5e2b582662b1c0ab28460629a846e9fe693120c5.zip
Implement Debug trait for PollFd
This is useful when using printf-style debugging to observe the variables of the program. Also includes a test. Fixes #885.
Diffstat (limited to 'test/test_poll.rs')
-rw-r--r--test/test_poll.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/test/test_poll.rs b/test/test_poll.rs
index c28c8fd8..a9831ed4 100644
--- a/test/test_poll.rs
+++ b/test/test_poll.rs
@@ -1,7 +1,7 @@
use nix::poll::{EventFlags, poll, PollFd};
use nix::sys::signal::SigSet;
use nix::sys::time::{TimeSpec, TimeValLike};
-use nix::unistd::{write, pipe};
+use nix::unistd::{write, pipe, close};
#[test]
fn test_poll() {
@@ -21,6 +21,24 @@ fn test_poll() {
assert!(fds[0].revents().unwrap().contains(EventFlags::POLLIN));
}
+#[test]
+fn test_poll_debug() {
+ assert_eq!(format!("{:?}", PollFd::new(0, EventFlags::empty())),
+ "PollFd { fd: 0, events: (empty), revents: (empty) }");
+ assert_eq!(format!("{:?}", PollFd::new(1, EventFlags::POLLIN)),
+ "PollFd { fd: 1, events: POLLIN, revents: (empty) }");
+
+ // Testing revents requires doing some I/O
+ let (r, w) = pipe().unwrap();
+ let mut fds = [PollFd::new(r, EventFlags::POLLIN)];
+ write(w, b" ").unwrap();
+ close(w).unwrap();
+ poll(&mut fds, -1).unwrap();
+ assert_eq!(format!("{:?}", fds[0]),
+ format!("PollFd {{ fd: {}, events: POLLIN, revents: POLLIN | POLLHUP }}", r));
+ close(r).unwrap();
+}
+
// ppoll(2) is the same as poll except for how it handles timeouts and signals.
// Repeating the test for poll(2) should be sufficient to check that our
// bindings are correct.