use crate::node::{InputStreams, Node};
use crate::nodes::time::ParseTimeNode;
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};
type AnySender = mpsc::Sender<Arc<dyn Any + Send + Sync>>;
fn create_input_streams() -> (AnySender, AnySender, AnySender, InputStreams) {
let (config_tx, config_rx) = mpsc::channel(10);
let (input_tx, input_rx) = mpsc::channel(10);
let (format_tx, format_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(input_rx)) as crate::node::InputStream,
);
inputs.insert(
"format".to_string(),
Box::pin(ReceiverStream::new(format_rx)) as crate::node::InputStream,
);
(config_tx, input_tx, format_tx, inputs)
}
#[tokio::test]
async fn test_parse_time_node_creation() {
let node = ParseTimeNode::new("test_parse_time".to_string());
assert_eq!(node.name(), "test_parse_time");
assert!(node.has_input_port("configuration"));
assert!(node.has_input_port("in"));
assert!(node.has_input_port("format"));
assert!(node.has_output_port("out"));
assert!(node.has_output_port("error"));
}
#[tokio::test]
async fn test_parse_time_node_rfc3339() {
let node = ParseTimeNode::new("test_parse_time".to_string());
let (_config_tx, input_tx, _format_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let time_str = "2024-01-15T09:50:45.123Z".to_string();
let expected_timestamp = 1705312245123i64;
let _ = input_tx
.send(Arc::new(time_str) as Arc<dyn Any + Send + Sync>)
.await;
drop(input_tx);
let out_stream = outputs.remove("out").unwrap();
let mut results = Vec::new();
let mut stream = out_stream;
while let Some(result) = stream.next().await {
if let Ok(arc_timestamp) = result.downcast::<i64>() {
results.push(*arc_timestamp);
}
}
assert_eq!(results.len(), 1);
assert!((results[0] - expected_timestamp).abs() < 1000);
}
#[tokio::test]
async fn test_parse_time_node_custom_format() {
let node = ParseTimeNode::new("test_parse_time".to_string());
let (_config_tx, input_tx, format_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let time_str = "2024-01-15 09:50:45".to_string();
let custom_format = "%Y-%m-%d %H:%M:%S".to_string();
let _ = format_tx
.send(Arc::new(custom_format) as Arc<dyn Any + Send + Sync>)
.await;
let _ = input_tx
.send(Arc::new(time_str) as Arc<dyn Any + Send + Sync>)
.await;
drop(input_tx);
drop(format_tx);
let out_stream = outputs.remove("out").unwrap();
let mut results = Vec::new();
let mut stream = out_stream;
while let Some(result) = stream.next().await {
if let Ok(arc_timestamp) = result.downcast::<i64>() {
results.push(*arc_timestamp);
}
}
assert_eq!(results.len(), 1);
let expected_timestamp = 1705312245000i64; assert!((results[0] - expected_timestamp).abs() < 1000);
}
#[tokio::test]
async fn test_parse_time_node_multiple_formats() {
let node = ParseTimeNode::new("test_parse_time".to_string());
let (_config_tx, input_tx, format_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let time_str = "2024-01-15".to_string();
let format_str = "%Y-%m-%d".to_string();
let _ = format_tx
.send(Arc::new(format_str) as Arc<dyn Any + Send + Sync>)
.await;
let _ = input_tx
.send(Arc::new(time_str) as Arc<dyn Any + Send + Sync>)
.await;
drop(input_tx);
drop(format_tx);
let out_stream = outputs.remove("out").unwrap();
let mut results = Vec::new();
let mut stream = out_stream;
while let Some(result) = stream.next().await {
if let Ok(arc_timestamp) = result.downcast::<i64>() {
results.push(*arc_timestamp);
}
}
assert_eq!(results.len(), 1);
assert!(results[0] > 1700000000000i64); }
#[tokio::test]
async fn test_parse_time_node_error_handling() {
let node = ParseTimeNode::new("test_parse_time".to_string());
let (_config_tx, input_tx, _format_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let invalid_inputs = vec![
Arc::new("not a timestamp".to_string()) as Arc<dyn Any + Send + Sync>,
Arc::new("invalid-date-format".to_string()) as Arc<dyn Any + Send + Sync>,
Arc::new(12345i64) as Arc<dyn Any + Send + Sync>, ];
for invalid_input in invalid_inputs {
let _ = input_tx.send(invalid_input).await;
}
drop(input_tx);
let out_stream = outputs.remove("out").unwrap();
let error_stream = outputs.remove("error").unwrap();
let mut out_results = Vec::new();
let mut error_results = Vec::new();
let mut out_stream = out_stream;
while let Some(result) = out_stream.next().await {
if let Ok(arc_timestamp) = result.downcast::<i64>() {
out_results.push(*arc_timestamp);
}
}
let mut error_stream = error_stream;
while let Some(result) = error_stream.next().await {
if let Ok(error_msg) = result.downcast::<String>() {
error_results.push((*error_msg).clone());
}
}
assert_eq!(out_results.len(), 0); assert_eq!(error_results.len(), 3); for error in &error_results {
assert!(error.contains("Failed to parse") || error.contains("Unsupported type"));
}
}
#[tokio::test]
async fn test_parse_time_node_invalid_format() {
let node = ParseTimeNode::new("test_parse_time".to_string());
let (_config_tx, input_tx, format_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let invalid_format = "%invalid-format".to_string();
let time_str = "2024-01-15T09:50:45Z".to_string();
let _ = format_tx
.send(Arc::new(invalid_format) as Arc<dyn Any + Send + Sync>)
.await;
let _ = input_tx
.send(Arc::new(time_str) as Arc<dyn Any + Send + Sync>)
.await;
drop(input_tx);
drop(format_tx);
let out_stream = outputs.remove("out").unwrap();
let error_stream = outputs.remove("error").unwrap();
let mut out_results = Vec::new();
let mut error_results = Vec::new();
let mut out_stream = out_stream;
while let Some(result) = out_stream.next().await {
if let Ok(arc_timestamp) = result.downcast::<i64>() {
out_results.push(*arc_timestamp);
}
}
let mut error_stream = error_stream;
while let Some(result) = error_stream.next().await {
if let Ok(error_msg) = result.downcast::<String>() {
error_results.push((*error_msg).clone());
}
}
assert_eq!(out_results.len(), 0); assert_eq!(error_results.len(), 1); assert!(error_results[0].contains("Failed to parse"));
}