use std::any::Any;
use std::sync::Arc;
use streamweave::graph;
use streamweave::graph::Graph;
use streamweave::nodes::stream::DropNode;
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (config_tx, config_rx) = mpsc::channel(1);
let (input_tx, input_rx) = mpsc::channel(10);
let (output_tx, mut output_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let (error_tx, mut error_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let mut graph: Graph = graph! {
drop: DropNode::new("drop".to_string()),
graph.configuration => drop.configuration,
graph.input => drop.in,
drop.out => graph.output,
drop.error => graph.error
};
graph.connect_input_channel("configuration", config_rx)?;
graph.connect_input_channel("input", input_rx)?;
graph.connect_output_channel("output", output_tx)?;
graph.connect_output_channel("error", error_tx)?;
println!("✓ Graph built with DropNode using graph! macro");
let _ = config_tx
.send(Arc::new(()) as Arc<dyn Any + Send + Sync>)
.await;
println!("📥 Sending values to drop: 1, 2, 3, 4, 5");
let test_values = vec![1i32, 2i32, 3i32, 4i32, 5i32];
for value in test_values {
input_tx
.send(Arc::new(value) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
}
println!("✓ Configuration and test data sent to input channels");
println!("Executing graph with DropNode...");
let start = std::time::Instant::now();
graph
.execute()
.await
.map_err(|e| format!("Graph execution failed: {:?}", e))?;
println!("✓ Graph execution completed in {:?}", start.elapsed());
drop(config_tx);
drop(input_tx);
println!("Reading results from output channels...");
let mut output_count = 0;
let mut error_count = 0;
loop {
let output_result =
tokio::time::timeout(tokio::time::Duration::from_millis(100), output_rx.recv()).await;
let error_result =
tokio::time::timeout(tokio::time::Duration::from_millis(100), error_rx.recv()).await;
let mut has_data = false;
if let Ok(Some(_item)) = output_result {
output_count += 1;
has_data = true;
println!(" Unexpected output item received (this should not happen)");
}
if let Ok(Some(item)) = error_result
&& let Ok(error_msg) = item.downcast::<String>()
{
let error = (**error_msg).to_string();
println!(" Error: {}", error);
error_count += 1;
has_data = true;
}
if !has_data {
break;
}
}
println!(
"✓ Received {} items via output channel (should be 0)",
output_count
);
println!("✓ Received {} errors via error channel", error_count);
println!("✓ Total completed in {:?}", start.elapsed());
if output_count == 0 && error_count == 0 {
println!("✓ DropNode correctly dropped all input items");
} else {
println!(
"⚠ DropNode behavior may be unexpected (outputs: {}, errors: {}, expected outputs: 0, errors: 0)",
output_count, error_count
);
}
Ok(())
}