overnet_core/
test_util.rs
1use crate::labels::NodeId;
6use anyhow::Error;
7use std::sync::Arc;
8
9pub static TEST_ROUTER_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);
10
11pub struct NodeIdGenerator {
13 test_id: u64,
14 test_name: &'static str,
15 run: u64,
16 n: u64,
17}
18
19impl Iterator for NodeIdGenerator {
20 type Item = NodeId;
21 fn next(&mut self) -> Option<NodeId> {
22 let id = self.n;
23 if id >= 100 {
24 return None;
25 }
26 self.n += 1;
27 Some(self.node_id(id))
28 }
29}
30
31impl NodeIdGenerator {
32 pub fn new(test_name: &'static str, run: usize) -> NodeIdGenerator {
34 NodeIdGenerator {
35 test_id: crc::crc16::checksum_x25(test_name.as_bytes()) as u64,
36 test_name,
37 run: run as u64,
38 n: 1,
39 }
40 }
41
42 pub fn new_router(&mut self) -> Result<Arc<crate::router::Router>, Error> {
44 crate::router::Router::with_node_id(
45 self.next().ok_or_else(|| anyhow::format_err!("No more node ids available"))?,
46 None,
47 )
48 }
49
50 pub fn new_router_circuit_router(&mut self) -> Result<Arc<crate::router::Router>, Error> {
52 crate::router::Router::with_node_id(
53 self.next().ok_or_else(|| anyhow::format_err!("No more node ids available"))?,
54 Some(TEST_ROUTER_INTERVAL),
55 )
56 }
57
58 pub fn test_desc(&self) -> String {
60 format!("({}) {}", self.node_id(0).0, self.test_name)
61 }
62
63 pub fn node_id(&self, idx: u64) -> NodeId {
65 (self.test_id * 10000 * 100000 + self.run * 10000 + idx).into()
66 }
67}
68
69#[cfg(test)]
70mod test {
71
72 #[fuchsia::test]
73 fn node_id_generator_works() {
74 let n = super::NodeIdGenerator::new("foo", 3);
75 assert_eq!(n.node_id(1), 26676000030001.into());
76 assert_eq!(&n.test_desc(), "(26676000030000) foo");
77 }
78}