File size: 6,620 Bytes
224e773 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | use std::collections::HashMap;
/// Dispatcher orchestrates execution of routing nodes
pub struct Dispatcher {
execution_count: usize,
}
impl Dispatcher {
pub fn new() -> Self {
Self {
execution_count: 0,
}
}
/// Dispatch a single node execution
pub fn dispatch(&mut self, node_id: u64, message: &str) -> hyperkitty_core::Result<String> {
if message.is_empty() {
return Err(hyperkitty_core::Error::ParseError("empty_message".to_string()));
}
self.execution_count += 1;
Ok(format!(
"dispatch_{}(node: {}, msg: '{}', exec: {})",
self.execution_count, node_id, message, self.execution_count
))
}
/// Batch dispatch multiple nodes
pub fn dispatch_batch(
&mut self,
nodes: Vec<crate::nodes::RoutingNode>,
message: &str,
) -> hyperkitty_core::Result<HashMap<u64, String>> {
if nodes.is_empty() {
return Err(hyperkitty_core::Error::NoValidRoutes);
}
if message.is_empty() {
return Err(hyperkitty_core::Error::ParseError("empty_message".to_string()));
}
let mut results = HashMap::new();
for node in nodes {
if node.is_valid() {
self.execution_count += 1;
let result = format!(
"batch_exec_{}(node: {}, name: {}, weight: {:.4})",
self.execution_count, node.id, node.name, node.weight
);
results.insert(node.id, result);
}
}
if results.is_empty() {
return Err(hyperkitty_core::Error::NoValidRoutes);
}
Ok(results)
}
/// Get the number of dispatches executed
pub fn execution_count(&self) -> usize {
self.execution_count
}
/// Reset execution counter
pub fn reset(&mut self) {
self.execution_count = 0;
}
}
impl Default for Dispatcher {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dispatcher_creation() {
let dispatcher = Dispatcher::new();
assert_eq!(dispatcher.execution_count(), 0);
}
#[test]
fn test_dispatcher_single_dispatch() {
let mut dispatcher = Dispatcher::new();
let result = dispatcher.dispatch(42, "test message");
assert!(result.is_ok());
assert_eq!(dispatcher.execution_count(), 1);
}
#[test]
fn test_dispatcher_dispatch_rejects_empty_message() {
let mut dispatcher = Dispatcher::new();
let result = dispatcher.dispatch(1, "");
assert!(result.is_err());
assert_eq!(dispatcher.execution_count(), 0);
}
#[test]
fn test_dispatcher_increments_count() {
let mut dispatcher = Dispatcher::new();
let _ = dispatcher.dispatch(1, "msg1");
let _ = dispatcher.dispatch(2, "msg2");
let _ = dispatcher.dispatch(3, "msg3");
assert_eq!(dispatcher.execution_count(), 3);
}
#[test]
fn test_dispatcher_batch_dispatch_empty_nodes() {
let mut dispatcher = Dispatcher::new();
let result = dispatcher.dispatch_batch(vec![], "test");
assert!(result.is_err());
}
#[test]
fn test_dispatcher_batch_dispatch_empty_message() {
let mut dispatcher = Dispatcher::new();
let nodes = vec![crate::nodes::RoutingNode::new(1, "node".to_string(), 0.5)];
let result = dispatcher.dispatch_batch(nodes, "");
assert!(result.is_err());
}
#[test]
fn test_dispatcher_batch_dispatch_single_node() {
let mut dispatcher = Dispatcher::new();
let nodes = vec![crate::nodes::RoutingNode::new(100, "node1".to_string(), 0.5)];
let result = dispatcher.dispatch_batch(nodes, "message");
assert!(result.is_ok());
let results = result.unwrap();
assert_eq!(results.len(), 1);
assert!(results.contains_key(&100));
}
#[test]
fn test_dispatcher_batch_dispatch_multiple_nodes() {
let mut dispatcher = Dispatcher::new();
let nodes = vec![
crate::nodes::RoutingNode::new(1, "n1".to_string(), 0.5),
crate::nodes::RoutingNode::new(2, "n2".to_string(), 0.6),
crate::nodes::RoutingNode::new(3, "n3".to_string(), 0.7),
];
let result = dispatcher.dispatch_batch(nodes, "test");
assert!(result.is_ok());
let results = result.unwrap();
assert_eq!(results.len(), 3);
}
#[test]
fn test_dispatcher_batch_dispatch_skips_invalid() {
let mut dispatcher = Dispatcher::new();
let nodes = vec![
crate::nodes::RoutingNode::new(1, "valid".to_string(), 0.5),
crate::nodes::RoutingNode::new(2, "invalid".to_string(), 0.0),
];
let result = dispatcher.dispatch_batch(nodes, "test");
assert!(result.is_ok());
let results = result.unwrap();
assert_eq!(results.len(), 1);
assert!(results.contains_key(&1));
}
#[test]
fn test_dispatcher_reset() {
let mut dispatcher = Dispatcher::new();
let _ = dispatcher.dispatch(1, "msg");
assert_eq!(dispatcher.execution_count(), 1);
dispatcher.reset();
assert_eq!(dispatcher.execution_count(), 0);
}
#[test]
fn test_dispatcher_execution_count_increments_in_batch() {
let mut dispatcher = Dispatcher::new();
let nodes = vec![
crate::nodes::RoutingNode::new(1, "n1".to_string(), 0.5),
crate::nodes::RoutingNode::new(2, "n2".to_string(), 0.6),
];
let _ = dispatcher.dispatch_batch(nodes, "test");
assert_eq!(dispatcher.execution_count(), 2);
}
#[test]
fn test_dispatcher_dispatch_includes_node_id() {
let mut dispatcher = Dispatcher::new();
let result = dispatcher.dispatch(999, "msg");
let output = result.unwrap();
assert!(output.contains("999"));
}
#[test]
fn test_dispatcher_batch_includes_weights() {
let mut dispatcher = Dispatcher::new();
let nodes = vec![crate::nodes::RoutingNode::new(1, "node".to_string(), 0.7777)];
let result = dispatcher.dispatch_batch(nodes, "test");
let results = result.unwrap();
let output = results.get(&1).unwrap();
assert!(output.contains("0.7777"));
}
#[test]
fn test_dispatcher_default_trait() {
let dispatcher = Dispatcher::default();
assert_eq!(dispatcher.execution_count(), 0);
}
}
|