Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jul 9, 2016
1 parent 51af289 commit 83be405
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions tests/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate coio;
extern crate env_logger;

use std::io::{Read, Write};
use std::time::Duration;
Expand All @@ -9,13 +10,18 @@ use coio::sleep;

#[test]
fn test_tcp_echo() {
env_logger::init().unwrap();

Scheduler::new()
.run(move || {
let acceptor = TcpListener::bind("127.0.0.1:6789").unwrap();

// Listener
let listen_fut = Scheduler::spawn(move || {
let (mut stream, _) = acceptor.accept().unwrap();
println!("LISTEN: Started");

let (mut stream, addr) = acceptor.accept().unwrap();
println!("LISTEN: Accepted {:?}", addr);

let mut buf = [0u8; 1024];
while let Ok(len) = stream.read(&mut buf) {
Expand All @@ -24,21 +30,31 @@ fn test_tcp_echo() {
break;
}

println!("LISTEN: Going to sleep");
sleep(Duration::from_secs(1));
break;
}

println!("LISTEN: Finished");
});

let sender_fut = Scheduler::spawn(move || {
println!("SEND: Started");

let mut stream = TcpStream::connect("127.0.0.1:6789").unwrap();
stream.write_all(b"abcdefg")
.and_then(|_| stream.flush())
.unwrap();
.and_then(|_| stream.flush())
.unwrap();

println!("SEND: Written");

let _ = stream.set_read_timeout(Some(Duration::from_millis(100)));

println!("SEND: Waiting to read");
let mut buf = [0u8; 1024];
assert!(stream.read(&mut buf).is_err());

println!("SEND: Finished");
});

listen_fut.join().unwrap();
Expand Down

0 comments on commit 83be405

Please sign in to comment.