use crate::node::{InputStreams, Node};
use crate::nodes::common::TestSender;
use crate::nodes::while_loop_node::{WhileLoopConfig, WhileLoopNode, while_loop_config};
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 (data_tx, data_rx) = mpsc::channel(10);
let (break_tx, break_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(data_rx)) as crate::node::InputStream,
);
inputs.insert(
"condition".to_string(),
Box::pin(ReceiverStream::new(break_rx)) as crate::node::InputStream,
);
(config_tx, data_tx, break_tx, inputs)
}
#[tokio::test]
async fn test_while_loop_node_creation() {
let node = WhileLoopNode::new("test_while_loop".to_string());
assert_eq!(node.name(), "test_while_loop");
assert!(node.has_input_port("configuration"));
assert!(node.has_input_port("in"));
assert!(node.has_input_port("condition"));
assert!(node.has_output_port("out"));
assert!(node.has_output_port("break"));
assert!(node.has_output_port("error"));
assert!(!node.has_config());
}
#[tokio::test]
async fn test_while_loop_node_basic_loop() {
let node = WhileLoopNode::new("test_while_loop".to_string());
let config: WhileLoopConfig = while_loop_config(
|value| async move {
if let Ok(arc_i32) = value.downcast::<i32>() {
Ok(*arc_i32 < 5)
} else {
Err("Expected i32".to_string())
}
},
100, );
let (config_tx, data_tx, _break_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = config_tx
.send(Arc::new(Arc::new(config)) as Arc<dyn Any + Send + Sync>)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
let _ = data_tx
.send(Arc::new(3i32) as Arc<dyn Any + Send + Sync>)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(100)).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);
tokio::select! {
result = stream.next() => {
if let Some(item) = result
&& let Ok(arc_string) = item.downcast::<String>() {
errors.push(arc_string.clone());
}
}
_ = &mut timeout => {},
}
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("Maximum iterations"));
}
#[tokio::test]
async fn test_while_loop_node_exits_when_condition_false() {
let node = WhileLoopNode::new("test_while_loop".to_string());
let config: WhileLoopConfig = while_loop_config(
|_value| async move {
Ok(false) },
100,
);
let (config_tx, data_tx, _break_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = config_tx
.send(Arc::new(Arc::new(config)) as Arc<dyn Any + Send + Sync>)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
let _ = data_tx
.send(Arc::new(42i32) 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);
tokio::select! {
result = stream.next() => {
if let Some(item) = result
&& let Ok(arc_i32) = item.downcast::<i32>() {
results.push(*arc_i32);
}
}
_ = &mut timeout => {},
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 42);
}
#[tokio::test]
async fn test_while_loop_node_break_signal() {
let node = WhileLoopNode::new("test_while_loop".to_string());
let config: WhileLoopConfig = while_loop_config(
|_value| async move {
Ok(true) },
1000, );
let (config_tx, data_tx, break_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = config_tx
.send(Arc::new(Arc::new(config)) as Arc<dyn Any + Send + Sync>)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
let _ = data_tx
.send(Arc::new(10i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = break_tx
.send(Arc::new(()) as Arc<dyn Any + Send + Sync>)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
let break_stream = outputs.remove("break").unwrap();
let mut results = Vec::new();
let mut stream = break_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
tokio::select! {
result = stream.next() => {
if let Some(item) = result
&& let Ok(arc_i32) = item.downcast::<i32>() {
results.push(*arc_i32);
}
}
_ = &mut timeout => {},
}
assert_eq!(results.len(), 1);
assert_eq!(results[0], 10);
}
#[tokio::test]
async fn test_while_loop_node_error_on_no_config() {
let node = WhileLoopNode::new("test_while_loop".to_string());
let (_config_tx, data_tx, _break_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = data_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);
tokio::select! {
result = stream.next() => {
if let Some(item) = result
&& let Ok(arc_string) = item.downcast::<String>() {
errors.push(arc_string.clone());
}
}
_ = &mut timeout => {},
}
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("No configuration set"));
}
#[tokio::test]
async fn test_while_loop_node_condition_error() {
let node = WhileLoopNode::new("test_while_loop".to_string());
let config: WhileLoopConfig = while_loop_config(
|_value| async move { Err("Condition evaluation failed".to_string()) },
100,
);
let (config_tx, data_tx, _break_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = config_tx
.send(Arc::new(Arc::new(config)) as Arc<dyn Any + Send + Sync>)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
let _ = data_tx
.send(Arc::new(42i32) 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);
tokio::select! {
result = stream.next() => {
if let Some(item) = result
&& let Ok(arc_string) = item.downcast::<String>() {
errors.push(arc_string.clone());
}
}
_ = &mut timeout => {},
}
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("Condition evaluation failed"));
}