mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
add new tests and increase coverage
This commit is contained in:
@ -331,3 +331,63 @@ impl CidFrameCache {
|
|||||||
&self.cache[frame.get_int() as usize]
|
&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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -52,6 +52,7 @@ pub(crate) struct UdpProtocol {
|
|||||||
data_in: RwLock<mpsc::UnboundedReceiver<Vec<u8>>>,
|
data_in: RwLock<mpsc::UnboundedReceiver<Vec<u8>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: PERFORMACE: Use BufWriter and BufReader from std::io!
|
||||||
impl TcpProtocol {
|
impl TcpProtocol {
|
||||||
pub(crate) fn new(stream: TcpStream, metrics: Arc<NetworkMetrics>) -> Self {
|
pub(crate) fn new(stream: TcpStream, metrics: Arc<NetworkMetrics>) -> Self {
|
||||||
Self { stream, metrics }
|
Self { stream, metrics }
|
||||||
|
@ -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), '-');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
use async_std::task;
|
use async_std::task;
|
||||||
use task::block_on;
|
use task::block_on;
|
||||||
use veloren_network::StreamError;
|
use veloren_network::{NetworkError, StreamError};
|
||||||
mod helper;
|
mod helper;
|
||||||
use helper::{network_participant_stream, tcp, udp};
|
use helper::{network_participant_stream, tcp, udp};
|
||||||
|
use std::io::ErrorKind;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
@ -157,3 +158,27 @@ fn tcp_and_udp_2_connections() -> std::result::Result<(), Box<dyn std::error::Er
|
|||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn failed_listen_on_used_ports() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user