#![allow(unused_imports, dead_code, unused, clippy::type_complexity)]
use super::{ReduceConfig, ReduceConfigWrapper, ReduceNode, reduce_config};
use crate::node::{InputStreams, Node, OutputStreams};
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};
fn create_input_streams() -> (
mpsc::Sender<Arc<dyn Any + Send + Sync>>,
mpsc::Sender<Arc<dyn Any + Send + Sync>>,
mpsc::Sender<Arc<dyn Any + Send + Sync>>,
mpsc::Sender<Arc<dyn Any + Send + Sync>>,
InputStreams,
) {
let (config_tx, config_rx) = mpsc::channel(10);
let (in_tx, in_rx) = mpsc::channel(10);
let (initial_tx, initial_rx) = mpsc::channel(10);
let (function_tx, function_rx) = mpsc::channel(10);
let mut inputs = HashMap::new();
inputs.insert(
"configuration".to_string(),
Box::pin(ReceiverStream::new(config_rx)) as crate::node::InputStream,
);
inputs.insert(
"in".to_string(),
Box::pin(ReceiverStream::new(in_rx)) as crate::node::InputStream,
);
inputs.insert(
"initial".to_string(),
Box::pin(ReceiverStream::new(initial_rx)) as crate::node::InputStream,
);
inputs.insert(
"function".to_string(),
Box::pin(ReceiverStream::new(function_rx)) as crate::node::InputStream,
);
(config_tx, in_tx, initial_tx, function_tx, inputs)
}
#[tokio::test]
async fn test_reduce_node_creation() {
let node = ReduceNode::new("test_reduce".to_string());
assert_eq!(node.name(), "test_reduce");
assert!(node.has_input_port("configuration"));
assert!(node.has_input_port("in"));
assert!(node.has_input_port("initial"));
assert!(node.has_input_port("function"));
assert!(node.has_output_port("out"));
assert!(node.has_output_port("error"));
}
#[tokio::test]
async fn test_reduce_sum() {
let node = ReduceNode::new("test_reduce".to_string());
let (_config_tx, in_tx, initial_tx, function_tx, inputs) = create_input_streams();
let mut outputs: OutputStreams = match node.execute(inputs).await {
Ok(outputs) => outputs,
Err(e) => panic!("Node execution failed: {}", e),
};
let sum_function: ReduceConfig = reduce_config(
|acc: Arc<dyn Any + Send + Sync>, value: Arc<dyn Any + Send + Sync>| async move {
if let (Ok(acc_i32), Ok(val_i32)) = (acc.downcast::<i32>(), value.downcast::<i32>()) {
Ok(Arc::new(*acc_i32 + *val_i32) as Arc<dyn Any + Send + Sync>)
} else {
Err("Expected i32".to_string())
}
},
);
let _ = initial_tx
.send(Arc::new(0i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = function_tx
.send(Arc::new(ReduceConfigWrapper::new(sum_function)) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(1i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(2i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(3i32) as Arc<dyn Any + Send + Sync>)
.await;
drop(in_tx); drop(initial_tx); drop(function_tx);
let out_stream = outputs.remove("out").unwrap();
let mut results: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
results.push(item);
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
if let Ok(sum) = results[0].clone().downcast::<i32>() {
assert_eq!(*sum, 6i32);
} else {
panic!("Result is not an i32");
}
}
#[tokio::test]
async fn test_reduce_empty_stream() {
let node = ReduceNode::new("test_reduce".to_string());
let (_config_tx, in_tx, initial_tx, function_tx, inputs) = create_input_streams();
let mut outputs: OutputStreams = match node.execute(inputs).await {
Ok(outputs) => outputs,
Err(e) => panic!("Node execution failed: {}", e),
};
let sum_function: ReduceConfig = reduce_config(
|acc: Arc<dyn Any + Send + Sync>, value: Arc<dyn Any + Send + Sync>| async move {
if let (Ok(acc_i32), Ok(val_i32)) = (acc.downcast::<i32>(), value.downcast::<i32>()) {
Ok(Arc::new(*acc_i32 + *val_i32) as Arc<dyn Any + Send + Sync>)
} else {
Err("Expected i32".to_string())
}
},
);
let _ = initial_tx
.send(Arc::new(0i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = function_tx
.send(Arc::new(ReduceConfigWrapper::new(sum_function)) as Arc<dyn Any + Send + Sync>)
.await;
drop(in_tx); drop(initial_tx); drop(function_tx);
let out_stream = outputs.remove("out").unwrap();
let mut results: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
results.push(item);
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
if let Ok(result) = results[0].clone().downcast::<i32>() {
assert_eq!(*result, 0i32); } else {
panic!("Result is not an i32");
}
}
#[tokio::test]
async fn test_reduce_single_value() {
let node = ReduceNode::new("test_reduce".to_string());
let (_config_tx, in_tx, initial_tx, function_tx, inputs) = create_input_streams();
let mut outputs: OutputStreams = match node.execute(inputs).await {
Ok(outputs) => outputs,
Err(e) => panic!("Node execution failed: {}", e),
};
let sum_function: ReduceConfig = reduce_config(
|acc: Arc<dyn Any + Send + Sync>, value: Arc<dyn Any + Send + Sync>| async move {
if let (Ok(acc_i32), Ok(val_i32)) = (acc.downcast::<i32>(), value.downcast::<i32>()) {
Ok(Arc::new(*acc_i32 + *val_i32) as Arc<dyn Any + Send + Sync>)
} else {
Err("Expected i32".to_string())
}
},
);
let _ = initial_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = function_tx
.send(Arc::new(ReduceConfigWrapper::new(sum_function)) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
.await;
drop(in_tx); drop(initial_tx); drop(function_tx);
let out_stream = outputs.remove("out").unwrap();
let mut results: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
results.push(item);
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
if let Ok(result) = results[0].clone().downcast::<i32>() {
assert_eq!(*result, 15i32);
} else {
panic!("Result is not an i32");
}
}
#[tokio::test]
async fn test_reduce_function_error() {
let node = ReduceNode::new("test_reduce".to_string());
let (_config_tx, in_tx, initial_tx, function_tx, inputs) = create_input_streams();
let mut outputs: OutputStreams = match node.execute(inputs).await {
Ok(outputs) => outputs,
Err(e) => panic!("Node execution failed: {}", e),
};
let error_function: ReduceConfig = reduce_config(
|acc: Arc<dyn Any + Send + Sync>, value: Arc<dyn Any + Send + Sync>| async move {
if let (Ok(acc_i32), Ok(val_i32)) = (acc.downcast::<i32>(), value.downcast::<i32>()) {
if *val_i32 < 0 {
Err("Negative values not allowed".to_string())
} else {
Ok(Arc::new(*acc_i32 + *val_i32) as Arc<dyn Any + Send + Sync>)
}
} else {
Err("Expected i32".to_string())
}
},
);
let _ = initial_tx
.send(Arc::new(0i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = function_tx
.send(Arc::new(ReduceConfigWrapper::new(error_function)) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(1i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(-2i32) as Arc<dyn Any + Send + Sync>)
.await;
drop(in_tx);
drop(initial_tx);
drop(function_tx);
let error_stream: crate::node::OutputStream = outputs.remove("error").unwrap();
let mut errors: Vec<String> = Vec::new();
let mut stream = error_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
if let Ok(arc_str) = Arc::downcast::<String>(item.clone()) {
errors.push((*arc_str).clone());
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(errors.len(), 1);
assert_eq!(&*errors[0], "Negative values not allowed");
}
#[tokio::test]
async fn test_reduce_f64() {
let node = ReduceNode::new("test_reduce".to_string());
let (_config_tx, in_tx, initial_tx, function_tx, inputs) = create_input_streams();
let mut outputs: OutputStreams = match node.execute(inputs).await {
Ok(outputs) => outputs,
Err(e) => panic!("Node execution failed: {}", e),
};
let sum_function: ReduceConfig = reduce_config(
|acc: Arc<dyn Any + Send + Sync>, value: Arc<dyn Any + Send + Sync>| async move {
if let (Ok(acc_f64), Ok(val_f64)) = (acc.downcast::<f64>(), value.downcast::<f64>()) {
Ok(Arc::new(*acc_f64 + *val_f64) as Arc<dyn Any + Send + Sync>)
} else {
Err("Expected f64".to_string())
}
},
);
let _ = initial_tx
.send(Arc::new(0.0f64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = function_tx
.send(Arc::new(ReduceConfigWrapper::new(sum_function)) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(1.5f64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(2.5f64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in_tx
.send(Arc::new(3.5f64) as Arc<dyn Any + Send + Sync>)
.await;
drop(in_tx);
drop(initial_tx);
drop(function_tx);
let out_stream = outputs.remove("out").unwrap();
let mut results: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
results.push(item);
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
if let Ok(sum) = results[0].clone().downcast::<f64>() {
assert!((*sum - 7.5).abs() < 0.0001);
} else {
panic!("Result is not an f64");
}
}