Add a simple UDP test

This commit is contained in:
Alex Crichton
2016-08-02 23:56:01 -07:00
parent c458e23940
commit 1d7098eece
3 changed files with 75 additions and 6 deletions
+13
View File
@@ -1,3 +1,4 @@
use std::fmt;
use std::io::{self, ErrorKind, Read, Write};
use std::mem;
use std::net::{self, SocketAddr, Shutdown};
@@ -127,6 +128,12 @@ impl Iterator for NonblockingIter {
}
}
impl fmt::Debug for TcpListener {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.listener.io().fmt(f)
}
}
impl Stream for TcpListener {
type Item = Ready;
type Error = io::Error;
@@ -335,6 +342,12 @@ impl<'a> Write for &'a TcpStream {
}
}
impl fmt::Debug for TcpStream {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.source.io().fmt(f)
}
}
impl Stream for TcpStream {
type Item = Ready;
type Error = io::Error;
+19 -6
View File
@@ -1,6 +1,7 @@
use std::io;
use std::net::{self, SocketAddr, Ipv4Addr, Ipv6Addr};
use std::sync::Arc;
use std::fmt;
use futures::stream::Stream;
use futures::{Future, failed, Task, Poll};
@@ -71,16 +72,22 @@ impl UdpSocket {
///
/// Address type can be any implementor of `ToSocketAddrs` trait. See its
/// documentation for concrete examples.
pub fn send_to(&self, buf: &[u8], target: &SocketAddr)
-> io::Result<Option<usize>> {
self.source.io().send_to(buf, target)
pub fn send_to(&self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> {
match self.source.io().send_to(buf, target) {
Ok(Some(n)) => Ok(n),
Ok(None) => Err(io::Error::new(io::ErrorKind::WouldBlock, "would block")),
Err(e) => Err(e),
}
}
/// Receives data from the socket. On success, returns the number of bytes
/// read and the address from whence the data came.
pub fn recv_from(&self, buf: &mut [u8])
-> io::Result<Option<(usize, SocketAddr)>> {
self.source.io().recv_from(buf)
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
match self.source.io().recv_from(buf) {
Ok(Some(n)) => Ok(n),
Ok(None) => Err(io::Error::new(io::ErrorKind::WouldBlock, "would block")),
Err(e) => Err(e),
}
}
/// Gets the value of the `SO_BROADCAST` option for this socket.
@@ -224,6 +231,12 @@ impl UdpSocket {
}
}
impl fmt::Debug for UdpSocket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.source.io().fmt(f)
}
}
impl Stream for UdpSocket {
type Item = Ready;
type Error = io::Error;
+43
View File
@@ -0,0 +1,43 @@
extern crate futures;
extern crate futures_mio;
use futures::Future;
use futures::stream::Stream;
macro_rules! t {
($e:expr) => (match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
})
}
#[test]
fn send_messages() {
let mut l = t!(futures_mio::Loop::new());
let a = l.handle().udp_bind(&"127.0.0.1:0".parse().unwrap());
let b = l.handle().udp_bind(&"127.0.0.1:0".parse().unwrap());
let (a, b) = t!(l.run(a.join(b)));
let a_addr = t!(a.local_addr());
let b_addr = t!(b.local_addr());
let ((ar, a), (br, b)) = t!(l.run(a.into_future().join(b.into_future())));
let ar = ar.unwrap();
let br = br.unwrap();
assert!(ar.is_write());
assert!(!ar.is_read());
assert!(br.is_write());
assert!(!br.is_read());
assert_eq!(t!(a.send_to(b"1234", &b_addr)), 4);
let (br, b) = t!(l.run(b.into_future()));
let br = br.unwrap();
assert!(br.is_read());
let mut buf = [0; 32];
let (size, addr) = t!(b.recv_from(&mut buf));
assert_eq!(size, 4);
assert_eq!(&buf[..4], b"1234");
assert_eq!(addr, a_addr);
}