refactor: consolidate some tests/examples/benchmarks

This commit is contained in:
TechnoPorg committed 2026-02-12 23:31:19 +01:00
1 parent 49be497964
commit b8204c4ed9
83 files changed
+569 -10068

No files matched your search

-64
View File
@@ -1,64 +0,0 @@
//! Runtime compatibility tests for async-std
use crate::common::task_fn;
use dagx::DagRunner;
struct Value(i32);
#[dagx::task]
impl Value {
async fn run(&mut self) -> i32 {
self.0
}
}
struct Add;
#[dagx::task]
impl Add {
async fn run(&mut self, a: &i32, b: &i32) -> i32 {
a + b
}
}
#[async_std::test]
async fn test_basic_dag_async_std() {
let dag = DagRunner::new();
let x = dag.add_task(Value(2));
let y = dag.add_task(Value(3));
let sum = dag.add_task(Add).depends_on((x, y));
dag.run(async_std::task::spawn).await.unwrap();
assert_eq!(dag.get(sum).unwrap(), 5);
}
#[async_std::test]
async fn test_parallel_execution_async_std() {
let dag = DagRunner::new();
let tasks: Vec<_> = (0..10)
.map(|i| dag.add_task(task_fn::<(), _, _>(move |_: ()| i * 2)))
.collect();
dag.run(async_std::task::spawn).await.unwrap();
for (i, task) in tasks.into_iter().enumerate() {
assert_eq!(dag.get(task).unwrap(), i * 2);
}
}
#[async_std::test]
async fn test_complex_dependencies_async_std() {
let dag = DagRunner::new();
let a = dag.add_task(Value(10));
let b = dag.add_task(Value(20));
let sum = dag.add_task(Add).depends_on((a, b));
let double = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x * 2))
.depends_on(sum);
dag.run(async_std::task::spawn).await.unwrap();
assert_eq!(dag.get(double).unwrap().clone(), 60);
}
-34
View File
@@ -1,34 +0,0 @@
//! Runtime integration tests - verify dagx works correctly across different runtimes
use dagx::{DagResult, DagRunner};
struct Value(i32);
#[dagx::task]
impl Value {
async fn run(&mut self) -> i32 {
self.0
}
}
struct Add;
#[dagx::task]
impl Add {
async fn run(&mut self, a: &i32, b: &i32) -> i32 {
a + b
}
}
#[tokio::test]
async fn runtime_integration_tokio() -> DagResult<()> {
let dag = DagRunner::new();
let a = dag.add_task(Value(10));
let b = dag.add_task(Value(20));
let sum = dag.add_task(Add).depends_on((a, b));
dag.run(|fut| async move { tokio::spawn(fut).await.unwrap() })
.await?;
assert_eq!(dag.get(sum)?, 30);
Ok(())
}
+145 -4
View File
@@ -1,7 +1,148 @@
//! Runtime tests module
pub mod compatibility;
pub mod quirky;
use std::{any::Any, sync::Arc};
mod async_std;
mod smol;
use dagx::{task_fn, DagResult, DagRunner};
use futures_util::{future::BoxFuture, task::SpawnExt, Future};
use test_case::test_case;
type BoxFunc = Box<
dyn Fn(
BoxFuture<'static, DagResult<Arc<dyn Any + Send + Sync>>>,
) -> BoxFuture<'static, DagResult<Arc<dyn Any + Send + Sync>>>,
>;
trait RuntimeTest {
fn run_dagx_test<F>(self, f: impl Fn(BoxFunc) -> F)
where
F: Future<Output = ()>;
}
struct SmolRuntimeTest;
impl RuntimeTest for SmolRuntimeTest {
fn run_dagx_test<F>(self, f: impl Fn(BoxFunc) -> F)
where
F: Future<Output = ()>,
{
smol::block_on(f(Box::new(|fut| Box::pin(smol::spawn(fut)))));
}
}
struct FuturesExecutorRuntimeTest;
impl RuntimeTest for FuturesExecutorRuntimeTest {
fn run_dagx_test<F>(self, f: impl Fn(BoxFunc) -> F)
where
F: Future<Output = ()>,
{
let spawner = futures_executor::ThreadPool::new().unwrap();
futures_executor::block_on(f(Box::new(move |fut| {
Box::pin(spawner.spawn_with_handle(fut).unwrap())
})));
}
}
struct PollsterRuntimeTest;
impl RuntimeTest for PollsterRuntimeTest {
fn run_dagx_test<F>(self, f: impl Fn(BoxFunc) -> F)
where
F: Future<Output = ()>,
{
pollster::block_on(f(Box::new(|fut| {
Box::pin(async { pollster::block_on(fut) })
})));
}
}
static ASYNC_EXECUTOR: async_executor::Executor = async_executor::Executor::new();
struct AsyncExecutorRuntimeTest;
impl RuntimeTest for AsyncExecutorRuntimeTest {
fn run_dagx_test<F>(self, f: impl Fn(BoxFunc) -> F)
where
F: Future<Output = ()>,
{
smol::future::block_on(
ASYNC_EXECUTOR.run(f(Box::new(|fut| Box::pin(ASYNC_EXECUTOR.spawn(fut))))),
);
}
}
struct Value(i32);
#[dagx::task]
impl Value {
async fn run(&mut self) -> i32 {
self.0
}
}
struct Add;
#[dagx::task]
impl Add {
async fn run(&mut self, a: &i32, b: &i32) -> i32 {
a + b
}
}
#[test_case(SmolRuntimeTest)]
#[test_case(FuturesExecutorRuntimeTest)]
#[test_case(PollsterRuntimeTest)]
#[test_case(AsyncExecutorRuntimeTest)]
fn test_basic_dag(runner: impl RuntimeTest) {
runner.run_dagx_test(|spawner| async {
let dag = DagRunner::new();
let x = dag.add_task(Value(2));
let y = dag.add_task(Value(3));
let sum = dag.add_task(Add).depends_on((x, y));
dag.run(spawner).await.unwrap();
assert_eq!(dag.get(sum).unwrap(), 5);
})
}
#[test_case(SmolRuntimeTest)]
#[test_case(FuturesExecutorRuntimeTest)]
#[test_case(PollsterRuntimeTest)]
#[test_case(AsyncExecutorRuntimeTest)]
fn test_parallel_execution(runner: impl RuntimeTest) {
runner.run_dagx_test(|spawner| async {
let dag = DagRunner::new();
let tasks: Vec<_> = (0..10)
.map(|i| dag.add_task(task_fn::<(), _, _>(move |_: ()| i * 2)))
.collect();
dag.run(spawner).await.unwrap();
for (i, task) in tasks.into_iter().enumerate() {
assert_eq!(dag.get(task).unwrap(), i * 2);
}
});
}
#[test_case(SmolRuntimeTest)]
#[test_case(FuturesExecutorRuntimeTest)]
#[test_case(PollsterRuntimeTest)]
#[test_case(AsyncExecutorRuntimeTest)]
fn test_complex_dependencies(runner: impl RuntimeTest) {
runner.run_dagx_test(|spawner| async {
let dag = DagRunner::new();
let a = dag.add_task(Value(10));
let b = dag.add_task(Value(20));
let sum = dag.add_task(Add).depends_on((a, b));
let double = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x * 2))
.depends_on(sum);
dag.run(spawner).await.unwrap();
assert_eq!(dag.get(double).unwrap().clone(), 60);
});
}
-493
View File
@@ -1,493 +0,0 @@
//! Tests for quirky/edge-case async runtimes
//!
//! This file tests dagx with unusual async runtimes to ensure
//! the spawner parameter works correctly across different executor types.
//!
//! Tested runtimes:
//! - async-executor: Minimal executor from smol ecosystem
//! - pollster: Blocking/synchronous executor
//! - futures-executor: Basic futures crate executor
// Note: executor.run() synchronously executes futures to completion.
// We don't need the return value in these tests, so we allow unused results.
#![allow(clippy::let_underscore_future)]
#![allow(unused_must_use)]
use crate::common::task_fn;
use dagx::{DagRunner, TaskHandle};
use futures_executor::ThreadPool;
use futures_util::task::SpawnExt;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
// =======================
// async-executor Tests
// =======================
#[test]
fn test_async_executor_basic() {
// async-executor is a minimal executor from the smol ecosystem
let executor = async_executor::Executor::new();
let future = async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 10));
let y = dag.add_task(task_fn::<(), _, _>(|_: ()| 20));
let sum = dag
.add_task(task_fn::<(i32, i32), _, _>(|(a, b): (&i32, &i32)| a + b))
.depends_on((x, y));
dag.run(|fut| executor.spawn(fut)).await.unwrap();
assert_eq!(dag.get(sum).unwrap(), 30);
};
executor.run(future);
}
#[test]
fn test_async_executor_complex_dag() {
let executor = async_executor::Executor::new();
let future = async {
let dag = DagRunner::new();
// Create a diamond pattern
let source: TaskHandle<_> = dag.add_task(task_fn::<(), _, _>(|_: ()| 5)).into();
let left = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x * 2))
.depends_on(source);
let right = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x * 3))
.depends_on(source);
let sink = dag
.add_task(task_fn::<(i32, i32), _, _>(|(l, r): (&i32, &i32)| l + r))
.depends_on((&left, &right));
dag.run(|fut| executor.spawn(fut)).await.unwrap();
assert_eq!(dag.get(sink).unwrap(), 25); // (5*2) + (5*3) = 25
};
executor.run(future);
}
#[test]
fn test_async_executor_parallel_tasks() {
let executor = async_executor::Executor::new();
let future = async {
let dag = DagRunner::new();
let counter = Arc::new(AtomicUsize::new(0));
// Create 10 independent tasks
let tasks: Vec<_> = (0..10)
.map(|i| {
let c = counter.clone();
dag.add_task(task_fn::<(), _, _>(move |_: ()| {
let c = c.clone();
c.fetch_add(1, Ordering::SeqCst);
i
}))
})
.collect();
dag.run(|fut| executor.spawn(fut)).await.unwrap();
// All tasks should have run
assert_eq!(counter.load(Ordering::SeqCst), 10);
// Verify results
for (i, task) in tasks.into_iter().enumerate() {
assert_eq!(dag.get(task).unwrap(), i as i32);
}
};
executor.run(future);
}
#[test]
fn test_async_executor_with_local_spawn() {
// Test with LocalExecutor (single-threaded)
let executor = async_executor::LocalExecutor::new();
let future = async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 42));
dag.run(|fut| executor.spawn(fut)).await.unwrap();
assert_eq!(dag.get(x).unwrap(), 42);
};
executor.run(future);
}
#[test]
fn test_async_executor_deep_chain() {
let executor = async_executor::Executor::new();
let future = async {
let dag = DagRunner::new();
// Build a chain of 50 tasks
let start = dag.add_task(task_fn::<(), _, _>(|_: ()| 0));
let mut current = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x + 1))
.depends_on(start);
for _ in 1..50 {
current = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x + 1))
.depends_on(current);
}
dag.run(|fut| executor.spawn(fut)).await.unwrap();
assert_eq!(dag.get(current).unwrap(), 50);
};
executor.run(future);
}
// =======================
// pollster Tests
// =======================
#[test]
fn test_pollster_basic() {
// pollster is a blocking executor - it blocks the thread
pollster::block_on(async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 100));
let doubled = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x * 2))
.depends_on(x);
dag.run(|fut| async { pollster::block_on(fut) })
.await
.unwrap();
assert_eq!(dag.get(doubled).unwrap(), 200);
});
}
#[test]
fn test_pollster_complex() {
pollster::block_on(async {
let dag = DagRunner::new();
// Fan-out pattern
let source: TaskHandle<_> = dag.add_task(task_fn::<(), _, _>(|_: ()| 7)).into();
let tasks: Vec<_> = (0..5)
.map(|i| {
dag.add_task(task_fn::<i32, _, _>(move |&x: &i32| x * i))
.depends_on(source)
})
.collect();
dag.run(|fut| async { pollster::block_on(fut) })
.await
.unwrap();
for (i, task) in tasks.iter().enumerate() {
assert_eq!(dag.get(task).unwrap(), 7 * i as i32);
}
});
}
#[test]
fn test_pollster_error_handling() {
pollster::block_on(async {
let dag = DagRunner::new();
// Task that returns Result
let task = dag.add_task(task_fn::<(), _, _>(|_: ()| Ok::<i32, String>(42)));
dag.run(|fut| async { pollster::block_on(fut) })
.await
.unwrap();
assert_eq!(dag.get(task).unwrap(), Ok(42));
});
}
// =======================
// futures-executor Tests
// =======================
#[test]
fn test_futures_executor_basic() {
futures_executor::block_on(async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 15));
let y = dag.add_task(task_fn::<(), _, _>(|_: ()| 25));
let product = dag
.add_task(task_fn::<(i32, i32), _, _>(|(a, b): (&i32, &i32)| a * b))
.depends_on((x, y));
let spawner = ThreadPool::new().unwrap();
dag.run(|fut| spawner.spawn_with_handle(fut).unwrap())
.await
.unwrap();
assert_eq!(dag.get(product).unwrap(), 375); // 15 * 25
});
}
#[test]
fn test_futures_executor_threadpool() {
let pool = futures_executor::ThreadPool::new().unwrap();
let spawner = pool.clone();
pool.spawn_ok(async move {
let dag = DagRunner::new();
// Create multiple independent tasks
let tasks: Vec<_> = (0..20)
.map(|i| dag.add_task(task_fn::<(), _, _>(move |_: ()| i * i)))
.collect();
dag.run(|fut| spawner.spawn_with_handle(fut).unwrap())
.await
.unwrap();
for (i, task) in tasks.into_iter().enumerate() {
assert_eq!(dag.get(task).unwrap(), (i * i) as i32);
}
});
// Give tasks time to complete
std::thread::sleep(std::time::Duration::from_millis(100));
}
// Note: LocalPool test removed because it cannot run within another executor context
// (cargo test framework may use tokio, causing EnterError)
// LocalPool is single-threaded anyway, so futures-executor ThreadPool is more representative
#[test]
fn test_futures_executor_diamond_pattern() {
futures_executor::block_on(async {
let dag = DagRunner::new();
let a: TaskHandle<_> = dag.add_task(task_fn::<(), _, _>(|_: ()| 8)).into();
let b = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x + 2))
.depends_on(a);
let c = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x - 2))
.depends_on(a);
let d = dag
.add_task(task_fn::<(i32, i32), _, _>(|(x, y): (&i32, &i32)| x * y))
.depends_on((&b, &c));
let spawner = ThreadPool::new().unwrap();
dag.run(|fut| spawner.spawn_with_handle(fut).unwrap())
.await
.unwrap();
assert_eq!(dag.get(d).unwrap(), 60); // (8+2) * (8-2) = 10 * 6
});
}
// =======================
// Cross-Runtime Tests
// =======================
#[test]
fn test_all_runtimes_compatibility() {
// This test verifies the same DAG works across all runtimes
// Test 1: async-executor
{
let executor = async_executor::Executor::new();
executor.run(async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 123));
dag.run(|fut| executor.spawn(fut)).await.unwrap();
assert_eq!(dag.get(x).unwrap(), 123);
});
}
// Test 2: pollster
{
pollster::block_on(async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 123));
dag.run(|fut| async { pollster::block_on(fut) })
.await
.unwrap();
assert_eq!(dag.get(x).unwrap(), 123);
});
}
// Test 3: futures-executor
{
futures_executor::block_on(async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 123));
let spawner = ThreadPool::new().unwrap();
dag.run(|fut| spawner.spawn_with_handle(fut).unwrap())
.await
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(10));
assert_eq!(dag.get(x).unwrap(), 123);
});
}
}
#[test]
fn test_runtime_edge_cases() {
// Test with LocalExecutor (no Send requirement)
let executor = async_executor::LocalExecutor::new();
executor.run(async {
let dag = DagRunner::new();
// This tests that our API works even with !Send futures
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| {
// Local state
42
}));
dag.run(|fut| executor.spawn(fut)).await.unwrap();
assert_eq!(dag.get(x).unwrap(), 42);
});
}
// =======================
// Stress Tests for Quirky Runtimes
// =======================
#[test]
fn test_async_executor_stress() {
let executor = async_executor::Executor::new();
executor.run(async {
let dag = DagRunner::new();
// Create 100 tasks
let tasks: Vec<_> = (0..100)
.map(|i| {
dag.add_task(task_fn::<(), _, _>(move |_: ()| {
// Small delay to test executor scheduling
i
}))
})
.collect();
dag.run(|fut| executor.spawn(fut)).await.unwrap();
for (i, task) in tasks.into_iter().enumerate() {
assert_eq!(dag.get(task).unwrap(), i as i32);
}
});
}
#[test]
fn test_pollster_nested_blocks() {
// Test nested blocking - edge case
pollster::block_on(async {
let dag1 = DagRunner::new();
let x = dag1.add_task(task_fn::<(), _, _>(|_: ()| 10));
dag1.run(|fut| async { pollster::block_on(fut) })
.await
.unwrap();
assert_eq!(dag1.get(x).unwrap(), 10);
});
}
#[test]
fn test_futures_executor_multi_pool() {
// Test with multiple thread pools
let pool1 = futures_executor::ThreadPool::new().unwrap();
let spawner = pool1.clone();
pool1.spawn_ok(async move {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 50));
dag.run(|fut| spawner.spawn_with_handle(fut).unwrap())
.await
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(50));
assert_eq!(dag.get(x).unwrap(), 50);
});
std::thread::sleep(std::time::Duration::from_millis(100));
}
// =======================
// Edge Case: Sync Tasks with Quirky Runtimes
// =======================
#[test]
fn test_sync_tasks_on_async_executor() {
struct SyncAdd;
#[dagx::task]
impl SyncAdd {
fn run(a: &i32, b: &i32) -> i32 {
// Synchronous task
a + b
}
}
let executor = async_executor::Executor::new();
executor.run(async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 5));
let y = dag.add_task(task_fn::<(), _, _>(|_: ()| 7));
let sum = dag.add_task(SyncAdd).depends_on((x, y));
dag.run(|fut| executor.spawn(fut)).await.unwrap();
assert_eq!(dag.get(sum).unwrap(), 12);
});
}
#[test]
fn test_sync_tasks_on_pollster() {
struct SyncDouble;
#[dagx::task]
impl SyncDouble {
fn run(x: &i32) -> i32 {
x * 2
}
}
pollster::block_on(async {
let dag = DagRunner::new();
let x = dag.add_task(task_fn::<(), _, _>(|_: ()| 21));
let doubled = dag.add_task(SyncDouble).depends_on(x);
dag.run(|fut| async { pollster::block_on(fut) })
.await
.unwrap();
assert_eq!(dag.get(doubled).unwrap(), 42);
});
}
-70
View File
@@ -1,70 +0,0 @@
//! Runtime compatibility tests for smol
use crate::common::task_fn;
use dagx::DagRunner;
struct Value(i32);
#[dagx::task]
impl Value {
async fn run(&mut self) -> i32 {
self.0
}
}
struct Add;
#[dagx::task]
impl Add {
async fn run(&mut self, a: &i32, b: &i32) -> i32 {
a + b
}
}
#[test]
fn test_basic_dag_smol() {
smol::block_on(async {
let dag = DagRunner::new();
let x = dag.add_task(Value(2));
let y = dag.add_task(Value(3));
let sum = dag.add_task(Add).depends_on((x, y));
dag.run(smol::spawn).await.unwrap();
assert_eq!(dag.get(sum).unwrap(), 5);
});
}
#[test]
fn test_parallel_execution_smol() {
smol::block_on(async {
let dag = DagRunner::new();
let tasks: Vec<_> = (0..10)
.map(|i| dag.add_task(task_fn::<(), _, _>(move |_: ()| i * 2)))
.collect();
dag.run(smol::spawn).await.unwrap();
for (i, task) in tasks.into_iter().enumerate() {
assert_eq!(dag.get(task).unwrap(), i * 2);
}
});
}
#[test]
fn test_complex_dependencies_smol() {
smol::block_on(async {
let dag = DagRunner::new();
let a = dag.add_task(Value(10));
let b = dag.add_task(Value(20));
let sum = dag.add_task(Add).depends_on((a, b));
let double = dag
.add_task(task_fn::<i32, _, _>(|&x: &i32| x * 2))
.depends_on(sum);
dag.run(smol::spawn).await.unwrap();
assert_eq!(dag.get(double).unwrap().clone(), 60);
});
}