diff --git a/network/src/metrics.rs b/network/src/metrics.rs index 1bc4666df1..8a0b3eb835 100644 --- a/network/src/metrics.rs +++ b/network/src/metrics.rs @@ -331,3 +331,63 @@ impl CidFrameCache { &self.cache[frame.get_int() as usize] } } + +#[cfg(test)] +mod tests { + use crate::{ + metrics::*, + types::{Frame, Pid}, + }; + + #[test] + fn register_metrics() { + let registry = Registry::new(); + let metrics = NetworkMetrics::new(&Pid::fake(1)).unwrap(); + metrics.register(®istry).unwrap(); + } + + #[test] + fn pid_cid_frame_cache() { + let pid = Pid::fake(1); + let frame1 = Frame::Raw("Foo".as_bytes().to_vec()); + let frame2 = Frame::Raw("Bar".as_bytes().to_vec()); + let metrics = NetworkMetrics::new(&pid).unwrap(); + let mut cache = PidCidFrameCache::new(metrics.frames_in_total, pid); + let v1 = cache.with_label_values(1, &frame1); + v1.inc(); + assert_eq!(v1.get(), 1); + let v2 = cache.with_label_values(1, &frame1); + v2.inc(); + assert_eq!(v2.get(), 2); + let v3 = cache.with_label_values(1, &frame2); + v3.inc(); + assert_eq!(v3.get(), 3); + let v4 = cache.with_label_values(3, &frame1); + v4.inc(); + assert_eq!(v4.get(), 1); + let v5 = cache.with_label_values(3, &Frame::Shutdown); + v5.inc(); + assert_eq!(v5.get(), 1); + } + + #[test] + fn cid_frame_cache() { + let pid = Pid::fake(1); + let frame1 = Frame::Raw("Foo".as_bytes().to_vec()); + let frame2 = Frame::Raw("Bar".as_bytes().to_vec()); + let metrics = NetworkMetrics::new(&pid).unwrap(); + let mut cache = CidFrameCache::new(metrics.frames_wire_out_total, 1); + let v1 = cache.with_label_values(&frame1); + v1.inc(); + assert_eq!(v1.get(), 1); + let v2 = cache.with_label_values(&frame1); + v2.inc(); + assert_eq!(v2.get(), 2); + let v3 = cache.with_label_values(&frame2); + v3.inc(); + assert_eq!(v3.get(), 3); + let v4 = cache.with_label_values(&Frame::Shutdown); + v4.inc(); + assert_eq!(v4.get(), 1); + } +} diff --git a/network/src/protocols.rs b/network/src/protocols.rs index dbd0f13714..3a96cd380c 100644 --- a/network/src/protocols.rs +++ b/network/src/protocols.rs @@ -52,6 +52,7 @@ pub(crate) struct UdpProtocol { data_in: RwLock>>, } +//TODO: PERFORMACE: Use BufWriter and BufReader from std::io! impl TcpProtocol { pub(crate) fn new(stream: TcpStream, metrics: Arc) -> Self { Self { stream, metrics } diff --git a/network/src/types.rs b/network/src/types.rs index d8fc7c568d..88a64ce509 100644 --- a/network/src/types.rs +++ b/network/src/types.rs @@ -310,3 +310,34 @@ fn sixlet_to_str(sixlet: u128) -> char { _ => '-', } } + +#[cfg(test)] +mod tests { + use crate::types::*; + + #[test] + fn frame_int2str() { + assert_eq!(Frame::int_to_string(3), "OpenStream"); + assert_eq!(Frame::int_to_string(7), "Raw"); + assert_eq!(Frame::int_to_string(8), ""); + } + + #[test] + fn frame_get_int() { + assert_eq!(Frame::get_int(&Frame::Raw("Foo".as_bytes().to_vec())), 7); + assert_eq!(Frame::get_int(&Frame::Shutdown), 2); + } + + #[test] + fn frame_creation() { + Pid::new(); + assert_eq!(format!("{}", Pid::fake(2)), "CAAAAA"); + } + + #[test] + fn test_sixlet_to_str() { + assert_eq!(sixlet_to_str(0), 'A'); + assert_eq!(sixlet_to_str(63), '/'); + assert_eq!(sixlet_to_str(64), '-'); + } +} diff --git a/network/tests/integration.rs b/network/tests/integration.rs index c9451ebec8..0f0d489560 100644 --- a/network/tests/integration.rs +++ b/network/tests/integration.rs @@ -1,8 +1,9 @@ use async_std::task; use task::block_on; -use veloren_network::StreamError; +use veloren_network::{NetworkError, StreamError}; mod helper; use helper::{network_participant_stream, tcp, udp}; +use std::io::ErrorKind; #[test] #[ignore] @@ -157,3 +158,27 @@ fn tcp_and_udp_2_connections() -> std::result::Result<(), Box std::result::Result<(), Box> { + let (_, _) = helper::setup(false, 0); + let network = Network::new(Pid::new(), &ThreadPoolBuilder::new().build(), None); + let udp1 = udp(); + let tcp1 = tcp(); + block_on(network.listen(udp1.clone()))?; + block_on(network.listen(tcp1.clone()))?; + std::thread::sleep(std::time::Duration::from_millis(50)); + + let network2 = Network::new(Pid::new(), &ThreadPoolBuilder::new().build(), None); + let e1 = block_on(network2.listen(udp1)); + let e2 = block_on(network2.listen(tcp1)); + match e1 { + Err(NetworkError::ListenFailed(e)) if e.kind() == ErrorKind::AddrInUse => (), + _ => assert!(false), + }; + match e2 { + Err(NetworkError::ListenFailed(e)) if e.kind() == ErrorKind::AddrInUse => (), + _ => assert!(false), + }; + Ok(()) +}