Skip to content

Commit

Permalink
fix doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
irvingoujAtDevolution committed Feb 7, 2024
1 parent 80578fd commit 82a4d5d
Showing 1 changed file with 85 additions and 49 deletions.
134 changes: 85 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,55 +343,91 @@ impl Event {
/// # Examples
///
/// ```
/// #[cfg(target_os = "linux")]
/// fn main() -> io::Result<()> {
/// use std::{io::Write, time::Duration};
///
/// std::thread::spawn(|| {
/// let listener = net::TcpListener::bind("0.0.0.0:8080").unwrap();
/// println!("Listening on {}", listener.local_addr().unwrap());
/// for stream in listener.incoming() {
/// let mut stream = stream.unwrap();
/// stream.write(b"Hello, world!\n").unwrap();
/// }
/// });
/// std::thread::sleep(Duration::from_millis(100));
/// let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?;
/// let poller = polling::Poller::new()?;
/// unsafe {
/// poller.add(&socket, Event::new(0, true, true))?;
/// }
///
/// socket.set_nonblocking(true)?;
///
/// let mut events = polling::Events::new();
/// poller.wait(&mut events, Some(Duration::from_secs(3)))?;
///
/// let event = events.iter().next().expect("no event");
///
/// assert!(event.is_interrupt());
///
/// let addr = net::SocketAddr::new("127.0.0.1".parse().unwrap(), 8080);
/// let err = socket.connect(&addr.into()).unwrap_err();
///
/// // EINPROGRESS
/// assert_eq!(115, err.raw_os_error().expect("No OS error"));
///
/// poller.modify(&socket, Event::writable(0)).expect("modify failed");
/// events.clear();
/// poller.wait(&mut events, Some(Duration::from_secs(3)))?;
///
/// let event = events.iter().next().expect("no event");
///
/// assert!(event.writable);
/// assert!(!event.is_interrupt());
/// assert!(!event.is_err().unwrap());
///
/// println!("event: {:?}", event);
/// println!("socket is now writable");
///
/// Ok(())
/// }
///use std::io;
///
///#[cfg(target_os = "linux")]
///fn main() -> io::Result<()> {
/// use std::net;
/// use std::{io::Write, time::Duration};
///
/// use polling::Event;
/// use socket2::Type;
///
/// std::thread::spawn(|| {
/// let listener = net::TcpListener::bind("0.0.0.0:8080").unwrap();
/// println!("Listening on {}", listener.local_addr().unwrap());
/// for stream in listener.incoming() {
/// let mut stream = stream.unwrap();
/// stream.write_all(b"Hello, world!\n").unwrap();
/// }
/// });
/// std::thread::sleep(Duration::from_millis(100));
/// let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?;
/// let poller = polling::Poller::new()?;
/// unsafe {
/// poller.add(&socket, Event::new(0, true, true))?;
/// }
///
/// socket.set_nonblocking(true)?;
///
/// let mut events = polling::Events::new();
/// poller.wait(&mut events, Some(Duration::from_secs(3)))?;
///
/// let event = events.iter().next().expect("no event");
///
/// assert!(event.is_interrupt());
///
/// let addr = net::SocketAddr::new("127.0.0.1".parse().unwrap(), 8080);
/// let err = socket.connect(&addr.into()).unwrap_err();
///
/// // EINPROGRESS
/// assert_eq!(115, err.raw_os_error().expect("No OS error"));
///
/// poller
/// .modify(&socket, Event::writable(0))
/// .expect("modify failed");
/// events.clear();
/// poller.wait(&mut events, Some(Duration::from_secs(3)))?;
///
/// let event = events.iter().next().expect("no event");
///
/// assert!(event.writable);
/// assert!(!event.is_interrupt());
/// assert!(!event.is_err().unwrap());
///
/// println!("event: {:?}", event);
/// println!("socket is now writable");
/// // ========================================================================
/// // the below is example of a bad socket
/// println!("testing bad socket");
/// let bad_socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?;
/// let addr = net::SocketAddr::new("127.0.0.1".parse().unwrap(), 12345);
/// bad_socket.set_nonblocking(true)?;
///
/// unsafe {
/// poller.add(&bad_socket, Event::writable(0))?;
/// }
///
/// events.clear();
/// poller.wait(&mut events, Some(Duration::from_secs(3)))?;
///
/// let err = bad_socket.connect(&addr.into()).unwrap_err();
/// assert_eq!(115, err.raw_os_error().expect("No OS error"));
///
/// poller
/// .modify(&bad_socket, Event::writable(0))
/// .expect("modify failed");
///
/// events.clear();
/// poller.wait(&mut events, Some(Duration::from_secs(3)))?;
///
/// let event = events.iter().next().expect("no event");
///
/// assert!(event.is_err().unwrap());
/// println!("bad socket is now in error state");
///
/// Ok(())
///}
/// ```
/// Returns `Some(true)` if the connection has failed, `Some(false)` if there is an error,
/// or `None` if the platform does not support detecting this condition.
Expand Down

0 comments on commit 82a4d5d

Please sign in to comment.