From f3b29a5ae50d93fdbc2037975976275b6ae4329d Mon Sep 17 00:00:00 2001 From: Manel Montilla Date: Thu, 16 Nov 2023 17:46:12 +0100 Subject: [PATCH] WIP --- wruster/src/streams/cancellable_stream.rs | 1 - wruster/src/streams/test.rs | 29 +++++------------------ wruster/src/streams/test_utils.rs | 20 ++++++++++++++++ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/wruster/src/streams/cancellable_stream.rs b/wruster/src/streams/cancellable_stream.rs index 14d0342..7f311fc 100644 --- a/wruster/src/streams/cancellable_stream.rs +++ b/wruster/src/streams/cancellable_stream.rs @@ -153,7 +153,6 @@ where }; break; } - } Ok(bytes_written) } diff --git a/wruster/src/streams/test.rs b/wruster/src/streams/test.rs index c36d1c2..efb0a9c 100644 --- a/wruster/src/streams/test.rs +++ b/wruster/src/streams/test.rs @@ -1,7 +1,7 @@ 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::*, *, @@ -9,15 +9,17 @@ use super::{ 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] @@ -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) }); @@ -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 { - 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 { - 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] diff --git a/wruster/src/streams/test_utils.rs b/wruster/src/streams/test_utils.rs index f408350..355f3c0 100644 --- a/wruster/src/streams/test_utils.rs +++ b/wruster/src/streams/test_utils.rs @@ -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 { @@ -57,3 +59,21 @@ pub fn get_free_port() -> u16 { .unwrap() .port() } + +#[allow(dead_code)] +pub fn load_test_file(name: &str) -> Result { + 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 { + 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()); +}