Update tests, examples, benches
This commit is contained in:
1 parent
0137296fb4
commit
86787b3a0d
71 files changed
+1791
-3547
No files matched your search
@@ -5,9 +5,6 @@ use dagx::{DagResult, DagRunner};
|
||||
use futures::FutureExt;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_spawner_actually_spawns_tasks() -> DagResult<()> {
|
||||
@@ -18,14 +15,12 @@ async fn test_spawner_actually_spawns_tasks() -> DagResult<()> {
|
||||
// Create 10 independent tasks
|
||||
let tasks: Vec<_> = (0..10)
|
||||
.map(|i| {
|
||||
dag.add_task(task_fn({
|
||||
dag.add_task(task_fn::<(), _, _>({
|
||||
let value = spawn_count.clone();
|
||||
move |_: ()| {
|
||||
let value = value.clone();
|
||||
async move {
|
||||
value.fetch_add(1, Ordering::SeqCst);
|
||||
i
|
||||
}
|
||||
value.fetch_add(1, Ordering::SeqCst);
|
||||
i
|
||||
}
|
||||
}))
|
||||
})
|
||||
@@ -49,57 +44,3 @@ async fn test_spawner_actually_spawns_tasks() -> DagResult<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tasks_not_running_inline() -> DagResult<()> {
|
||||
// Verify that tasks are spawned and CAN run on different threads than the caller
|
||||
// Note: Small/fast tasks may run on the same thread if the scheduler chooses
|
||||
use std::thread;
|
||||
|
||||
let dag = DagRunner::new();
|
||||
let calling_thread = thread::current().id();
|
||||
let task_threads = Arc::new(Mutex::new(Vec::new()));
|
||||
|
||||
// Create tasks with some work to encourage distribution
|
||||
let tasks: Vec<_> = (0..20)
|
||||
.map(|i| {
|
||||
let threads = task_threads.clone();
|
||||
dag.add_task(task_fn(move |_: ()| {
|
||||
let threads = threads.clone();
|
||||
async move {
|
||||
threads.lock().unwrap().push(thread::current().id());
|
||||
|
||||
// Add some async work
|
||||
sleep(Duration::from_millis(2)).await;
|
||||
|
||||
i
|
||||
}
|
||||
}))
|
||||
})
|
||||
.collect();
|
||||
|
||||
dag.run(|fut| tokio::spawn(fut).map(Result::unwrap)).await?;
|
||||
|
||||
// Verify results
|
||||
for (i, task) in tasks.into_iter().enumerate() {
|
||||
assert_eq!(dag.get(task)?, i as i32);
|
||||
}
|
||||
|
||||
// Check if tasks ran on different threads than the caller
|
||||
// This is a capability test - the scheduler MAY choose to use the same thread
|
||||
let task_thread_ids = task_threads.lock().unwrap();
|
||||
let different_threads = task_thread_ids
|
||||
.iter()
|
||||
.filter(|&&tid| tid != calling_thread)
|
||||
.count();
|
||||
|
||||
if different_threads == 0 {
|
||||
eprintln!(
|
||||
"Warning: All tasks ran on the calling thread. This may indicate tasks aren't \
|
||||
being spawned, or the scheduler chose not to distribute (valid for very fast tasks)"
|
||||
);
|
||||
}
|
||||
|
||||
// The key thing is that tasks completed successfully via spawner
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user