Changeset - b7be5c34cd3a
[Not reviewed]
0 3 0
Christopher Esterhuyse - 5 years ago 2020-02-06 15:24:47
christopheresterhuyse@gmail.com
cleaned up tests
3 files changed with 77 insertions and 95 deletions:
0 comments (0 inline, 0 general)
src/test/connector.rs
Show inline comments
 
@@ -5,18 +5,6 @@ use super::*;
 
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()
 
}
 

	
 
static PDL: &[u8] = b"
 
primitive blocked(in i, out o) {
 
    while(true) synchronous {}
 
@@ -75,7 +63,7 @@ fn connects_ok() {
 
    */
 
    let timeout = Duration::from_millis(1_500);
 
    let addrs = [next_addr()];
 
    do_all(&[
 
    assert!(do_all(&[
 
        &|x| {
 
            // Alice
 
            x.configure(PDL, b"blocked").unwrap();
 
@@ -90,7 +78,7 @@ fn connects_ok() {
 
            x.bind_port(1, Native).unwrap();
 
            x.connect(timeout).unwrap();
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -101,7 +89,7 @@ fn connected_but_silent_natives() {
 
    */
 
    let timeout = Duration::from_millis(1_500);
 
    let addrs = [next_addr()];
 
    do_all(&[
 
    assert!(do_all(&[
 
        &|x| {
 
            // Alice
 
            x.configure(PDL, b"blocked").unwrap();
 
@@ -118,7 +106,7 @@ fn connected_but_silent_natives() {
 
            x.connect(timeout).unwrap();
 
            assert_eq!(Ok(0), x.sync(timeout));
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -134,7 +122,7 @@ fn self_forward_ok() {
 
    let timeout = Duration::from_millis(1_500);
 
    const N: usize = 5;
 
    static MSG: &[u8] = b"Echo!";
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Alice
 
@@ -149,7 +137,7 @@ fn self_forward_ok() {
 
                assert_eq!(Ok(MSG), x.read_gotten(1));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 
#[test]
 
fn token_spout_ok() {
 
@@ -160,7 +148,7 @@ fn token_spout_ok() {
 
    */
 
    let timeout = Duration::from_millis(1_500);
 
    const N: usize = 5;
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Alice
 
@@ -173,7 +161,7 @@ fn token_spout_ok() {
 
                assert_eq!(Ok(&[] as &[u8]), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -184,7 +172,7 @@ fn waiter_ok() {
 
    Alice<--token_spout
 
    */
 
    let timeout = Duration::from_millis(1_500);
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Alice
 
@@ -199,7 +187,7 @@ fn waiter_ok() {
 
            assert_eq!(Ok(0), x.sync(timeout));
 
            assert_eq!(Ok(&[] as &[u8]), x.read_gotten(0));
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -214,7 +202,7 @@ fn self_forward_timeout() {
 
    */
 
    let timeout = Duration::from_millis(500);
 
    static MSG: &[u8] = b"Echo!";
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Sender
 
@@ -226,7 +214,7 @@ fn self_forward_timeout() {
 
            // native and forward components cannot find a solution
 
            assert_eq!(Err(SyncErr::Timeout), x.sync(timeout));
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -239,7 +227,8 @@ fn forward_det() {
 
    let addrs = [next_addr()];
 
    const N: usize = 5;
 
    static MSG: &[u8] = b"Hello!";
 
    do_all(&[
 

	
 
    assert!(do_all(&[
 
        &|x| {
 
            x.configure(PDL, b"forward").unwrap();
 
            x.bind_port(0, Native).unwrap();
 
@@ -261,7 +250,7 @@ fn forward_det() {
 
                assert_eq!(Ok(MSG), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -275,7 +264,7 @@ fn nondet_proto_det_natives() {
 
    let addrs = [next_addr()];
 
    const N: usize = 5;
 
    static MSG: &[u8] = b"Message, here!";
 
    do_all(&[
 
    assert!(do_all(&[
 
        &|x| {
 
            // Alice
 
            x.configure(PDL, b"sync").unwrap();
 
@@ -299,7 +288,7 @@ fn nondet_proto_det_natives() {
 
                assert_eq!(Ok(MSG), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -312,7 +301,7 @@ fn putter_determines() {
 
    let addrs = [next_addr()];
 
    const N: usize = 3;
 
    static MSG: &[u8] = b"Hidey ho!";
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Alice
 
@@ -339,7 +328,7 @@ fn putter_determines() {
 
                assert_eq!(Ok(MSG), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -352,7 +341,7 @@ fn getter_determines() {
 
    let addrs = [next_addr()];
 
    const N: usize = 5;
 
    static MSG: &[u8] = b"Hidey ho!";
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Alice
 
@@ -380,7 +369,7 @@ fn getter_determines() {
 
                assert_eq!(Ok(MSG), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -395,7 +384,7 @@ fn alternator_2() {
 
    let addrs = [next_addr(), next_addr()];
 
    const N: usize = 5;
 
    static MSG: &[u8] = b"message";
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Sender
 
@@ -447,7 +436,7 @@ fn alternator_2() {
 
                assert_eq!(Ok(MSG), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -461,7 +450,7 @@ fn composite_chain() {
 
    let addrs = [next_addr(), next_addr()];
 
    const N: usize = 1;
 
    static MSG: &[u8] = b"Hippity Hoppity";
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Alice
 
@@ -487,7 +476,7 @@ fn composite_chain() {
 
                assert_eq!(Ok(MSG), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -501,7 +490,7 @@ fn exchange() {
 
    let timeout = Duration::from_millis(1_500);
 
    let addrs = [next_addr(), next_addr()];
 
    const N: usize = 1;
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Alice
 
@@ -533,7 +522,7 @@ fn exchange() {
 
                assert_eq!(Ok(b"A->B" as &[u8]), x.read_gotten(0));
 
            }
 
        },
 
    ]);
 
    ]));
 
}
 

	
 
#[test]
 
@@ -548,7 +537,7 @@ fn filter_messages() {
 
    let timeout = Duration::from_millis(1_500);
 
    let addrs = [next_addr()];
 
    const N: usize = 1;
 
    do_all(&[
 
    assert!(do_all(&[
 
        //
 
        &|x| {
 
            // Sender
 
@@ -599,5 +588,5 @@ fn filter_messages() {
 
                }
 
            }
 
        },
 
    ]);
 
    ]));
 
}
src/test/mod.rs
Show inline comments
 
@@ -2,10 +2,23 @@ use crate::common::ControllerId;
 
use crate::runtime::Connector;
 
use crate::runtime::Unconfigured;
 
use core::fmt::Debug;
 
use std::net::SocketAddr;
 

	
 
mod connector;
 
mod setup;
 

	
 
// 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()
 
}
 

	
 
struct Panicked(Box<dyn std::any::Any>);
 
impl Debug for Panicked {
 
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 
@@ -24,7 +37,11 @@ fn handle(result: Result<(), Box<(dyn std::any::Any + Send + 'static)>>) {
 
    }
 
}
 

	
 
fn do_all(i: &[&(dyn Fn(&mut Connector) + Sync)]) {
 
// Given a set of tasks (where each is some function that interacts with a connector)
 
// run each task in in its own thread.
 
// print the log and OK/PANIC result of each thread
 
// then finally, return true IFF no threads panicked
 
fn do_all(i: &[&(dyn Fn(&mut Connector) + Sync)]) -> bool {
 
    let cid_iter = 0..(i.len() as ControllerId);
 
    let mut connectors = cid_iter
 
        .clone()
 
@@ -44,7 +61,7 @@ fn do_all(i: &[&(dyn Fn(&mut Connector) + Sync)]) {
 
    })
 
    .unwrap();
 

	
 
    let mut failures = false;
 
    let mut alright = true;
 

	
 
    for ((controller_id, connector), res) in
 
        cid_iter.zip(connectors.iter_mut()).zip(results.into_iter())
 
@@ -57,12 +74,10 @@ fn do_all(i: &[&(dyn Fn(&mut Connector) + Sync)]) {
 
        match res {
 
            Ok(()) => println!("CID {:?} OK!", controller_id),
 
            Err(e) => {
 
                failures = true;
 
                alright = false;
 
                println!("CI {:?} PANIC! {:?}", controller_id, Panicked(e));
 
            }
 
        };
 
    }
 
    if failures {
 
        panic!("FAILURES!");
 
    }
 
    alright
 
}
src/test/setup.rs
Show inline comments
 
use super::*;
 
use crate::common::*;
 
use crate::runtime::*;
 

	
 
use PortBinding::*;
 

	
 
use super::*;
 

	
 
#[test]
 
fn config_ok_0() {
 
    let pdl = b"primitive main() {}";
 
@@ -28,84 +26,64 @@ fn config_non_port() {
 
    ProtocolD::parse(pdl).unwrap();
 
}
 

	
 
#[test]
 
fn bind_too_much() {
 
    let mut x = Connector::Unconfigured(Unconfigured { controller_id: 0 });
 
    x.configure(b"primitive main(in a) {}", b"main").unwrap();
 
    x.bind_port(0, Native).unwrap();
 
    assert!(x.bind_port(1, Native).is_err());
 
}
 

	
 
#[test]
 
fn config_and_connect_2() {
 
    let timeout = Duration::from_millis(1_500);
 
    let addrs = ["127.0.0.1:9000".parse().unwrap(), "127.0.0.1:9001".parse().unwrap()];
 
    use std::thread;
 
    let handles = vec![
 
        //
 
        thread::spawn(move || {
 
            let mut x = Connector::Unconfigured(Unconfigured { controller_id: 0 });
 
    let addrs = [next_addr(), next_addr()];
 
    assert!(do_all(&[
 
        &|x| {
 
            x.configure(b"primitive main(in a, out b) {}", b"main").unwrap();
 
            x.bind_port(0, Passive(addrs[0])).unwrap();
 
            x.bind_port(1, Passive(addrs[1])).unwrap();
 
            x.connect(timeout).unwrap();
 
        }),
 
        thread::spawn(move || {
 
            let mut x = Connector::Unconfigured(Unconfigured { controller_id: 1 });
 
        },
 
        &|x| {
 
            x.configure(b"primitive main(out a, in b) {}", b"main").unwrap();
 
            x.bind_port(0, Active(addrs[0])).unwrap();
 
            x.bind_port(1, Active(addrs[1])).unwrap();
 
            x.connect(timeout).unwrap();
 
        }),
 
    ];
 
    for h in handles {
 
        handle(h.join())
 
    }
 
}
 

	
 
#[test]
 
fn bind_too_much() {
 
    let mut x = Connector::Unconfigured(Unconfigured { controller_id: 0 });
 
    x.configure(b"primitive main(in a) {}", b"main").unwrap();
 
    x.bind_port(0, Native).unwrap();
 
    assert!(x.bind_port(1, Native).is_err());
 
        },
 
    ]));
 
}
 

	
 
#[test]
 
fn config_and_connect_chain() {
 
    let timeout = Duration::from_millis(1_500);
 
    let addrs = [
 
        "127.0.0.1:9002".parse().unwrap(),
 
        "127.0.0.1:9003".parse().unwrap(),
 
        "127.0.0.1:9004".parse().unwrap(),
 
    ];
 
    use std::thread;
 
    let handles = vec![
 
        //
 
        thread::spawn(move || {
 
    let addrs = [next_addr(), next_addr(), next_addr()];
 
    assert!(do_all(&[
 
        &|x| {
 
            // PRODUCER A->
 
            let mut x = Connector::Unconfigured(Unconfigured { controller_id: 0 });
 
            x.configure(b"primitive main(out a) {}", b"main").unwrap();
 
            x.bind_port(0, Active(addrs[0])).unwrap();
 
            x.connect(timeout).unwrap();
 
        }),
 
        thread::spawn(move || {
 
        },
 
        &|x| {
 
            // FORWARDER ->B->
 
            let mut x = Connector::Unconfigured(Unconfigured { controller_id: 1 });
 
            x.configure(b"primitive main(in a, out b) {}", b"main").unwrap();
 
            x.bind_port(0, Passive(addrs[0])).unwrap();
 
            x.bind_port(1, Active(addrs[1])).unwrap();
 
            x.connect(timeout).unwrap();
 
        }),
 
        thread::spawn(move || {
 
        },
 
        &|x| {
 
            // FORWARDER ->C->
 
            let mut x = Connector::Unconfigured(Unconfigured { controller_id: 2 });
 
            x.configure(b"primitive main(in a, out b) {}", b"main").unwrap();
 
            x.bind_port(0, Passive(addrs[1])).unwrap();
 
            x.bind_port(1, Active(addrs[2])).unwrap();
 
            x.connect(timeout).unwrap();
 
        }),
 
        thread::spawn(move || {
 
        },
 
        &|x| {
 
            // CONSUMER ->D
 
            let mut x = Connector::Unconfigured(Unconfigured { controller_id: 3 });
 
            x.configure(b"primitive main(in a) {}", b"main").unwrap();
 
            x.bind_port(0, Passive(addrs[2])).unwrap();
 
            x.connect(timeout).unwrap();
 
        }),
 
    ];
 
    for h in handles {
 
        handle(h.join())
 
    }
 
        },
 
    ]));
 
}
0 comments (0 inline, 0 general)