use crate::node::{InputStreams, Node};
use crate::nodes::common::TestSender;
use crate::nodes::math::LogNode;
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 (in_tx, in_rx) = mpsc::channel(10);
let (base_tx, base_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(
"base".to_string(),
Box::pin(ReceiverStream::new(base_rx)) as crate::node::InputStream,
);
(config_tx, in_tx, base_tx, inputs)
}
#[tokio::test]
async fn test_log_node_creation() {
let node = LogNode::new("test_log".to_string());
assert_eq!(node.name(), "test_log");
assert!(node.has_input_port("configuration"));
assert!(node.has_input_port("in"));
assert!(node.has_input_port("base"));
assert!(node.has_output_port("out"));
assert!(node.has_output_port("error"));
}
#[tokio::test]
async fn test_log_node_base_10() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(100i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_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_f64) = item.downcast::<f64>() {
results.push(*arc_f64);
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert!((results[0] - 2.0).abs() < 1e-10);
}
#[tokio::test]
async fn test_log_node_base_2() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(8i64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(2i64) 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!((results[0] - 3.0).abs() < 1e-10);
}
#[tokio::test]
async fn test_log_node_base_e() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(std::f64::consts::E) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(std::f64::consts::E) 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!((results[0] - 1.0).abs() < 1e-10);
}
#[tokio::test]
async fn test_log_node_f32() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(4.0f32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(2.0f32) 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!((results[0] - 2.0).abs() < 1e-10);
}
#[tokio::test]
async fn test_log_node_f64() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(9.0f64) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(3.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!((results[0] - 2.0).abs() < 1e-10);
}
#[tokio::test]
async fn test_log_node_negative_value() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(-10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(10i32) 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("Logarithm value must be positive"));
}
#[tokio::test]
async fn test_log_node_zero_value() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(0i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(10i32) 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("Logarithm value must be positive"));
}
#[tokio::test]
async fn test_log_node_invalid_base_zero() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(0i32) 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("Logarithm base must be positive and not equal to 1"));
}
#[tokio::test]
async fn test_log_node_invalid_base_one() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(1i32) 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("Logarithm base must be positive and not equal to 1"));
}
#[tokio::test]
async fn test_log_node_negative_base() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(-10i32) 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("Logarithm base must be positive and not equal to 1"));
}
#[tokio::test]
async fn test_log_node_unsupported_type() {
let node = LogNode::new("test_log".to_string());
let (_config_tx, in_tx, base_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = base_tx
.send(Arc::new(10i32) 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 type for logarithm"));
}