use crate::node::{InputStreams, Node};
use crate::nodes::common::TestSender;
use crate::nodes::math::MinNode;
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() -> (TestSender, TestSender, TestSender, InputStreams) {
let (config_tx, config_rx) = mpsc::channel(10);
let (in1_tx, in1_rx) = mpsc::channel(10);
let (in2_tx, in2_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(
"in1".to_string(),
Box::pin(ReceiverStream::new(in1_rx)) as crate::node::InputStream,
);
inputs.insert(
"in2".to_string(),
Box::pin(ReceiverStream::new(in2_rx)) as crate::node::InputStream,
);
(config_tx, in1_tx, in2_tx, inputs)
}
#[tokio::test]
async fn test_min_node_creation() {
let node = MinNode::new("test_min".to_string());
assert_eq!(node.name(), "test_min");
assert!(node.has_input_port("configuration"));
assert!(node.has_input_port("in1"));
assert!(node.has_input_port("in2"));
assert!(node.has_output_port("out"));
assert!(node.has_output_port("error"));
}
#[tokio::test]
async fn test_min_node_i32() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_i32) = item.downcast::<i32>() {
results.push(*arc_i32);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 5);
}
#[tokio::test]
async fn test_min_node_i32_equal() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_i32) = item.downcast::<i32>() {
results.push(*arc_i32);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 5);
}
#[tokio::test]
async fn test_min_node_i64() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(100i64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(50i64) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_i64) = item.downcast::<i64>() {
results.push(*arc_i64);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 50i64);
}
#[tokio::test]
async fn test_min_node_u32() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(10u32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(5u32) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_u32) = item.downcast::<u32>() {
results.push(*arc_u32);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 5u32);
}
#[tokio::test]
async fn test_min_node_u64() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(100u64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(50u64) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_u64) = item.downcast::<u64>() {
results.push(*arc_u64);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 50u64);
}
#[tokio::test]
async fn test_min_node_f32() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(3.5f32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(2.5f32) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_f32) = item.downcast::<f32>() {
results.push(*arc_f32);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 2.5f32);
}
#[tokio::test]
async fn test_min_node_f64() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(3.5f64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(2.5f64) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_f64) = item.downcast::<f64>() {
results.push(*arc_f64);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 2.5f64);
}
#[tokio::test]
async fn test_min_node_type_promotion_i32_to_i64() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(5i64) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_i64) = item.downcast::<i64>() {
results.push(*arc_i64);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 5i64);
}
#[tokio::test]
async fn test_min_node_type_promotion_i32_to_f64() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(5.0f64) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_f64) = item.downcast::<f64>() {
results.push(*arc_f64);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 5.0f64);
}
#[tokio::test]
async fn test_min_node_unsupported_type() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new("hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
.await;
let error_stream = outputs.remove("error").unwrap();
let mut errors = 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_string) = item.downcast::<String>() {
errors.push(arc_string.clone());
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("Unsupported types for minimum"));
}
#[tokio::test]
async fn test_min_node_negative_values() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(-5i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(-10i32) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_i32) = item.downcast::<i32>() {
results.push(*arc_i32);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], -10);
}
#[tokio::test]
async fn test_min_node_f32_negative() {
let node = MinNode::new("test_min".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new(-2.5f32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(-3.5f32) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = 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 {
if let Ok(arc_f32) = item.downcast::<f32>() {
results.push(*arc_f32);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], -3.5f32);
}