Update tests, examples, benches

This commit is contained in:
TechnoPorg committed 2026-02-11 22:01:50 +01:00
1 parent 0137296fb4
commit 86787b3a0d
71 files changed
+1791 -3547

No files matched your search

+84 -43
View File
@@ -1,6 +1,7 @@
//! Unit tests for task module
use crate::task::{task_fn, Task};
use crate::task::{task_fn, Task, TaskInput};
use std::any::Any;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
@@ -11,50 +12,74 @@ async fn test_task_fn_creation_and_execution() {
// Test line 111 in task.rs - Task implementation for TaskFn
// Create a simple task function
let task = task_fn(|input: i32| async move { input * 2 });
let task = task_fn::<i32, _, _>(|input: &i32| input * 2);
// Test the Task trait implementation
let result = task.run(42).await;
let result = task
.run(TaskInput::new(
vec![Arc::new(42) as Arc<dyn Any + Send + Sync + 'static>].iter(),
))
.await;
assert_eq!(result, 84);
}
#[tokio::test]
async fn test_task_fn_with_unit_input() {
// Test task_fn with unit input
let task = task_fn(|_: ()| async { 100 });
let task = task_fn::<(), _, _>(|_: ()| 100);
let result = task.run(()).await;
let result = task
.run(TaskInput::new(
vec![Arc::new(()) as Arc<dyn Any + Send + Sync + 'static>].iter(),
))
.await;
assert_eq!(result, 100);
}
#[tokio::test]
async fn test_task_fn_with_tuple_input() {
// Test task_fn with tuple input
let task = task_fn(|(a, b): (i32, i32)| async move { a + b });
let task = task_fn::<(i32, i32), _, _>(|(a, b): (&i32, &i32)| a + b);
let result = task.run((10, 20)).await;
let result = task
.run(TaskInput::new(
vec![
Arc::new(10) as Arc<dyn Any + Send + Sync + 'static>,
Arc::new(20) as Arc<dyn Any + Send + Sync + 'static>,
]
.iter(),
))
.await;
assert_eq!(result, 30);
}
#[tokio::test]
async fn test_task_fn_with_string_input() {
// Test task_fn with String input
let task = task_fn(|input: String| {
let task = task_fn::<String, _, _>(|input: &String| {
let len = input.len();
async move { len }
len
});
let test_string = String::from("hello");
let result = task.run(test_string).await;
let result = task
.run(TaskInput::new(
vec![Arc::new(test_string) as Arc<dyn Any + Send + Sync + 'static>].iter(),
))
.await;
assert_eq!(result, 5);
}
#[tokio::test]
async fn test_task_fn_with_complex_output() {
// Test task_fn with complex output type
let task = task_fn(|input: i32| async move { vec![input, input * 2, input * 3] });
let task = task_fn::<i32, _, _>(|&input: &i32| vec![input, input * 2, input * 3]);
let result = task.run(5).await;
let result = task
.run(TaskInput::new(
vec![Arc::new(5) as Arc<dyn Any + Send + Sync + 'static>].iter(),
))
.await;
assert_eq!(result, vec![5, 10, 15]);
}
@@ -62,9 +87,13 @@ async fn test_task_fn_with_complex_output() {
async fn test_task_fn_stateful_closure() {
// Test that task_fn can capture state
let multiplier = 3;
let task = task_fn(move |input: i32| async move { input * multiplier });
let task = task_fn::<i32, _, _>(move |input: &i32| input * multiplier);
let result = task.run(7).await;
let result = task
.run(TaskInput::new(
vec![Arc::new(7) as Arc<dyn Any + Send + Sync + 'static>].iter(),
))
.await;
assert_eq!(result, 21);
}
@@ -74,34 +103,30 @@ struct CustomTask {
}
impl Task for CustomTask {
type Input = i32;
type Input = (i32,);
type Output = i32;
#[allow(refining_impl_trait)]
fn run(mut self, input: Self::Input) -> Pin<Box<dyn Future<Output = Self::Output> + Send>> {
fn run(
mut self,
input: TaskInput<'_, Self::Input>,
) -> Pin<Box<dyn Future<Output = Self::Output> + Send + '_>> {
Box::pin(async move {
self.state += input;
self.state += input.next().0;
self.state
})
}
#[allow(refining_impl_trait)]
fn extract_and_run(
self,
_dependencies: Vec<Arc<dyn std::any::Any + Send + Sync>>,
) -> Pin<Box<dyn Future<Output = Result<Self::Output, String>> + Send>> {
Box::pin(async move {
// This is a test helper - just return an error
Err("extract_and_run not implemented for CustomTask (test helper)".to_string())
})
}
}
#[tokio::test]
async fn test_custom_task_implementation() {
let task = CustomTask { state: 10 };
let result1 = task.run(5).await;
let result1 = task
.run(TaskInput::new(
vec![Arc::new(5) as Arc<dyn Any + Send + Sync + 'static>].iter(),
))
.await;
assert_eq!(result1, 15);
// Can't call run again since task was consumed
@@ -131,15 +156,21 @@ fn test_task_trait_bounds() {
#[tokio::test]
async fn test_task_fn_single_call() {
// Test that task_fn works (can only call once due to ownership)
let task = task_fn(|x: i32| async move { x + 1 });
let task = task_fn::<i32, _, _>(|x: &i32| x + 1);
assert_eq!(task.run(1).await, 2);
assert_eq!(
task.run(TaskInput::new(
vec![Arc::new(1) as Arc<dyn Any + Send + Sync + 'static>].iter()
))
.await,
2
);
}
#[tokio::test]
async fn test_task_fn_with_result_type() {
// Test task_fn returning Result
let task = task_fn(|x: i32| async move {
let task = task_fn::<i32, _, _>(|&x: &i32| {
if x > 0 {
Ok(x * 2)
} else {
@@ -147,30 +178,40 @@ async fn test_task_fn_with_result_type() {
}
});
assert_eq!(task.run(5).await, Ok(10));
assert_eq!(
task.run(TaskInput::new(
vec![Arc::new(5) as Arc<dyn Any + Send + Sync + 'static>].iter()
))
.await,
Ok(10)
);
}
#[tokio::test]
async fn test_task_fn_with_option_type() {
// Test task_fn returning Option
let task = task_fn(|x: i32| async move {
if x > 0 {
Some(x * 2)
} else {
None
}
});
let task = task_fn::<i32, _, _>(|&x: &i32| if x > 0 { Some(x * 2) } else { None });
assert_eq!(task.run(5).await, Some(10));
assert_eq!(
task.run(TaskInput::new(
vec![Arc::new(5) as Arc<dyn Any + Send + Sync + 'static>].iter()
))
.await,
Some(10)
);
}
#[tokio::test]
async fn test_task_fn_explicit_trait_call() {
// Test explicit Task trait method call to cover line 121
let task = task_fn(|x: i32| async move { x * 3 });
let task = task_fn::<i32, _, _>(|x: &i32| x * 3);
// Explicitly call the Task trait's run method
let result = Task::run(task, 10).await;
let result = Task::run(
task,
TaskInput::new(vec![Arc::new(10) as Arc<dyn Any + Send + Sync + 'static>].iter()),
)
.await;
assert_eq!(result, 30);
}