test: update tests/examples/benches

This commit is contained in:
TechnoPorg committed 2026-02-17 21:31:18 +01:00
1 parent e4fc0b0d44
commit 72a83e29dc
47 files changed
+491 -1239

No files matched your search

+26 -19
View File
@@ -4,11 +4,11 @@ use dagx::{DagResult, DagRunner};
use dagx_test::task_fn;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
#[tokio::test]
async fn test_retry_pattern_simulation() -> DagResult<()> {
let dag = DagRunner::new();
let mut dag = DagRunner::new();
let attempt_counter = Arc::new(AtomicUsize::new(0));
// Task that would fail on first attempts (simulated)
@@ -29,18 +29,19 @@ async fn test_retry_pattern_simulation() -> DagResult<()> {
}
}));
dag.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
let mut output = dag
.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
.await?;
// Task completes with error on first "attempt"
assert_eq!(dag.get(task)?, Err("Simulated failure"));
assert_eq!(output.get(task)?, Err("Simulated failure"));
Ok(())
}
#[tokio::test]
async fn test_circuit_breaker_pattern() -> DagResult<()> {
let dag = DagRunner::new();
let mut dag = DagRunner::new();
let failure_count = Arc::new(AtomicUsize::new(0));
let circuit_open = Arc::new(AtomicBool::new(false));
@@ -71,12 +72,13 @@ async fn test_circuit_breaker_pattern() -> DagResult<()> {
})
.collect();
dag.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
let mut output = dag
.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
.await?;
// First few tasks fail and open circuit
for task in tasks.into_iter().take(4) {
assert!(dag.get(task)?.is_err());
assert!(output.get(task)?.is_err());
}
Ok(())
@@ -84,8 +86,8 @@ async fn test_circuit_breaker_pattern() -> DagResult<()> {
#[tokio::test]
async fn test_error_accumulation_pattern() -> DagResult<()> {
let dag = DagRunner::new();
let errors = Arc::new(parking_lot::Mutex::new(Vec::new()));
let mut dag = DagRunner::new();
let errors = Arc::new(Mutex::new(Vec::new()));
// Tasks that may produce errors
let mut tasks: Vec<_> = (0..10)
@@ -94,7 +96,10 @@ async fn test_error_accumulation_pattern() -> DagResult<()> {
dag.add_task(task_fn::<(), _, _>(move |_: ()| {
let errors = errors.clone();
if i % 2 == 0 {
errors.lock().push(format!("Error from task {}", i));
errors
.lock()
.unwrap()
.push(format!("Error from task {}", i));
return Err(format!("Task {} error", i));
}
Ok(i)
@@ -102,11 +107,12 @@ async fn test_error_accumulation_pattern() -> DagResult<()> {
})
.collect();
dag.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
let mut output = dag
.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
.await?;
// Check accumulated errors
let error_list = errors.lock().clone();
let error_list = errors.lock().unwrap().clone();
assert_eq!(error_list.len(), 5); // Even numbered tasks
tasks.truncate(2);
@@ -114,15 +120,15 @@ async fn test_error_accumulation_pattern() -> DagResult<()> {
let [first, second] = <[_; 2]>::try_from(tasks).map_err(|_e| ()).unwrap();
// Verify specific results
assert!(dag.get(first)?.is_err());
assert_eq!(dag.get(second)?, Ok(1));
assert!(output.get(first)?.is_err());
assert_eq!(output.get(second)?, Ok(1));
Ok(())
}
#[tokio::test]
async fn test_error_boundary_isolation() -> DagResult<()> {
let dag = DagRunner::new();
let mut dag = DagRunner::new();
// Group A: contains an error - use Result
let a1 = dag.add_task(task_fn::<(), _, _>(|_: ()| 10));
@@ -143,15 +149,16 @@ async fn test_error_boundary_isolation() -> DagResult<()> {
.add_task(task_fn::<i32, _, _>(|&x: &i32| x + 5))
.depends_on(b2);
dag.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
let mut output = dag
.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
.await?;
// Group A fails (returns Err)
assert_eq!(dag.get(a2)?, Err("Group A error"));
assert_eq!(output.get(a2)?, Err("Group A error"));
// Groups B and C succeed
assert_eq!(dag.get(b2)?, 40);
assert_eq!(dag.get(c1)?, 45);
assert_eq!(output.get(b2)?, 40);
assert_eq!(output.get(c1)?, 45);
Ok(())
}