Files
@ ce6bcc0a0c26
Branch filter:
Location: CSY/reowolf/src/test/connector.rs - annotation
ce6bcc0a0c26
14.7 KiB
application/rls-services+xml
more examples: protocol alternator, natives alternating
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d 06f259bf8031 63a9f085c357 63a9f085c357 63a9f085c357 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d cc23d3cb40d3 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d cc23d3cb40d3 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d 06f259bf8031 b788ae5c2251 b788ae5c2251 58e19b42641f b788ae5c2251 a226385adc2d b788ae5c2251 e71e02fb13d4 e71e02fb13d4 91c57e99c66e a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d cc23d3cb40d3 91c57e99c66e 91c57e99c66e a226385adc2d a226385adc2d b788ae5c2251 b788ae5c2251 63a9f085c357 63a9f085c357 63a9f085c357 e71e02fb13d4 b788ae5c2251 b788ae5c2251 e71e02fb13d4 e71e02fb13d4 91c57e99c66e a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d 63a9f085c357 a226385adc2d a226385adc2d a226385adc2d a226385adc2d 63a9f085c357 63a9f085c357 a226385adc2d cc23d3cb40d3 91c57e99c66e 91c57e99c66e a226385adc2d a226385adc2d b788ae5c2251 b788ae5c2251 63a9f085c357 63a9f085c357 63a9f085c357 e71e02fb13d4 b788ae5c2251 b788ae5c2251 b788ae5c2251 b788ae5c2251 58e19b42641f 58e19b42641f 58e19b42641f 58e19b42641f a226385adc2d 58e19b42641f e71e02fb13d4 e71e02fb13d4 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d cc23d3cb40d3 a226385adc2d a226385adc2d a226385adc2d a226385adc2d 58e19b42641f 58e19b42641f e71e02fb13d4 e71e02fb13d4 e71e02fb13d4 58e19b42641f a226385adc2d 58e19b42641f 58e19b42641f 58e19b42641f e71e02fb13d4 e71e02fb13d4 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d 58e19b42641f a226385adc2d a226385adc2d a226385adc2d a226385adc2d 58e19b42641f 58e19b42641f a226385adc2d cc23d3cb40d3 a226385adc2d a226385adc2d a226385adc2d a226385adc2d 58e19b42641f 58e19b42641f e71e02fb13d4 e71e02fb13d4 e71e02fb13d4 58e19b42641f a226385adc2d 58e19b42641f 58e19b42641f 58e19b42641f 58e19b42641f 58e19b42641f a226385adc2d ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d ce6bcc0a0c26 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d ce6bcc0a0c26 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d ce6bcc0a0c26 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d ce6bcc0a0c26 a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d a226385adc2d ce6bcc0a0c26 ce6bcc0a0c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0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 ce6bcc0a0c26 | 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);
}
}",
b"main",
)
.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);
}
}",
b"main",
)
.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);
}
}",
b"main",
)
.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);
}
}
}
}",
b"main",
)
.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
}
}",
b"main",
)
.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);
}
}
}
}",
b"main",
)
.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());
}
static FORWARD: &[u8] = b"
primitive forward(in i, out o) {
while(true) synchronous {
put(o, get(i));
}
}";
#[test]
fn connect_natives() {
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr()];
do_all(&[
&|x| {
x.configure(FORWARD, b"forward").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(FORWARD, b"forward").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() {
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr()];
do_all(&[
//
&|x| {
x.configure(FORWARD, b"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, b"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());
},
]);
}
static SYNC: &[u8] = b"
primitive sync(in i, out o) {
while(true) synchronous {
if (fires(i)) put(o, get(i));
}
}";
#[test]
fn native_alt() {
/*
Alice -->sync--A|P-->sync--> Bob
*/
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr()];
const N: usize = 3;
do_all(&[
//
&|x| {
x.configure(SYNC, b"sync").unwrap();
x.bind_port(0, Native).unwrap();
x.bind_port(1, Active(addrs[0])).unwrap();
x.connect(timeout).unwrap();
let msg = b"HI".to_vec();
for _i in 0..N {
// round _i*2: batches: [0=>*]
assert_eq!(0, x.sync(timeout).unwrap());
// round _i*2+1: batches: [0=>HI]
x.put(0, msg.clone()).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
}
},
&|x| {
x.configure(SYNC, b"sync").unwrap();
x.bind_port(0, Passive(addrs[0])).unwrap();
x.bind_port(1, Native).unwrap();
x.connect(timeout).unwrap();
let expect = b"HI".to_vec();
for _i in 0..(2 * N) {
// round _i batches:[0=>*, 0=>HI]
x.next_batch().unwrap();
x.get(0).unwrap();
match x.sync(timeout).unwrap() {
0 => assert_eq!(Err(ReadGottenErr::DidNotGet), x.read_gotten(0)),
1 => assert_eq!(Ok(&expect[..]), x.read_gotten(0)),
_ => unreachable!(),
}
}
},
]);
}
static ALTERNATOR_2: &[u8] = b"
primitive alternator_2(in i, out a, out b) {
while(true) {
synchronous { put(a, get(i)); }
synchronous { put(b, get(i)); }
}
}";
#[test]
fn alternator_2() {
/* /--|-->A
Sender -->alternator_2
\--|-->B
*/
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr(), next_addr()];
const N: usize = 5;
do_all(&[
//
&|x| {
// Sender
x.configure(ALTERNATOR_2, b"alternator_2").unwrap();
x.bind_port(0, Native).unwrap();
x.bind_port(1, Passive(addrs[0])).unwrap();
x.bind_port(2, Passive(addrs[1])).unwrap();
x.connect(timeout).unwrap();
for _ in 0..N {
for _ in 0..2 {
x.put(0, b"hey".to_vec()).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
}
}
},
&|x| {
// A
x.configure(SYNC, b"sync").unwrap();
x.bind_port(0, Active(addrs[0])).unwrap();
x.bind_port(1, Native).unwrap();
x.connect(timeout).unwrap();
let expecting: &[u8] = b"hey";
for _ in 0..N {
// get msg round
x.get(0).unwrap();
assert_eq!(Ok(0), x.sync(timeout)); // GET ONE
assert_eq!(Ok(expecting), x.read_gotten(0));
// silent round
assert_eq!(Ok(0), x.sync(timeout)); // MISS ONE
assert_eq!(Err(ReadGottenErr::DidNotGet), x.read_gotten(0));
}
},
&|x| {
// B
x.configure(SYNC, b"sync").unwrap();
x.bind_port(0, Active(addrs[1])).unwrap();
x.bind_port(1, Native).unwrap();
x.connect(timeout).unwrap();
let expecting: &[u8] = b"hey";
for _ in 0..N {
// silent round
assert_eq!(Ok(0), x.sync(timeout)); // MISS ONE
assert_eq!(Err(ReadGottenErr::DidNotGet), x.read_gotten(0));
// get msg round
x.get(0).unwrap();
assert_eq!(Ok(0), x.sync(timeout)); // GET ONE
assert_eq!(Ok(expecting), x.read_gotten(0));
}
},
]);
}
static PARITY_ROUTER: &[u8] = b"
primitive parity_router(in i, out odd, out even) {
while(true) synchronous {
msg m = get(i);
if (m[0]%2==0) {
put(even, m);
} else {
put(odd, m);
}
}
}";
#[test]
// THIS DOES NOT YET WORK. TODOS are hit
fn parity_router() {
/* /--|-->Getsodd
Sender -->parity_router
\--|-->Getseven
*/
let timeout = Duration::from_millis(1_500);
let addrs = [next_addr(), next_addr()];
const N: usize = 1;
do_all(&[
//
&|x| {
// Sender
x.configure(PARITY_ROUTER, b"parity_router").unwrap();
x.bind_port(0, Native).unwrap();
x.bind_port(1, Passive(addrs[0])).unwrap();
x.bind_port(2, Passive(addrs[1])).unwrap();
x.connect(timeout).unwrap();
for i in 0..N {
let msg = vec![i as u8]; // messages [0], [1], [2], ...
x.put(0, msg).unwrap();
assert_eq!(0, x.sync(timeout).unwrap());
}
},
&|x| {
// Getsodd
x.configure(FORWARD, b"forward").unwrap();
x.bind_port(0, Active(addrs[0])).unwrap();
x.bind_port(1, Native).unwrap();
x.connect(timeout).unwrap();
for _ in 0..N {
// round _i batches:[0=>*, 0=>?]
x.next_batch().unwrap();
x.get(0).unwrap();
match x.sync(timeout).unwrap() {
0 => assert_eq!(Err(ReadGottenErr::DidNotGet), x.read_gotten(0)),
1 => {
let msg = x.read_gotten(0).unwrap();
assert!(msg[0] % 2 == 1); // assert msg is odd
}
_ => unreachable!(),
}
}
},
&|x| {
// Getseven
x.configure(FORWARD, b"forward").unwrap();
x.bind_port(0, Active(addrs[1])).unwrap();
x.bind_port(1, Native).unwrap();
x.connect(timeout).unwrap();
for _ in 0..N {
// round _i batches:[0=>*, 0=>?]
x.next_batch().unwrap();
x.get(0).unwrap();
match x.sync(timeout).unwrap() {
0 => assert_eq!(Err(ReadGottenErr::DidNotGet), x.read_gotten(0)),
1 => {
let msg = x.read_gotten(0).unwrap();
assert!(msg[0] % 2 == 0); // assert msg is even
}
_ => unreachable!(),
}
}
},
]);
}
|