Files
@ 3f6d45f84e76
Branch filter:
Location: CSY/reowolf/src/runtime/my_tests.rs
3f6d45f84e76
4.6 KiB
application/rls-services+xml
trivial sync reintegrated. debug improvements. more logging
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | use crate as reowolf;
use crossbeam_utils::thread::scope;
use reowolf::{Connector, EndpointSetup, Polarity::*, ProtocolDescription};
use std::net::SocketAddr;
use std::{sync::Arc, time::Duration};
fn next_test_addr() -> SocketAddr {
use std::{
net::{Ipv4Addr, SocketAddrV4},
sync::atomic::{AtomicU16, Ordering::SeqCst},
};
static TEST_PORT: AtomicU16 = AtomicU16::new(5_000);
let port = TEST_PORT.fetch_add(1, SeqCst);
SocketAddrV4::new(Ipv4Addr::LOCALHOST, port).into()
}
lazy_static::lazy_static! {
static ref MINIMAL_PROTO: Arc<ProtocolDescription> =
{ Arc::new(reowolf::ProtocolDescription::parse(b"").unwrap()) };
}
#[test]
fn simple_connector() {
let c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
println!("{:#?}", c);
}
#[test]
fn new_port_pair() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [_, _] = c.new_port_pair();
let [_, _] = c.new_port_pair();
println!("{:#?}", c);
}
#[test]
fn new_sync() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [o, i] = c.new_port_pair();
c.add_component(b"sync", &[i, o]).unwrap();
println!("{:#?}", c);
}
#[test]
fn new_net_port() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let sock_addr = next_test_addr();
let _ = c.new_net_port(Getter, EndpointSetup { sock_addr, is_active: false }).unwrap();
let _ = c.new_net_port(Putter, EndpointSetup { sock_addr, is_active: true }).unwrap();
println!("{:#?}", c);
}
#[test]
fn trivial_connect() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
c.connect(Duration::from_secs(1)).unwrap();
println!("{:#?}", c);
}
#[test]
fn single_node_connect() {
let sock_addr = next_test_addr();
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let _ = c.new_net_port(Getter, EndpointSetup { sock_addr, is_active: false }).unwrap();
let _ = c.new_net_port(Putter, EndpointSetup { sock_addr, is_active: true }).unwrap();
let res = c.connect(Duration::from_secs(1));
println!("{:#?}", c);
c.get_logger().dump_log(&mut std::io::stdout().lock());
res.unwrap();
}
#[test]
fn multithreaded_connect() {
let sock_addr = next_test_addr();
scope(|s| {
s.spawn(|_| {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let _ = c.new_net_port(Getter, EndpointSetup { sock_addr, is_active: true }).unwrap();
c.connect(Duration::from_secs(1)).unwrap();
c.print_state();
});
s.spawn(|_| {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 1);
let _ = c.new_net_port(Putter, EndpointSetup { sock_addr, is_active: false }).unwrap();
c.connect(Duration::from_secs(1)).unwrap();
c.print_state();
});
})
.unwrap();
}
#[test]
fn put_no_sync() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [o, _] = c.new_port_pair();
c.connect(Duration::from_secs(1)).unwrap();
c.put(o, (b"hi" as &[_]).into()).unwrap();
}
#[test]
fn wrong_polarity_bad() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [_, i] = c.new_port_pair();
c.connect(Duration::from_secs(1)).unwrap();
c.put(i, (b"hi" as &[_]).into()).unwrap_err();
}
#[test]
fn dup_put_bad() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [o, _] = c.new_port_pair();
c.connect(Duration::from_secs(1)).unwrap();
c.put(o, (b"hi" as &[_]).into()).unwrap();
c.put(o, (b"hi" as &[_]).into()).unwrap_err();
}
#[test]
fn trivial_sync() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
c.connect(Duration::from_secs(1)).unwrap();
c.sync(Duration::from_secs(1)).unwrap();
c.print_state();
}
#[test]
fn unconnected_gotten_err() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [_, i] = c.new_port_pair();
assert_eq!(reowolf::error::GottenError::NoPreviousRound, c.gotten(i).unwrap_err());
}
#[test]
fn connected_gotten_err_no_round() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [_, i] = c.new_port_pair();
c.connect(Duration::from_secs(1)).unwrap();
assert_eq!(reowolf::error::GottenError::NoPreviousRound, c.gotten(i).unwrap_err());
}
#[test]
fn connected_gotten_err_ungotten() {
let mut c = Connector::new_simple(MINIMAL_PROTO.clone(), 0);
let [_, i] = c.new_port_pair();
c.connect(Duration::from_secs(1)).unwrap();
c.sync(Duration::from_secs(1)).unwrap();
assert_eq!(reowolf::error::GottenError::PortDidntGet, c.gotten(i).unwrap_err());
}
|