Skip to content

Commit

Permalink
Update support for SDK Internal events, And added some unit tests for…
Browse files Browse the repository at this point in the history
… Network internal (#41)

* update testing for bus impl

* support sdk internal events, add test internal netwk

* test internal and transport event of network

* update more core unit tests
  • Loading branch information
luongngocminh committed Nov 10, 2023
1 parent 7072dfc commit 0448da6
Show file tree
Hide file tree
Showing 27 changed files with 963 additions and 102 deletions.
9 changes: 7 additions & 2 deletions examples/examples/manual_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bluesea_identity::{NodeAddr, NodeAddrBuilder, Protocol};
use clap::Parser;
use key_value::{KeyValueBehaviorEvent, KeyValueHandlerEvent};
use key_value::{KeyValueBehaviorEvent, KeyValueHandlerEvent, KeyValueSdkEvent};
use layers_spread_router::SharedRouter;
use layers_spread_router_sync::{LayersSpreadRouterSyncBehavior, LayersSpreadRouterSyncBehaviorEvent, LayersSpreadRouterSyncHandlerEvent};
use manual_discovery::{ManualBehavior, ManualBehaviorConf, ManualBehaviorEvent, ManualHandlerEvent};
Expand Down Expand Up @@ -28,6 +28,11 @@ enum NodeHandleEvent {
KeyValue(KeyValueHandlerEvent),
}

#[derive(convert_enum::From, convert_enum::TryInto)]
enum NodeSdkEvent {
KeyValue(KeyValueSdkEvent),
}

/// Node with manual network builder
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -92,7 +97,7 @@ async fn main() {
plan_cfg.behaviors.push(Box::new(tun_tap));
}

let mut plane = NetworkPlane::<NodeBehaviorEvent, NodeHandleEvent>::new(plan_cfg);
let mut plane = NetworkPlane::<NodeBehaviorEvent, NodeHandleEvent, NodeSdkEvent>::new(plan_cfg);

plane.started();

Expand Down
53 changes: 53 additions & 0 deletions packages/core/identity/src/conn_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,56 @@ impl Hash for ConnId {
state.write_u64(self.value);
}
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_conn_id_from_out() {
let conn_id = ConnId::from_out(1, 123);
assert_eq!(conn_id.protocol(), 1);
assert_eq!(conn_id.direction(), ConnDirection::Outgoing);
assert_eq!(conn_id.uuid(), 123);
}

#[test]
fn test_conn_id_from_in() {
let conn_id = ConnId::from_in(2, 456);
assert_eq!(conn_id.protocol(), 2);
assert_eq!(conn_id.direction(), ConnDirection::Incoming);
assert_eq!(conn_id.uuid(), 456);
}

#[test]
fn test_conn_id_display() {
let conn_id = ConnId::from_out(3, 789);
assert_eq!(format!("{}", conn_id), "Conn(Outgoing,3,789)");
}

#[test]
fn test_conn_id_debug() {
let conn_id = ConnId::from_in(4, 101112);
assert_eq!(format!("{:?}", conn_id), "\"Conn(Incoming,4,101112)\"");
}

#[test]
fn test_conn_id_eq() {
let conn_id1 = ConnId::from_out(5, 131415);
let conn_id2 = ConnId::from_out(5, 131415);
let conn_id3 = ConnId::from_in(5, 131415);
assert_eq!(conn_id1, conn_id2);
assert_ne!(conn_id1, conn_id3);
}

#[test]
fn test_conn_id_hash() {
let conn_id1 = ConnId::from_out(6, 161718);
let conn_id2 = ConnId::from_out(6, 161718);
let conn_id3 = ConnId::from_in(6, 161718);
let mut set = std::collections::HashSet::new();
set.insert(conn_id1);
assert!(set.contains(&conn_id1));
assert!(set.contains(&conn_id2));
assert!(!set.contains(&conn_id3));
}
}
74 changes: 68 additions & 6 deletions packages/core/identity/src/node_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub type NodeId = u32;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NodeSegment {
Public,
Private(u8),
Expand Down Expand Up @@ -74,6 +75,7 @@ impl NodeIdType for NodeId {
*self as u8
}

// TODO: typo in name, should be eq_until_layer
fn eq_util_layer(&self, other: &Self) -> u8 {
if self.layer(3) != other.layer(3) {
return 4;
Expand All @@ -97,6 +99,8 @@ impl NodeIdType for NodeId {

#[cfg(test)]
mod tests {
use crate::NodeSegment;

use super::{NodeId, NodeIdType};

#[test]
Expand All @@ -107,12 +111,70 @@ mod tests {
}

#[test]
fn simple_distance() {
fn test_random() {
let id1 = NodeId::random();
let id2 = NodeId::random();
assert_ne!(id1, id2);
}

#[test]
fn test_segment() {
let id1: NodeId = 30;
assert_eq!(id1.segment(), NodeSegment::Public);
}

#[test]
fn test_distance() {
let id1: NodeId = 0;
let id2: NodeId = 1;
let id3: NodeId = 2;
let id4: NodeId = 3;
let id5: NodeId = u32::MAX;

assert_eq!(id1.distance(&id1), 0);
assert_eq!(id1.distance_bits(&id1), 0);

assert_eq!(id1.distance_bits(&id2), 1);
assert_eq!(id1.distance_bits(&id3), 2);
assert_eq!(id1.distance_bits(&id4), 2);
assert_eq!(id1.distance_bits(&id5), 32);
}

#[test]
fn test_bucket_index() {
let id1: NodeId = 0;
assert_eq!(id1.distance_bits(&0), 0);
assert_eq!(id1.distance_bits(&1), 1);
assert_eq!(id1.distance_bits(&2), 2);
assert_eq!(id1.distance_bits(&3), 2);
assert_eq!(id1.distance_bits(&u32::MAX), 32);
let id2: NodeId = 1;
let id3: NodeId = 2;
let id4: NodeId = 3;
let id5: NodeId = u32::MAX;

assert_eq!(id1.bucket_index(), 0);
assert_eq!(id2.bucket_index(), 1);
assert_eq!(id3.bucket_index(), 2);
assert_eq!(id4.bucket_index(), 2);
assert_eq!(id5.bucket_index(), 32);
}

#[test]
fn test_layer() {
let id1: NodeId = 0x12345678;
assert_eq!(id1.layer(0), 0x78);
assert_eq!(id1.layer(1), 0x56);
assert_eq!(id1.layer(2), 0x34);
assert_eq!(id1.layer(3), 0x12);
}

#[test]
fn test_eq_util_layer() {
let id1: NodeId = 0x12345678;
let id2: NodeId = 0x12345679;
let id3: NodeId = 0x12345778;
let id4: NodeId = 0x12355678;
let id5: NodeId = 0x12345678;

assert_eq!(id1.eq_util_layer(&id2), 1);
assert_eq!(id1.eq_util_layer(&id3), 2);
assert_eq!(id1.eq_util_layer(&id4), 3);
assert_eq!(id1.eq_util_layer(&id5), 0);
}
}
26 changes: 26 additions & 0 deletions packages/core/router/src/force_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,29 @@ impl RouterTable for ForceLocalRouter {
RouteAction::Local
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_path_to_node() {
let router = ForceLocalRouter();
let dest = NodeId::from(1u32);
assert_eq!(router.path_to_node(dest), RouteAction::Local);
}

#[test]
fn test_path_to_key() {
let router = ForceLocalRouter();
let key = NodeId::from(2u32);
assert_eq!(router.path_to_key(key), RouteAction::Local);
}

#[test]
fn test_path_to_service() {
let router = ForceLocalRouter();
let service_id = 3;
assert_eq!(router.path_to_service(service_id), RouteAction::Local);
}
}
26 changes: 26 additions & 0 deletions packages/core/router/src/force_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,29 @@ impl RouterTable for ForceNodeRouter {
RouteAction::Next(self.0, self.1)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_path_to_node() {
let router = ForceNodeRouter(ConnId::from_in(1, 1), NodeId::from(2u32));
let dest = NodeId::from(3u32);
assert_eq!(router.path_to_node(dest), RouteAction::Next(ConnId::from_in(1, 1), NodeId::from(2u32)));
}

#[test]
fn test_path_to_key() {
let router = ForceNodeRouter(ConnId::from_in(1, 1), NodeId::from(2u32));
let key = NodeId::from(4u32);
assert_eq!(router.path_to_key(key), RouteAction::Next(ConnId::from_in(1, 1), NodeId::from(2u32)));
}

#[test]
fn test_path_to_service() {
let router = ForceNodeRouter(ConnId::from_in(1, 1), NodeId::from(2u32));
let service_id = 5;
assert_eq!(router.path_to_service(service_id), RouteAction::Next(ConnId::from_in(1, 1), NodeId::from(2u32)));
}
}
48 changes: 48 additions & 0 deletions packages/core/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum RouteRule {
}

/// Determine the destination of an action/message
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RouteAction {
/// Reject the message
Reject,
Expand Down Expand Up @@ -62,3 +63,50 @@ pub trait RouterTable: Send + Sync {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_local() {
let local = RouteAction::Local;
let remote = RouteAction::Next(ConnId::from_in(1, 1), 2);
let reject = RouteAction::Reject;

assert!(local.is_local());
assert!(!remote.is_local());
assert!(!reject.is_local());
}

#[test]
fn test_is_reject() {
let local = RouteAction::Local;
let remote = RouteAction::Next(ConnId::from_in(1, 1), 2);
let reject = RouteAction::Reject;

assert!(!local.is_reject());
assert!(!remote.is_reject());
assert!(reject.is_reject());
}

#[test]
fn test_is_remote() {
let local = RouteAction::Local;
let remote = RouteAction::Next(ConnId::from_in(1, 1), 2);
let reject = RouteAction::Reject;

assert!(!local.is_remote());
assert!(remote.is_remote());
assert!(!reject.is_remote());
}

#[test]
fn test_derive_action_to_service() {
let router = ForceLocalRouter();
let route = RouteRule::ToService(3);
let service_id = 1;

assert_eq!(router.derive_action(&route, service_id), RouteAction::Local);
}
}
5 changes: 2 additions & 3 deletions packages/core/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "utils"
version = "0.1.0"
edition = "2021"

[features]
auto-clear = []
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand All @@ -11,6 +13,3 @@ log = { workspace = true }
async-std = { workspace = true }
async-notify = { workspace = true }
async-trait = { workspace = true }

[features]
auto-clear = []
52 changes: 52 additions & 0 deletions packages/core/utils/src/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,55 @@ where
Self::from_iter(arr)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_insert_and_get() {
let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
assert_eq!(map.get(&"key1"), Some(&"value1"));
assert_eq!(map.get(&"key2"), Some(&"value2"));
}

#[test]
fn test_remove() {
let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
assert_eq!(map.remove(&"key1"), Some("value1"));
assert_eq!(map.get(&"key1"), None);
}

#[test]
fn test_contains_key() {
let mut map = HashMap::new();
map.insert("key1", "value1");
assert_eq!(map.contains_key(&"key1"), true);
assert_eq!(map.contains_key(&"key2"), false);
}

#[test]
fn test_len_and_is_empty() {
let mut map = HashMap::new();
assert_eq!(map.len(), 0);
assert_eq!(map.is_empty(), true);
map.insert("key1", "value1");
assert_eq!(map.len(), 1);
assert_eq!(map.is_empty(), false);
}

#[test]
fn test_iter() {
let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key1", &"value1")));
assert_eq!(iter.next(), Some((&"key2", &"value2")));
assert_eq!(iter.next(), None);
}
}

0 comments on commit 0448da6

Please sign in to comment.