Move tests into module

This commit is contained in:
Fredrik 2017-09-30 15:55:57 +02:00
parent c84a14ea49
commit 80ffb9e7c4

View File

@ -84,6 +84,17 @@ fn multiaddr_to_socketaddr(addr: &Multiaddr) -> Result<SocketAddr, &Multiaddr>
} }
} }
#[cfg(test)]
mod tests {
use super::{Tcp, multiaddr_to_socketaddr};
use std;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use tokio_io;
use futures::Future;
use futures::stream::Stream;
use multiaddr::Multiaddr;
use transport::Transport;
#[test] #[test]
fn multiaddr_to_tcp_conversion() { fn multiaddr_to_tcp_conversion() {
use std::net::{Ipv6Addr}; use std::net::{Ipv6Addr};
@ -117,26 +128,23 @@ fn communicating_between_dialer_and_listener() {
let mut tcp = Tcp::new().unwrap(); let mut tcp = Tcp::new().unwrap();
let handle = tcp.event_loop.handle(); let handle = tcp.event_loop.handle();
let listener = tcp.listen_on(addr).unwrap().for_each(|sock| { let listener = tcp.listen_on(addr).unwrap().for_each(|sock| {
println!("Listening");
// Define what to do with the socket that just connected to us // Define what to do with the socket that just connected to us
// Which in this case is read 3 bytes // Which in this case is read 3 bytes
let handle_conn = tokio_io::io::read_exact(sock, [0; 3]).map(|(_, buf)| { let handle_conn = tokio_io::io::read_exact(sock, [0; 3]).map(|(_, buf)| {
println!("Actually read {:?}", buf);
assert_eq!(buf, [1,2,3]) assert_eq!(buf, [1,2,3])
}).map_err(|err| { }).map_err(|err| {
panic!("IO error {:?}", err) panic!("IO error {:?}", err)
}); });
println!("handling");
// Spawn the future as a concurrent task // Spawn the future as a concurrent task
handle.spawn(handle_conn); handle.spawn(handle_conn);
Ok(()) Ok(())
}); });
println!("starting loop");
tcp.event_loop.run(listener).unwrap(); tcp.event_loop.run(listener).unwrap();
}); });
std::thread::sleep(std::time::Duration::from_millis(100));
let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap(); let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap();
let mut tcp = Tcp::new().unwrap(); let mut tcp = Tcp::new().unwrap();
// Obtain a future socket through dialing // Obtain a future socket through dialing
@ -153,5 +161,6 @@ fn communicating_between_dialer_and_listener() {
}); });
// Execute the future in our event loop // Execute the future in our event loop
tcp.event_loop.run(action).unwrap(); tcp.event_loop.run(action).unwrap();
std::thread::sleep(std::time::Duration::from_millis(1000)); std::thread::sleep(std::time::Duration::from_millis(100));
}
} }