From 5e2b582662b1c0ab28460629a846e9fe693120c5 Mon Sep 17 00:00:00 2001 From: Zhouyu Qian Date: Thu, 19 Apr 2018 18:02:30 -0700 Subject: 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. --- test/test_poll.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'test/test_poll.rs') 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. -- cgit v1.2.3