overnet_core/
test_util.rsuse crate::labels::NodeId;
use anyhow::Error;
use std::sync::Arc;
pub static TEST_ROUTER_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);
pub struct NodeIdGenerator {
test_id: u64,
test_name: &'static str,
run: u64,
n: u64,
}
impl Iterator for NodeIdGenerator {
type Item = NodeId;
fn next(&mut self) -> Option<NodeId> {
let id = self.n;
if id >= 100 {
return None;
}
self.n += 1;
Some(self.node_id(id))
}
}
impl NodeIdGenerator {
pub fn new(test_name: &'static str, run: usize) -> NodeIdGenerator {
NodeIdGenerator {
test_id: crc::crc16::checksum_x25(test_name.as_bytes()) as u64,
test_name,
run: run as u64,
n: 1,
}
}
pub fn new_router(&mut self) -> Result<Arc<crate::router::Router>, Error> {
crate::router::Router::with_node_id(
self.next().ok_or_else(|| anyhow::format_err!("No more node ids available"))?,
None,
)
}
pub fn new_router_circuit_router(&mut self) -> Result<Arc<crate::router::Router>, Error> {
crate::router::Router::with_node_id(
self.next().ok_or_else(|| anyhow::format_err!("No more node ids available"))?,
Some(TEST_ROUTER_INTERVAL),
)
}
pub fn test_desc(&self) -> String {
format!("({}) {}", self.node_id(0).0, self.test_name)
}
pub fn node_id(&self, idx: u64) -> NodeId {
(self.test_id * 10000 * 100000 + self.run * 10000 + idx).into()
}
}
#[cfg(test)]
mod test {
#[fuchsia::test]
fn node_id_generator_works() {
let n = super::NodeIdGenerator::new("foo", 3);
assert_eq!(n.node_id(1), 26676000030001.into());
assert_eq!(&n.test_desc(), "(26676000030000) foo");
}
}