2019-12-20 13:56:01 +00:00
|
|
|
#![feature(trait_alias)]
|
|
|
|
mod api;
|
2020-02-21 13:08:34 +00:00
|
|
|
mod channel;
|
|
|
|
mod controller;
|
2019-12-20 13:56:01 +00:00
|
|
|
mod message;
|
2020-02-21 13:08:34 +00:00
|
|
|
mod metrics;
|
|
|
|
mod mpsc;
|
|
|
|
mod tcp;
|
|
|
|
mod types;
|
|
|
|
mod udp;
|
2020-02-04 15:42:04 +00:00
|
|
|
mod worker;
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-02-25 18:30:50 +00:00
|
|
|
pub use api::{
|
|
|
|
Address, Network, NetworkError, Participant, ParticipantError, Promise, Stream, StreamError,
|
|
|
|
};
|
|
|
|
|
2019-12-20 13:56:01 +00:00
|
|
|
#[cfg(test)]
|
2020-01-13 16:53:28 +00:00
|
|
|
pub mod tests {
|
2019-12-20 13:56:01 +00:00
|
|
|
use crate::api::*;
|
2020-02-10 17:25:47 +00:00
|
|
|
use futures::executor::block_on;
|
2020-02-21 15:10:55 +00:00
|
|
|
use std::{net::SocketAddr, sync::Arc, thread, time::Duration};
|
2020-02-04 15:42:04 +00:00
|
|
|
use tracing::*;
|
2020-02-21 13:08:34 +00:00
|
|
|
use tracing_subscriber::EnvFilter;
|
2020-01-13 16:53:28 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
use uvth::ThreadPoolBuilder;
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
pub fn test_tracing() {
|
2020-02-21 13:08:34 +00:00
|
|
|
let filter = EnvFilter::from_default_env()
|
|
|
|
//.add_directive("[worker]=trace".parse().unwrap())
|
2020-02-21 15:10:55 +00:00
|
|
|
.add_directive("trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::tests=trace".parse().unwrap())
|
2020-02-21 13:08:34 +00:00
|
|
|
.add_directive("veloren_network::worker=debug".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::controller=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::channel=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::message=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::metrics=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::types=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::mpsc=debug".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::udp=debug".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::tcp=debug".parse().unwrap());
|
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
tracing_subscriber::FmtSubscriber::builder()
|
|
|
|
// all spans/events with a level higher than TRACE (e.g, info, warn, etc.)
|
|
|
|
// will be written to stdout.
|
|
|
|
.with_max_level(Level::TRACE)
|
2020-02-21 13:08:34 +00:00
|
|
|
.with_env_filter(filter)
|
2020-01-13 16:53:28 +00:00
|
|
|
// sets this to be the default, global subscriber for this application.
|
|
|
|
.init();
|
2019-12-20 13:56:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 15:10:55 +00:00
|
|
|
#[test]
|
|
|
|
fn aaa() { test_tracing(); }
|
2020-02-10 17:25:47 +00:00
|
|
|
|
2019-12-20 13:56:01 +00:00
|
|
|
#[test]
|
2020-02-21 15:10:55 +00:00
|
|
|
fn client_server() {
|
2020-01-13 16:53:28 +00:00
|
|
|
let thread_pool = Arc::new(
|
|
|
|
ThreadPoolBuilder::new()
|
|
|
|
.name("veloren-network-test".into())
|
|
|
|
.build(),
|
|
|
|
);
|
2020-02-21 15:10:55 +00:00
|
|
|
thread::sleep(Duration::from_millis(200));
|
|
|
|
let n1 = Network::new(Uuid::new_v4(), thread_pool.clone());
|
|
|
|
let n2 = Network::new(Uuid::new_v4(), thread_pool.clone());
|
|
|
|
let a1 = Address::Tcp(SocketAddr::from(([127, 0, 0, 1], 52000)));
|
|
|
|
let a2 = Address::Tcp(SocketAddr::from(([127, 0, 0, 1], 52001)));
|
2020-02-10 17:25:47 +00:00
|
|
|
block_on(n1.listen(&a1)).unwrap(); //await
|
|
|
|
block_on(n2.listen(&a2)).unwrap(); // only requiered here, but doesnt hurt on n1
|
2020-02-21 15:10:55 +00:00
|
|
|
thread::sleep(Duration::from_millis(3)); //TODO: listeing still doesnt block correctly!
|
|
|
|
|
|
|
|
let p1 = block_on(n1.connect(&a2)).unwrap(); //await
|
|
|
|
let s1 = block_on(p1.open(16, Promise::InOrder | Promise::NoCorrupt)).unwrap();
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-02-25 18:30:50 +00:00
|
|
|
assert!(s1.send("Hello World").is_ok());
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-02-21 15:10:55 +00:00
|
|
|
let p1_n2 = block_on(n2.connected()).unwrap(); //remote representation of p1
|
2020-03-04 15:52:30 +00:00
|
|
|
let mut s1_n2 = block_on(p1_n2.opened()).unwrap(); //remote representation of s1
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-03-04 15:52:30 +00:00
|
|
|
let s: Result<String, _> = block_on(s1_n2.recv());
|
2020-02-21 15:10:55 +00:00
|
|
|
assert_eq!(s, Ok("Hello World".to_string()));
|
2020-02-10 17:25:47 +00:00
|
|
|
|
2020-02-25 18:30:50 +00:00
|
|
|
assert!(p1.close(s1).is_ok());
|
2020-02-21 15:10:55 +00:00
|
|
|
}
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-02-21 15:10:55 +00:00
|
|
|
#[test]
|
|
|
|
fn client_server_stream() {
|
|
|
|
let thread_pool = Arc::new(
|
|
|
|
ThreadPoolBuilder::new()
|
|
|
|
.name("veloren-network-test".into())
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
thread::sleep(Duration::from_millis(400));
|
|
|
|
let n1 = Network::new(Uuid::new_v4(), thread_pool.clone());
|
|
|
|
let n2 = Network::new(Uuid::new_v4(), thread_pool.clone());
|
|
|
|
let a1 = Address::Tcp(SocketAddr::from(([127, 0, 0, 1], 52010)));
|
|
|
|
let a2 = Address::Tcp(SocketAddr::from(([127, 0, 0, 1], 52011)));
|
2020-02-04 15:42:04 +00:00
|
|
|
|
2020-02-21 15:10:55 +00:00
|
|
|
block_on(n1.listen(&a1)).unwrap(); //await
|
|
|
|
block_on(n2.listen(&a2)).unwrap(); // only requiered here, but doesnt hurt on n1
|
|
|
|
thread::sleep(Duration::from_millis(3)); //TODO: listeing still doesnt block correctly!
|
|
|
|
|
|
|
|
let p1 = block_on(n1.connect(&a2)).unwrap(); //await
|
|
|
|
|
|
|
|
let s1 = block_on(p1.open(16, Promise::InOrder | Promise::NoCorrupt)).unwrap();
|
|
|
|
let s2 = block_on(p1.open(16, Promise::InOrder | Promise::NoCorrupt)).unwrap();
|
|
|
|
let s3 = block_on(p1.open(16, Promise::InOrder | Promise::NoCorrupt)).unwrap();
|
|
|
|
let s4 = block_on(p1.open(16, Promise::InOrder | Promise::NoCorrupt)).unwrap();
|
|
|
|
let s5 = block_on(p1.open(16, Promise::InOrder | Promise::NoCorrupt)).unwrap();
|
|
|
|
|
2020-02-25 18:30:50 +00:00
|
|
|
assert!(s3.send("Hello World3").is_ok());
|
|
|
|
assert!(s1.send("Hello World1").is_ok());
|
|
|
|
assert!(s5.send("Hello World5").is_ok());
|
|
|
|
assert!(s2.send("Hello World2").is_ok());
|
|
|
|
assert!(s4.send("Hello World4").is_ok());
|
2020-02-21 15:10:55 +00:00
|
|
|
|
|
|
|
let p1_n2 = block_on(n2.connected()).unwrap(); //remote representation of p1
|
2020-03-04 15:52:30 +00:00
|
|
|
let mut s1_n2 = block_on(p1_n2.opened()).unwrap(); //remote representation of s1
|
|
|
|
let mut s2_n2 = block_on(p1_n2.opened()).unwrap(); //remote representation of s2
|
|
|
|
let mut s3_n2 = block_on(p1_n2.opened()).unwrap(); //remote representation of s3
|
|
|
|
let mut s4_n2 = block_on(p1_n2.opened()).unwrap(); //remote representation of s4
|
|
|
|
let mut s5_n2 = block_on(p1_n2.opened()).unwrap(); //remote representation of s5
|
2020-02-21 15:10:55 +00:00
|
|
|
|
|
|
|
info!("all streams opened");
|
|
|
|
|
2020-03-04 15:52:30 +00:00
|
|
|
let s: Result<String, _> = block_on(s3_n2.recv());
|
2020-02-21 15:10:55 +00:00
|
|
|
assert_eq!(s, Ok("Hello World3".to_string()));
|
2020-03-04 15:52:30 +00:00
|
|
|
let s: Result<String, _> = block_on(s1_n2.recv());
|
2020-02-21 15:10:55 +00:00
|
|
|
assert_eq!(s, Ok("Hello World1".to_string()));
|
2020-03-04 15:52:30 +00:00
|
|
|
let s: Result<String, _> = block_on(s2_n2.recv());
|
2020-02-21 15:10:55 +00:00
|
|
|
assert_eq!(s, Ok("Hello World2".to_string()));
|
2020-03-04 15:52:30 +00:00
|
|
|
let s: Result<String, _> = block_on(s5_n2.recv());
|
2020-02-21 15:10:55 +00:00
|
|
|
assert_eq!(s, Ok("Hello World5".to_string()));
|
2020-03-04 15:52:30 +00:00
|
|
|
let s: Result<String, _> = block_on(s4_n2.recv());
|
2020-02-21 15:10:55 +00:00
|
|
|
assert_eq!(s, Ok("Hello World4".to_string()));
|
|
|
|
|
2020-02-25 18:30:50 +00:00
|
|
|
assert!(p1.close(s1).is_ok());
|
2019-12-20 13:56:01 +00:00
|
|
|
}
|
|
|
|
}
|