Skip to content

Commit

Permalink
Fix various failing tests
Browse files Browse the repository at this point in the history
- Make sure that waitable handles interact properly with the new
  infrastructure
- Fix failing doctests

Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Aug 11, 2023
1 parent 1951527 commit a568261
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 67 deletions.
7 changes: 4 additions & 3 deletions src/iocp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,9 @@ impl PacketUnwrapped {
// If there was a change, indicate that we need an update.
match socket.status {
SocketStatus::Polling { flags } => {
let our_flags = event_to_afd_mask(interest.readable, interest.writable, true)
| interest.extra.flags;
let our_flags =
event_to_afd_mask(interest.readable, interest.writable, true)
| interest.extra.flags;

our_flags != flags
}
Expand All @@ -799,7 +800,7 @@ impl PacketUnwrapped {
// Update if there is no ongoing wait.
handle.status.is_idle()
}
_ => true
_ => true,
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! # Examples
//!
//! ```no_run
//! use polling::{Event, Poller};
//! use polling::{Event, Events, Poller};
//! use std::net::TcpListener;
//!
//! // Create a TCP listener.
Expand All @@ -33,13 +33,13 @@
//! }
//!
//! // The event loop.
//! let mut events = Vec::new();
//! let mut events = Events::new();
//! loop {
//! // Wait for at least one I/O event.
//! events.clear();
//! poller.wait(&mut events, None)?;
//!
//! for ev in &events {
//! for ev in events.iter() {
//! if ev.key == key {
//! // Perform a non-blocking accept operation.
//! socket.accept()?;
Expand Down Expand Up @@ -603,7 +603,7 @@ impl Poller {
/// # Examples
///
/// ```
/// use polling::{Event, Poller};
/// use polling::{Event, Events, Poller};
/// use std::net::TcpListener;
/// use std::time::Duration;
///
Expand All @@ -616,7 +616,7 @@ impl Poller {
/// poller.add(&socket, Event::all(key))?;
/// }
///
/// let mut events = Vec::new();
/// let mut events = Events::new();
/// let n = poller.wait(&mut events, Some(Duration::from_secs(1)))?;
/// poller.delete(&socket)?;
/// # std::io::Result::Ok(())
Expand Down Expand Up @@ -650,14 +650,14 @@ impl Poller {
/// # Examples
///
/// ```
/// use polling::Poller;
/// use polling::{Events, Poller};
///
/// let poller = Poller::new()?;
///
/// // Notify the poller.
/// poller.notify()?;
///
/// let mut events = Vec::new();
/// let mut events = Events::new();
/// poller.wait(&mut events, None)?; // wakes up immediately
/// assert!(events.is_empty());
/// # std::io::Result::Ok(())
Expand Down Expand Up @@ -804,7 +804,7 @@ impl Events {
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len() != 0
self.len() == 0
}

/// Get the total capacity of the list.
Expand Down
22 changes: 11 additions & 11 deletions src/os/iocp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait PollerIocpExt: PollerSealed {
/// # Examples
///
/// ```rust
/// use polling::{Poller, Event};
/// use polling::{Poller, Event, Events};
/// use polling::os::iocp::{CompletionPacket, PollerIocpExt};
///
/// use std::thread;
Expand All @@ -39,7 +39,7 @@ pub trait PollerIocpExt: PollerSealed {
/// });
///
/// // Wait for the event.
/// let mut events = vec![];
/// let mut events = Events::new();
/// poller.wait(&mut events, None)?;
///
/// assert_eq!(events.len(), 1);
Expand Down Expand Up @@ -72,7 +72,7 @@ pub trait PollerIocpExt: PollerSealed {
/// # Examples
///
/// ```no_run
/// use polling::{Poller, Event, PollMode};
/// use polling::{Poller, Event, Events, PollMode};
/// use polling::os::iocp::PollerIocpExt;
///
/// use std::process::Command;
Expand All @@ -92,11 +92,11 @@ pub trait PollerIocpExt: PollerSealed {
/// }
///
/// // Wait for the child process to exit.
/// let mut events = vec![];
/// let mut events = Events::new();
/// poller.wait(&mut events, None).unwrap();
///
/// assert_eq!(events.len(), 1);
/// assert_eq!(events[0], Event::all(0));
/// assert_eq!(events.iter().next().unwrap(), Event::all(0));
/// ```
unsafe fn add_waitable(
&self,
Expand All @@ -115,7 +115,7 @@ pub trait PollerIocpExt: PollerSealed {
/// # Examples
///
/// ```no_run
/// use polling::{Poller, Event, PollMode};
/// use polling::{Poller, Event, Events, PollMode};
/// use polling::os::iocp::PollerIocpExt;
///
/// use std::process::Command;
Expand All @@ -135,11 +135,11 @@ pub trait PollerIocpExt: PollerSealed {
/// }
///
/// // Wait for the child process to exit.
/// let mut events = vec![];
/// let mut events = Events::new();
/// poller.wait(&mut events, None).unwrap();
///
/// assert_eq!(events.len(), 1);
/// assert_eq!(events[0], Event::all(0));
/// assert_eq!(events.iter().next().unwrap(), Event::all(0));
///
/// // Modify the waitable handle.
/// poller.modify_waitable(&child, Event::readable(0), PollMode::Oneshot).unwrap();
Expand All @@ -161,7 +161,7 @@ pub trait PollerIocpExt: PollerSealed {
/// # Examples
///
/// ```no_run
/// use polling::{Poller, Event, PollMode};
/// use polling::{Poller, Event, Events, PollMode};
/// use polling::os::iocp::PollerIocpExt;
///
/// use std::process::Command;
Expand All @@ -181,11 +181,11 @@ pub trait PollerIocpExt: PollerSealed {
/// }
///
/// // Wait for the child process to exit.
/// let mut events = vec![];
/// let mut events = Events::new();
/// poller.wait(&mut events, None).unwrap();
///
/// assert_eq!(events.len(), 1);
/// assert_eq!(events[0], Event::all(0));
/// assert_eq!(events.iter().next().unwrap(), Event::all(0));
///
/// // Remove the waitable handle.
/// poller.remove_waitable(&child).unwrap();
Expand Down
15 changes: 10 additions & 5 deletions src/os/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait PollerKqueueExt<F: Filter>: PollerSealed {
/// # Examples
///
/// ```no_run
/// use polling::{Poller, PollMode};
/// use polling::{Events, Poller, PollMode};
/// use polling::os::kqueue::{Filter, PollerKqueueExt, Signal};
///
/// let poller = Poller::new().unwrap();
Expand All @@ -40,7 +40,7 @@ pub trait PollerKqueueExt<F: Filter>: PollerSealed {
/// poller.add_filter(Signal(libc::SIGINT), 0, PollMode::Oneshot).unwrap();
///
/// // Wait for the signal.
/// let mut events = vec![];
/// let mut events = Events::new();
/// poller.wait(&mut events, None).unwrap();
/// # let _ = events;
/// ```
Expand All @@ -54,7 +54,7 @@ pub trait PollerKqueueExt<F: Filter>: PollerSealed {
/// # Examples
///
/// ```no_run
/// use polling::{Poller, PollMode};
/// use polling::{Events, Poller, PollMode};
/// use polling::os::kqueue::{Filter, PollerKqueueExt, Signal};
///
/// let poller = Poller::new().unwrap();
Expand All @@ -66,7 +66,7 @@ pub trait PollerKqueueExt<F: Filter>: PollerSealed {
/// poller.modify_filter(Signal(libc::SIGINT), 1, PollMode::Oneshot).unwrap();
///
/// // Wait for the signal.
/// let mut events = vec![];
/// let mut events = Events::new();
/// poller.wait(&mut events, None).unwrap();
/// # let _ = events;
/// ```
Expand Down Expand Up @@ -183,7 +183,12 @@ pub enum ProcessOps {

impl<'a> Process<'a> {
/// Monitor a child process.
pub fn new(child: &'a Child, ops: ProcessOps) -> Self {
///
/// # Safety
///
/// Once registered into the `Poller`, the `Child` object must outlive this filter's
/// registration into the poller.
pub unsafe fn new(child: &'a Child, ops: ProcessOps) -> Self {
Self { child, ops }
}
}
Expand Down
8 changes: 6 additions & 2 deletions tests/concurrent_modification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ fn concurrent_add() -> io::Result<()> {
poller.delete(&reader)?;
result?;

assert_eq!(events.iter().collect::<Vec<_>>(), [Event::readable(0)]);
assert_eq!(events.len(), 1);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

Ok(())
}
Expand Down Expand Up @@ -63,7 +65,9 @@ fn concurrent_modify() -> io::Result<()> {
.into_iter()
.collect::<io::Result<()>>()?;

assert_eq!(events.iter().collect::<Vec<_>>(), [Event::readable(0)]);
assert_eq!(events.len(), 1);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ fn basic_io() {
.unwrap(),
1
);
assert_eq!(events.iter().collect::<Vec<_>>(), &[Event::readable(1)]);

assert_eq!(events.len(), 1);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

poller.delete(&read).unwrap();
}
Expand Down
65 changes: 33 additions & 32 deletions tests/other_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@ fn level_triggered() {
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();

assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);
assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap().key, reader_token);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

// If we read some of the data, the notification should still be available.
reader.read_exact(&mut [0; 3]).unwrap();
events.clear();
poller
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();
assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);

assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap().key, reader_token);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

// If we read the rest of the data, the notification should be gone.
reader.read_exact(&mut [0; 2]).unwrap();
Expand All @@ -77,10 +78,10 @@ fn level_triggered() {
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();

assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);
assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap().key, reader_token);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

// After reading, the notification should vanish.
reader.read(&mut [0; 5]).unwrap();
Expand Down Expand Up @@ -137,10 +138,10 @@ fn edge_triggered() {
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();

assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);
assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap().key, reader_token);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

// If we read some of the data, the notification should not still be available.
reader.read_exact(&mut [0; 3]).unwrap();
Expand All @@ -156,10 +157,11 @@ fn edge_triggered() {
poller
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();
assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);

assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap().key, reader_token);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

// After modifying the stream and sending more data, it should be oneshot.
poller
Expand All @@ -172,10 +174,10 @@ fn edge_triggered() {
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();

assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);
assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap().key, reader_token);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);
}

#[test]
Expand Down Expand Up @@ -229,10 +231,9 @@ fn edge_oneshot_triggered() {
.wait(&mut events, Some(Duration::from_secs(10)))
.unwrap();

assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);
assert_eq!(events.len(), 1);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);

// If we read some of the data, the notification should not still be available.
reader.read_exact(&mut [0; 3]).unwrap();
Expand All @@ -254,10 +255,10 @@ fn edge_oneshot_triggered() {
poller
.wait(&mut events, Some(Duration::from_secs(0)))
.unwrap();
assert_eq!(
events.iter().collect::<Vec<_>>(),
[Event::readable(reader_token)]
);

assert_eq!(events.len(), 1);
assert!(events.iter().next().unwrap().readable);
assert!(!events.iter().next().unwrap().writable);
}

fn tcp_pair() -> io::Result<(TcpStream, TcpStream)> {
Expand Down
5 changes: 4 additions & 1 deletion tests/windows_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ fn post_multithread() {
.unwrap();

assert_eq!(events.len(), 1);
assert_eq!(events.iter().next(), Some(Event::writable(i)));
assert_eq!(events.iter().next().unwrap().key, i);
assert!(!events.iter().next().unwrap().readable);
assert!(events.iter().next().unwrap().writable);
events.clear();
}

poller
Expand Down
Loading

0 comments on commit a568261

Please sign in to comment.