Files
@ a226385adc2d
Branch filter:
Location: CSY/reowolf/src/test/connector.rs
a226385adc2d
8.1 KiB
application/rls-services+xml
natives working
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | extern crate test_generator;
use super::*;
use std::thread;
use crate::common::*;
use crate::runtime::{errors::*, PortBinding::*, *};
// using a static AtomicU16, shared between all tests in the binary,
// allocate and return a socketaddr of the form 127.0.0.1:X where X in 7000..
fn next_addr() -> SocketAddr {
use std::{
net::{Ipv4Addr, SocketAddrV4},
sync::atomic::{AtomicU16, Ordering::SeqCst},
};
static TEST_PORT: AtomicU16 = AtomicU16::new(7_000);
let port = TEST_PORT.fetch_add(1, SeqCst);
SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port).into()
}
#[test]
fn incremental() {
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr(), next_addr()];
let handles = vec![
thread::spawn(move || {
let controller_id = 0;
let mut x = Connector::Unconfigured(Unconfigured { controller_id });
x.configure(
b"primitive main(out a, out b) {
synchronous {
msg m = create(0);
put(a, m);
}
}",
)
.unwrap();
x.bind_port(0, Passive(addrs[0])).unwrap();
x.bind_port(1, Passive(addrs[1])).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
println!("\n---------\nLOG CID={}\n{}", controller_id, x.get_mut_logger().unwrap());
}),
thread::spawn(move || {
let controller_id = 1;
let mut x = Connector::Unconfigured(Unconfigured { controller_id });
x.configure(
b"primitive main(in a, in b) {
synchronous {
get(a);
}
}",
)
.unwrap();
x.bind_port(0, Active(addrs[0])).unwrap();
x.bind_port(1, Active(addrs[1])).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
println!("\n---------\nLOG CID={}\n{}", controller_id, x.get_mut_logger().unwrap());
}),
];
for h in handles {
handle(h.join())
}
}
#[test]
fn duo_positive() {
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr(), next_addr()];
let a = thread::spawn(move || {
let controller_id = 0;
let mut x = Connector::Unconfigured(Unconfigured { controller_id });
x.configure(
b"primitive main(out a, out b) {
synchronous {}
synchronous {}
synchronous {
msg m = create(0);
put(a, m);
}
synchronous {
msg m = create(0);
put(b, m);
}
}",
)
.unwrap();
x.bind_port(0, Passive(addrs[0])).unwrap();
x.bind_port(1, Passive(addrs[1])).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
assert_eq!(0, x.sync(timeout).unwrap());
assert_eq!(0, x.sync(timeout).unwrap());
assert_eq!(0, x.sync(timeout).unwrap());
println!("\n---------\nLOG CID={}\n{}", controller_id, x.get_mut_logger().unwrap());
});
let b = thread::spawn(move || {
let controller_id = 1;
let mut x = Connector::Unconfigured(Unconfigured { controller_id });
x.configure(
b"primitive main(in a, in b) {
while (true) {
synchronous {
if (fires(a)) {
get(a);
}
}
synchronous {
if (fires(b)) {
get(b);
}
}
}
}",
)
.unwrap();
x.bind_port(0, Active(addrs[0])).unwrap();
x.bind_port(1, Active(addrs[1])).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
assert_eq!(0, x.sync(timeout).unwrap());
assert_eq!(0, x.sync(timeout).unwrap());
assert_eq!(0, x.sync(timeout).unwrap());
println!("\n---------\nLOG CID={}\n{}", controller_id, x.get_mut_logger().unwrap());
});
handle(a.join());
handle(b.join());
}
#[test]
fn duo_negative() {
let timeout = Duration::from_millis(500);
let addrs = [next_addr(), next_addr()];
let a = thread::spawn(move || {
let controller_id = 0;
let mut x = Connector::Unconfigured(Unconfigured { controller_id });
x.configure(
b"primitive main(out a, out b) {
synchronous {}
synchronous {
msg m = create(0);
put(a, m); // fires a on second round
}
}",
)
.unwrap();
x.bind_port(0, Passive(addrs[0])).unwrap();
x.bind_port(1, Passive(addrs[1])).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
let r = x.sync(timeout);
println!("\n---------\nLOG CID={}\n{}", controller_id, x.get_mut_logger().unwrap());
match r {
Err(SyncErr::Timeout) => {}
x => unreachable!("{:?}", x),
}
});
let b = thread::spawn(move || {
let controller_id = 1;
let mut x = Connector::Unconfigured(Unconfigured { controller_id });
x.configure(
b"primitive main(in a, in b) {
while (true) {
synchronous {
if (fires(a)) {
get(a);
}
}
synchronous {
if (fires(b)) { // never fire a on even round
get(b);
}
}
}
}",
)
.unwrap();
x.bind_port(0, Active(addrs[0])).unwrap();
x.bind_port(1, Active(addrs[1])).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
let r = x.sync(timeout);
println!("\n---------\nLOG CID={}\n{}", controller_id, x.get_mut_logger().unwrap());
match r {
Err(SyncErr::Timeout) => {}
x => unreachable!("{:?}", x),
}
});
handle(a.join());
handle(b.join());
}
#[test]
fn connect_natives() {
static CHAIN: &[u8] = b"
primitive main(in i, out o) {
while(true) synchronous {}
}";
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr()];
do_all(&[
&|x| {
x.configure(CHAIN).unwrap();
x.bind_port(0, Native).unwrap();
x.bind_port(1, Passive(addrs[0])).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
},
&|x| {
x.configure(CHAIN).unwrap();
x.bind_port(0, Active(addrs[0])).unwrap();
x.bind_port(1, Native).unwrap();
x.connect(timeout).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
},
]);
}
#[test]
fn forward() {
static FORWARD: &[u8] = b"
primitive main(in i, out o) {
while(true) synchronous {
put(o, get(i));
}
}";
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr()];
do_all(&[
//
&|x| {
x.configure(FORWARD).unwrap();
x.bind_port(0, Native).unwrap();
x.bind_port(1, Passive(addrs[0])).unwrap();
x.connect(timeout).unwrap();
let msg = b"HELLO!".to_vec();
x.put(0, msg).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
},
&|x| {
x.configure(FORWARD).unwrap();
x.bind_port(0, Active(addrs[0])).unwrap();
x.bind_port(1, Native).unwrap();
x.connect(timeout).unwrap();
let expect = b"HELLO!".to_vec();
x.get(0).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
assert_eq!(expect, x.read_gotten(0).unwrap());
},
]);
}
|