2019-12-20 13:56:01 +00:00
|
|
|
#![feature(trait_alias)]
|
|
|
|
mod api;
|
2020-01-13 16:53:28 +00:00
|
|
|
mod internal;
|
2019-12-20 13:56:01 +00:00
|
|
|
mod message;
|
2020-02-04 15:42:04 +00:00
|
|
|
mod worker;
|
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-01-13 16:53:28 +00:00
|
|
|
use std::{net::SocketAddr, sync::Arc};
|
2020-02-04 15:42:04 +00:00
|
|
|
use tracing::*;
|
2020-01-13 16:53:28 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
use uvth::ThreadPoolBuilder;
|
2019-12-20 13:56:01 +00:00
|
|
|
|
|
|
|
struct N {
|
2020-02-04 15:42:04 +00:00
|
|
|
_id: u8,
|
2019-12-20 13:56:01 +00:00
|
|
|
}
|
2020-01-13 16:53:28 +00:00
|
|
|
|
2019-12-20 13:56:01 +00:00
|
|
|
impl Events for N {
|
2020-01-13 16:53:28 +00:00
|
|
|
fn on_remote_connection_open(_net: &Network<N>, _con: &Connection) {}
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
fn on_remote_connection_close(_net: &Network<N>, _con: &Connection) {}
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
fn on_remote_stream_open(_net: &Network<N>, _st: &Stream) {}
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
fn on_remote_stream_close(_net: &Network<N>, _st: &Stream) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_tracing() {
|
|
|
|
use tracing::Level;
|
|
|
|
|
|
|
|
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)
|
|
|
|
//.with_env_filter("veloren_network::api=info,my_crate::my_mod=debug,[my_span]=trace")
|
|
|
|
// sets this to be the default, global subscriber for this application.
|
|
|
|
.init();
|
2019-12-20 13:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
|
|
|
assert_eq!(2 + 2, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn client_server() {
|
2020-01-13 16:53:28 +00:00
|
|
|
let thread_pool = Arc::new(
|
|
|
|
ThreadPoolBuilder::new()
|
|
|
|
.name("veloren-network-test".into())
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
test_tracing();
|
|
|
|
let n1 = Network::<N>::new(Uuid::new_v4(), thread_pool.clone());
|
|
|
|
let n2 = Network::<N>::new(Uuid::new_v4(), thread_pool.clone());
|
2020-02-04 15:42:04 +00:00
|
|
|
let a1 = Address::Tcp(SocketAddr::from(([127, 0, 0, 1], 52000)));
|
|
|
|
let a2 = Address::Tcp(SocketAddr::from(([127, 0, 0, 1], 52001)));
|
2020-01-13 16:53:28 +00:00
|
|
|
n1.listen(&a1); //await
|
|
|
|
n2.listen(&a2); // only requiered here, but doesnt hurt on n1
|
2020-01-22 16:44:32 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_millis(20));
|
2019-12-20 13:56:01 +00:00
|
|
|
|
|
|
|
let p1 = n1.connect(&a2); //await
|
|
|
|
//n2.OnRemoteConnectionOpen triggered
|
2020-01-22 16:44:32 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_millis(20));
|
2019-12-20 13:56:01 +00:00
|
|
|
|
|
|
|
let s1 = n1.open(p1, 16, Promise::InOrder | Promise::NoCorrupt);
|
2020-01-22 16:44:32 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_millis(20));
|
2019-12-20 13:56:01 +00:00
|
|
|
//n2.OnRemoteStreamOpen triggered
|
|
|
|
|
2020-02-04 15:42:04 +00:00
|
|
|
n1.send("Hello World", &s1);
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(20));
|
2019-12-20 13:56:01 +00:00
|
|
|
// receive on n2 now
|
|
|
|
|
2020-02-04 15:42:04 +00:00
|
|
|
let s: Option<String> = n2.recv(&s1);
|
|
|
|
for _ in 1..4 {
|
|
|
|
error!("{:?}", s);
|
|
|
|
}
|
|
|
|
assert_eq!(s, Some("Hello World".to_string()));
|
|
|
|
|
2019-12-20 13:56:01 +00:00
|
|
|
n1.close(s1);
|
|
|
|
//n2.OnRemoteStreamClose triggered
|
2020-01-13 16:53:28 +00:00
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(20000));
|
2019-12-20 13:56:01 +00:00
|
|
|
}
|
|
|
|
}
|