Skip to content

Commit

Permalink
update testing for bus impl (#37)
Browse files Browse the repository at this point in the history
squash
  • Loading branch information
luongngocminh committed Nov 3, 2023
1 parent 09d5d9c commit b0ce07c
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -51,4 +51,5 @@ rand = "0.8"
parking_lot = "0.12"
env_logger = "0.10"
allocation-counter = { version = "*" }
clap = { version = "4.3.0", features = ["derive", "env"] }
clap = { version = "4.3.0", features = ["derive", "env"] }
mockall = "0.11.4"
4 changes: 4 additions & 0 deletions packages/core/router/Cargo.toml
Expand Up @@ -3,7 +3,11 @@ name = "bluesea-router"
version = "0.1.0"
edition = "2021"

[features]
mock = ["mockall"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bluesea-identity = { workspace = true }
mockall = { workspace = true, optional = true }
19 changes: 14 additions & 5 deletions packages/core/router/src/lib.rs
Expand Up @@ -5,6 +5,9 @@ use bluesea_identity::{ConnId, NodeId};
pub use force_local::ForceLocalRouter;
pub use force_node::ForceNodeRouter;

#[cfg(any(test, feature = "mock"))]
use mockall::automock;

/// ServiceMeta is using for determine which node will be routed, example node with lowest price or lowest latency, which for future use
pub type ServiceMeta = u32;

Expand All @@ -16,9 +19,13 @@ pub enum RouteRule {
ToKey(NodeId),
}

/// Determine the destination of an action/message
pub enum RouteAction {
/// Reject the message
Reject,
/// Will be processed locally
Local,
/// Will be forward to the given node
Next(ConnId, NodeId),
}

Expand All @@ -36,15 +43,17 @@ impl RouteAction {
}
}

#[cfg_attr(any(test, feature = "mock"), automock)]
pub trait RouterTable: Send + Sync {
/// This is used for finding path for sending data out to node
/// Determine the next action for the given destination node
fn path_to_node(&self, dest: NodeId) -> RouteAction;
/// This is used for finding path for sending data out to key
/// Determine the next action for the given key
fn path_to_key(&self, key: NodeId) -> RouteAction;
/// This is used for finding path for sending data out to service
/// Determine the next action for the given service
fn path_to_service(&self, service_id: u8) -> RouteAction;
/// This is used only for determine next action for incomming messages
fn action_for_incomming(&self, route: &RouteRule, service_id: u8) -> RouteAction {
/// Determine next action for incoming messages
/// given the route rule and service id
fn derive_action(&self, route: &RouteRule, service_id: u8) -> RouteAction {
match route {
RouteRule::Direct => RouteAction::Local,
RouteRule::ToNode(dest) => self.path_to_node(*dest),
Expand Down
4 changes: 3 additions & 1 deletion packages/network/Cargo.toml
Expand Up @@ -21,4 +21,6 @@ bytes = "1.4.0"
bincode = "1.3.3"

[dev-dependencies]
env_logger = { workspace = true }
env_logger = { workspace = true }
mockall = { workspace = true }
bluesea-router = { workspace = true, features = ["mock"]}
3 changes: 3 additions & 0 deletions packages/network/src/plane.rs
Expand Up @@ -54,8 +54,11 @@ impl<BE: Send + Sync + 'static, HE: Send + Sync + 'static> Awaker for HandlerAwa
}
}

#[derive(Debug, Eq, PartialEq)]
pub enum NetworkPlaneInternalEvent<BE> {
/// Trigger on_awake() hook for the behavior of the given service.
AwakeBehaviour { service_id: u8 },
/// An Event sent from the Handler layer to the Behaviour layer
ToBehaviourFromHandler { service_id: u8, node_id: NodeId, conn_id: ConnId, event: BE },
ToBehaviourLocalMsg { service_id: u8, msg: TransportMsg },
IncomingDisconnected(NodeId, ConnId),
Expand Down
9 changes: 9 additions & 0 deletions packages/network/src/plane/bus.rs
Expand Up @@ -2,6 +2,7 @@ use bluesea_identity::{ConnId, NodeId};

use crate::msg::TransportMsg;

#[derive(Debug, PartialEq, Eq)]
pub(crate) enum HandleEvent<HE> {
Awake,
FromBehavior(HE),
Expand All @@ -15,11 +16,19 @@ pub enum HandlerRoute {
}

pub(crate) trait PlaneBus<BE, HE>: Send + Sync {
/// Trigger the internal awake behaviour event for the given service.
/// This will call the on_awaker() method of the behaviour.
fn awake_behaviour(&self, service: u8) -> Option<()>;
/// Sends an Awake Handler event to the given service of a connection.
fn awake_handler(&self, service: u8, conn: ConnId) -> Option<()>;
/// Forward the given event from the handler to the behaviour layer.
fn to_behaviour_from_handler(&self, service_id: u8, node_id: NodeId, conn_id: ConnId, event: BE) -> Option<()>;
/// Sends an Event to the Handler of the given service by connection Id or node Id.
fn to_handler(&self, service_id: u8, route: HandlerRoute, event: HandleEvent<HE>) -> Option<()>;
/// Sends a Message to the network layer.
fn to_net(&self, msg: TransportMsg) -> Option<()>;
/// Sends a Message to the network layer, specify the destination node
fn to_net_node(&self, node: NodeId, msg: TransportMsg) -> Option<()>;
/// Sends a Message to the network layer, specify the destination connection
fn to_net_conn(&self, conn_id: ConnId, msg: TransportMsg) -> Option<()>;
}

0 comments on commit b0ce07c

Please sign in to comment.