Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
manelmontilla committed Nov 16, 2023
1 parent 23e9a6a commit f3b29a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
1 change: 0 additions & 1 deletion wruster/src/streams/cancellable_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ where
};
break;
}

}
Ok(bytes_written)
}
Expand Down
29 changes: 6 additions & 23 deletions wruster/src/streams/test.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use super::{
cancellable_stream::CancellableStream,
observable::ObservedStreamList,
test_utils::{get_free_port, TcpClient},
test_utils::{get_free_port, load_test_file, test_file_size, TcpClient},
timeout_stream::TimeoutStream,
tls::test_utils::*,
*,
};

use crate::test_utils::TestTLSClient;
use std::{
fs::{self, File},
io::{BufRead, BufReader, ErrorKind, Read, Write},
net::{Shutdown, TcpListener},
path::PathBuf,
str::FromStr,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread,
time::Duration, fs::{File, self}, path::PathBuf,
time::Duration,
};

#[test]
Expand Down Expand Up @@ -111,14 +113,12 @@ fn cancellable_stream_write_writes_data() {
let port = get_free_port();
let addr = format!("127.0.0.1:{}", port);
let listener = TcpListener::bind(addr.clone()).unwrap();
let mut server_data = load_test_file("big.png").unwrap();
let mut server_data = load_test_file("big.png").unwrap();
let handle = thread::spawn(move || {
let (stream, _) = listener.accept().unwrap();
let mut cstream = CancellableStream::new(stream).unwrap();
//cstream.set_write_timeout(Some(Duration::from_secs(30))).unwrap();
let mut data = Vec::new();
server_data.read_to_end(&mut data).unwrap();

cstream.write(&data)
});

Expand All @@ -137,24 +137,7 @@ fn cancellable_stream_write_writes_data() {
.join()
.unwrap()
.expect("expected data to be written correctly");
assert_eq!(bytes_sent,len.try_into().unwrap());
}

pub fn load_test_file(name: &str) -> Result<File, io::Error> {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("tests/assets");
file_path.push(name);
let metadata = fs::metadata(&file_path).unwrap();
let mut file = fs::File::open(&file_path).unwrap();
return Ok(file)
}

pub fn test_file_size(name: &str) -> Result<u64, io::Error> {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("tests/assets");
file_path.push(name);
let metadata = fs::metadata(&file_path).unwrap();
return Ok(metadata.len())
assert_eq!(bytes_sent, len.try_into().unwrap());
}

#[test]
Expand Down
20 changes: 20 additions & 0 deletions wruster/src/streams/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{
error::Error,
fs::{self, File},
io::{self, Read, Write},
net::{Ipv4Addr, Shutdown, SocketAddrV4, TcpListener, TcpStream},
path::PathBuf,
};

pub struct TcpClient {
Expand Down Expand Up @@ -57,3 +59,21 @@ pub fn get_free_port() -> u16 {
.unwrap()
.port()
}

#[allow(dead_code)]
pub fn load_test_file(name: &str) -> Result<File, io::Error> {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("tests/assets");
file_path.push(name);
let file = fs::File::open(&file_path).unwrap();
return Ok(file);
}

#[allow(dead_code)]
pub fn test_file_size(name: &str) -> Result<u64, io::Error> {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("tests/assets");
file_path.push(name);
let metadata = fs::metadata(&file_path).unwrap();
return Ok(metadata.len());
}

0 comments on commit f3b29a5

Please sign in to comment.