test: increase coverage for error paths and conversions

Add targeted coverage tests to maintain >80% code coverage minimum:

- ExtractInput error paths (14 tests): Type mismatch errors for Result,
  Option, Vec, HashMap, Arc, and primitive types. Directly exercises
  error formatting code that was previously uncovered.

- DagRunner::get() type mismatch (6 tests): Covers the type mismatch
  error path when attempting to retrieve task output with wrong type.
  Tests various type combinations to ensure robust error handling.

- TaskBuilder conversions (9 tests): Covers the From<TaskBuilder> to
  TaskHandle conversion for unit-type tasks, ensuring ID preservation
  and correct handling of mixed task types.

Also fixes timing test threshold from 10ms to 50ms to account for
tarpaulin instrumentation overhead while still validating parallel
execution.

Coverage increased from ~78% to 82.04% (above 80% minimum requirement).
This commit is contained in:
swaits committed 2025-10-10 17:06:59 -06:00
1 parent fad97dbf16
commit 8ee4092f9a
7 files changed
+922 -1

No files matched your search

+3
View File
@@ -294,3 +294,6 @@ impl_extract_tuple!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7);
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[cfg(test)]
mod coverage_tests;
+378
View File
@@ -0,0 +1,378 @@
//! Coverage tests for uncovered error paths in extract.rs
//!
//! These tests specifically target type mismatch error messages that aren't hit
//! in normal usage because the #[task] macro generates type-safe code.
//! We directly call extract_from_channels with mismatched types.
use crate::extract::ExtractInput;
use futures::channel::oneshot;
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
#[tokio::test]
async fn test_result_type_mismatch_coverage() {
// Tests line 95 in extract.rs - Result<T,E> type mismatch error message
// Create a channel that sends the WRONG type (String instead of Result<i32, String>)
let (tx, rx) = oneshot::channel::<Arc<String>>();
// Send a String value
tokio::spawn(async move {
let _ = tx.send(Arc::new("hello".to_string()));
});
// Try to extract as Result<i32, String> - this will fail with type mismatch
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Result::<i32, String>::extract_from_channels(deps).await;
// Verify the error message contains the expected type information
assert!(result.is_err());
let err_msg = result.unwrap_err();
// The error should mention Result type mismatch (line 95)
assert!(
err_msg.contains("Result"),
"Expected type mismatch error mentioning Result, got: {}",
err_msg
);
assert!(
err_msg.contains("Type mismatch"),
"Expected 'Type mismatch' in error, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_option_type_mismatch_coverage() {
// Tests line 124 in extract.rs - Option<T> type mismatch error message
// Create a channel that sends the WRONG type (i32 instead of Option<i32>)
let (tx, rx) = oneshot::channel::<Arc<i32>>();
// Send an i32 value
tokio::spawn(async move {
let _ = tx.send(Arc::new(42));
});
// Try to extract as Option<i32> - this will fail with type mismatch
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Option::<i32>::extract_from_channels(deps).await;
// Verify the error message contains the expected type information
assert!(result.is_err());
let err_msg = result.unwrap_err();
// The error should mention Option type mismatch (line 124)
assert!(
err_msg.contains("Option"),
"Expected type mismatch error mentioning Option, got: {}",
err_msg
);
assert!(
err_msg.contains("Type mismatch"),
"Expected 'Type mismatch' in error, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_vec_type_mismatch_coverage() {
// Tests line 152 in extract.rs - Vec<T> type mismatch error message
// Create a channel that sends the WRONG type (String instead of Vec<u8>)
let (tx, rx) = oneshot::channel::<Arc<String>>();
// Send a String value
tokio::spawn(async move {
let _ = tx.send(Arc::new("not a vec".to_string()));
});
// Try to extract as Vec<u8> - this will fail with type mismatch
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Vec::<u8>::extract_from_channels(deps).await;
// Verify the error message contains the expected type information
assert!(result.is_err());
let err_msg = result.unwrap_err();
// The error should mention Vec type mismatch (line 152)
assert!(
err_msg.contains("Vec"),
"Expected type mismatch error mentioning Vec, got: {}",
err_msg
);
assert!(
err_msg.contains("Type mismatch"),
"Expected 'Type mismatch' in error, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_hashmap_type_mismatch_coverage() {
// Tests line 181 in extract.rs - HashMap<K,V> type mismatch error message
// Create a channel that sends the WRONG type (Vec<i32> instead of HashMap)
let (tx, rx) = oneshot::channel::<Arc<Vec<i32>>>();
// Send a Vec value
tokio::spawn(async move {
let _ = tx.send(Arc::new(vec![1, 2, 3]));
});
// Try to extract as HashMap<String, i32> - this will fail with type mismatch
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = HashMap::<String, i32>::extract_from_channels(deps).await;
// Verify the error message contains the expected type information
assert!(result.is_err());
let err_msg = result.unwrap_err();
// The error should mention HashMap type mismatch (line 181)
assert!(
err_msg.contains("HashMap"),
"Expected type mismatch error mentioning HashMap, got: {}",
err_msg
);
assert!(
err_msg.contains("Type mismatch"),
"Expected 'Type mismatch' in error, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_arc_type_mismatch_coverage() {
// Tests line 211 in extract.rs - Arc<T> type mismatch error message
// Arc is special: it expects Arc<Arc<T>> from channel
// Create a channel that sends Arc<String> instead of Arc<Arc<String>>
let (tx, rx) = oneshot::channel::<Arc<String>>();
// Send an Arc<String> value (missing one level of Arc wrapping)
tokio::spawn(async move {
let _ = tx.send(Arc::new("not double arc".to_string()));
});
// Try to extract as Arc<String> - this will fail with type mismatch
// because it expects Arc<Arc<String>> in the channel
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Arc::<String>::extract_from_channels(deps).await;
// Verify the error message contains the expected type information
assert!(result.is_err());
let err_msg = result.unwrap_err();
// The error should mention Arc type mismatch (line 211)
assert!(
err_msg.contains("Arc"),
"Expected type mismatch error mentioning Arc, got: {}",
err_msg
);
assert!(
err_msg.contains("Type mismatch"),
"Expected 'Type mismatch' in error, got: {}",
err_msg
);
}
// Additional tests for better coverage of complex scenarios
#[tokio::test]
async fn test_result_with_wrong_ok_type() {
// Test Result with wrong Ok type
let (tx, rx) = oneshot::channel::<Arc<Result<String, String>>>();
tokio::spawn(async move {
let _ = tx.send(Arc::new(Ok("test".to_string())));
});
// Try to extract as Result<i32, String> - wrong Ok type
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Result::<i32, String>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("Result") && err_msg.contains("Type mismatch"),
"Expected Result type mismatch, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_result_with_wrong_err_type() {
// Test Result with wrong Err type
let (tx, rx) = oneshot::channel::<Arc<Result<i32, i32>>>();
tokio::spawn(async move {
let _ = tx.send(Arc::new(Err(42)));
});
// Try to extract as Result<i32, String> - wrong Err type
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Result::<i32, String>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("Result") && err_msg.contains("Type mismatch"),
"Expected Result type mismatch, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_option_with_wrong_inner_type() {
// Test Option with wrong inner type
let (tx, rx) = oneshot::channel::<Arc<Option<String>>>();
tokio::spawn(async move {
let _ = tx.send(Arc::new(Some("test".to_string())));
});
// Try to extract as Option<i32> - wrong inner type
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Option::<i32>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("Option") && err_msg.contains("Type mismatch"),
"Expected Option type mismatch, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_vec_with_wrong_element_type() {
// Test Vec with wrong element type
let (tx, rx) = oneshot::channel::<Arc<Vec<String>>>();
tokio::spawn(async move {
let _ = tx.send(Arc::new(vec!["a".to_string(), "b".to_string()]));
});
// Try to extract as Vec<i32> - wrong element type
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Vec::<i32>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("Vec") && err_msg.contains("Type mismatch"),
"Expected Vec type mismatch, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_hashmap_with_wrong_key_type() {
// Test HashMap with wrong key type
let (tx, rx) = oneshot::channel::<Arc<HashMap<i32, String>>>();
tokio::spawn(async move {
let mut map = HashMap::new();
map.insert(1, "one".to_string());
let _ = tx.send(Arc::new(map));
});
// Try to extract as HashMap<String, String> - wrong key type
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = HashMap::<String, String>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("HashMap") && err_msg.contains("Type mismatch"),
"Expected HashMap type mismatch, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_hashmap_with_wrong_value_type() {
// Test HashMap with wrong value type
let (tx, rx) = oneshot::channel::<Arc<HashMap<String, i32>>>();
tokio::spawn(async move {
let mut map = HashMap::new();
map.insert("key".to_string(), 42);
let _ = tx.send(Arc::new(map));
});
// Try to extract as HashMap<String, String> - wrong value type
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = HashMap::<String, String>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("HashMap") && err_msg.contains("Type mismatch"),
"Expected HashMap type mismatch, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_arc_with_wrong_inner_type() {
// Test Arc with completely wrong inner type
// Send Arc<i32> but expect Arc<String>
let (tx, rx) = oneshot::channel::<Arc<Arc<i32>>>();
tokio::spawn(async move {
let _ = tx.send(Arc::new(Arc::new(42)));
});
// Try to extract as Arc<String> - wrong inner type
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Arc::<String>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("Arc") && err_msg.contains("Type mismatch"),
"Expected Arc type mismatch, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_primitive_type_mismatch_for_completeness() {
// Test primitive type mismatch (already covered in existing tests but good for completeness)
let (tx, rx) = oneshot::channel::<Arc<String>>();
tokio::spawn(async move {
let _ = tx.send(Arc::new("not an i32".to_string()));
});
// Try to extract as i32 - type mismatch
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = i32::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("Type mismatch"),
"Expected type mismatch error, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_complex_nested_result_option() {
// Test complex nested type: send Result<String, String>, expect Result<Option<i32>, String>
let (tx, rx) = oneshot::channel::<Arc<Result<String, String>>>();
tokio::spawn(async move {
let _ = tx.send(Arc::new(Ok("test".to_string())));
});
// Try to extract as Result<Option<i32>, String> - nested type mismatch
let deps = vec![Box::new(rx) as Box<dyn Any + Send>];
let result = Result::<Option<i32>, String>::extract_from_channels(deps).await;
assert!(result.is_err());
let err_msg = result.unwrap_err();
assert!(
err_msg.contains("Result") && err_msg.contains("Type mismatch"),
"Expected Result type mismatch, got: {}",
err_msg
);
}
+3
View File
@@ -696,3 +696,6 @@ impl DagRunner {
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[cfg(test)]
mod coverage_tests;
+249
View File
@@ -0,0 +1,249 @@
//! Coverage tests for runner module - specifically targeting uncovered error paths
//!
//! These tests ensure we hit the type mismatch error path in get() (line 601)
use crate::error::DagError;
use crate::runner::DagRunner;
use crate::task::Task;
use crate::types::TaskHandle;
use std::collections::HashMap;
#[tokio::test]
async fn test_get_type_mismatch_error() {
// Test the type mismatch error path in get() - line 601 in runner.rs
// This occurs when Arc downcast fails due to type mismatch
use crate::task_fn;
let dag = DagRunner::new();
// Task outputs i32
let source = dag.add_task(task_fn(|_: ()| async { 42i32 }));
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Try to get as String - TYPE MISMATCH!
// Create a handle with the same ID but wrong type
let wrong_handle: TaskHandle<String> = TaskHandle {
id: source.id,
_phantom: std::marker::PhantomData,
};
let result = dag.get(wrong_handle);
assert!(result.is_err());
let err = result.unwrap_err();
match err {
DagError::TypeMismatch { expected, found } => {
assert_eq!(expected, std::any::type_name::<String>());
assert_eq!(found, "unknown"); // This covers line 601!
}
_ => panic!("Expected TypeMismatch error, got: {:?}", err),
}
}
#[tokio::test]
async fn test_get_type_mismatch_i32_to_vec() {
// Different type mismatch: i32 to Vec<u8>
use crate::task_fn;
let dag = DagRunner::new();
// Task outputs i32
let int_task = dag.add_task(task_fn(|_: ()| async { 123i32 }));
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Try to get as Vec<u8>
let wrong_handle: TaskHandle<Vec<u8>> = TaskHandle {
id: int_task.id,
_phantom: std::marker::PhantomData,
};
let result = dag.get(wrong_handle);
assert!(result.is_err());
match result.unwrap_err() {
DagError::TypeMismatch { expected, found } => {
assert_eq!(expected, std::any::type_name::<Vec<u8>>());
assert_eq!(found, "unknown");
}
_ => panic!("Expected TypeMismatch error"),
}
}
#[tokio::test]
async fn test_get_type_mismatch_struct_to_hashmap() {
// Complex type mismatch: custom struct to HashMap
#[derive(Clone)]
#[allow(dead_code)]
struct CustomOutput {
value: i32,
}
struct CustomTask;
#[crate::task]
impl CustomTask {
async fn run(&self) -> CustomOutput {
CustomOutput { value: 42 }
}
}
let dag = DagRunner::new();
let task_handle = dag.add_task(CustomTask);
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Try to get as HashMap
let wrong_handle: TaskHandle<HashMap<String, i32>> = TaskHandle {
id: task_handle.id,
_phantom: std::marker::PhantomData,
};
let result = dag.get(wrong_handle);
assert!(result.is_err());
match result.unwrap_err() {
DagError::TypeMismatch { expected, found } => {
assert!(expected.contains("HashMap"));
assert_eq!(found, "unknown");
}
_ => panic!("Expected TypeMismatch error"),
}
}
#[tokio::test]
async fn test_get_type_mismatch_vec_different_types() {
// Type mismatch between Vec<i32> and Vec<String>
use crate::task_fn;
let dag = DagRunner::new();
// Task outputs Vec<i32>
let vec_task = dag.add_task(task_fn(|_: ()| async { vec![1, 2, 3] }));
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Try to get as Vec<String>
let wrong_handle: TaskHandle<Vec<String>> = TaskHandle {
id: vec_task.id,
_phantom: std::marker::PhantomData,
};
let result = dag.get(wrong_handle);
assert!(result.is_err());
match result.unwrap_err() {
DagError::TypeMismatch { expected, found } => {
assert_eq!(expected, std::any::type_name::<Vec<String>>());
assert_eq!(found, "unknown");
}
_ => panic!("Expected TypeMismatch error"),
}
}
#[tokio::test]
async fn test_get_type_mismatch_option_types() {
// Type mismatch with Option types
use crate::task_fn;
let dag = DagRunner::new();
// Task outputs Option<i32>
let opt_task = dag.add_task(task_fn(|_: ()| async { Some(42) }));
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Try to get as Option<String>
let wrong_handle: TaskHandle<Option<String>> = TaskHandle {
id: opt_task.id,
_phantom: std::marker::PhantomData,
};
let result = dag.get(wrong_handle);
assert!(result.is_err());
match result.unwrap_err() {
DagError::TypeMismatch { expected, found } => {
assert_eq!(expected, std::any::type_name::<Option<String>>());
assert_eq!(found, "unknown");
}
_ => panic!("Expected TypeMismatch error"),
}
}
#[tokio::test]
async fn test_multiple_type_mismatches_same_dag() {
// Test multiple type mismatches in the same DAG
use crate::task_fn;
let dag = DagRunner::new();
// Create tasks with different output types
let int_task = dag.add_task(task_fn(|_: ()| async { 100i32 }));
let string_task = dag.add_task(task_fn(|_: ()| async { "hello".to_string() }));
let bool_task = dag.add_task(task_fn(|_: ()| async { true }));
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Try to get int_task as String
let wrong_int_handle: TaskHandle<String> = TaskHandle {
id: int_task.id,
_phantom: std::marker::PhantomData,
};
assert!(matches!(
dag.get(wrong_int_handle).unwrap_err(),
DagError::TypeMismatch { found, .. } if found == "unknown"
));
// Try to get string_task as bool
let wrong_string_handle: TaskHandle<bool> = TaskHandle {
id: string_task.id,
_phantom: std::marker::PhantomData,
};
assert!(matches!(
dag.get(wrong_string_handle).unwrap_err(),
DagError::TypeMismatch { found, .. } if found == "unknown"
));
// Try to get bool_task as i32
let wrong_bool_handle: TaskHandle<i32> = TaskHandle {
id: bool_task.id,
_phantom: std::marker::PhantomData,
};
assert!(matches!(
dag.get(wrong_bool_handle).unwrap_err(),
DagError::TypeMismatch { found, .. } if found == "unknown"
));
// Verify correct types still work
assert_eq!(dag.get(int_task).unwrap(), 100);
assert_eq!(dag.get(string_task).unwrap(), "hello");
assert!(dag.get(bool_task).unwrap());
}
+3
View File
@@ -95,3 +95,6 @@ impl<T> From<&TaskHandle<T>> for TaskHandle<T> {
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[cfg(test)]
mod coverage_tests;
+283
View File
@@ -0,0 +1,283 @@
//! Coverage tests for types module - specifically targeting the TaskBuilder to TaskHandle conversion
//!
//! These tests ensure we hit line 71 in the From<TaskBuilder> implementation
use crate::types::TaskHandle;
use crate::{task, DagRunner, Task};
#[tokio::test]
async fn test_unit_type_taskbuilder_to_handle_conversion() {
// Test that explicitly uses the From<TaskBuilder> conversion for unit-type tasks
// This covers line 71 in types.rs
// Unit struct (no fields) - its Input type is ()
struct UnitTask;
#[task]
impl UnitTask {
async fn run(&self) -> i32 {
42
}
}
let dag = DagRunner::new();
// This creates a TaskBuilder<UnitTask, Pending>
// Since UnitTask has Input = (), it can be converted to TaskHandle
let builder = dag.add_task(UnitTask);
// Explicitly convert to TaskHandle using From implementation
// This triggers the From<TaskBuilder> implementation at line 69-74,
// specifically covering line 71
let handle: TaskHandle<i32> = builder.into();
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Verify the task ran and we can get its output
assert_eq!(dag.get(handle).unwrap(), 42);
}
#[tokio::test]
async fn test_unit_type_conversion_with_multiple_tasks() {
// Test conversion with multiple unit-type tasks
struct FirstTask;
#[task]
impl FirstTask {
async fn run(&self) -> String {
"first".to_string()
}
}
struct SecondTask;
#[task]
impl SecondTask {
async fn run(&self) -> i32 {
100
}
}
struct ThirdTask;
#[task]
impl ThirdTask {
async fn run(&self) -> bool {
true
}
}
let dag = DagRunner::new();
// Create builders for all tasks
let first_builder = dag.add_task(FirstTask);
let second_builder = dag.add_task(SecondTask);
let third_builder = dag.add_task(ThirdTask);
// Convert each builder to handle explicitly
let first_handle: TaskHandle<String> = first_builder.into();
let second_handle: TaskHandle<i32> = second_builder.into();
let third_handle: TaskHandle<bool> = third_builder.into();
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
// Verify all outputs
assert_eq!(dag.get(first_handle).unwrap(), "first");
assert_eq!(dag.get(second_handle).unwrap(), 100);
assert!(dag.get(third_handle).unwrap());
}
#[tokio::test]
async fn test_unit_struct_with_state() {
// Even structs with fields can have unit input type
struct StatefulTask {
value: i32,
}
impl StatefulTask {
fn new(value: i32) -> Self {
Self { value }
}
}
#[task]
impl StatefulTask {
async fn run(&self) -> i32 {
self.value * 2
}
}
let dag = DagRunner::new();
// Create task with initial state
let builder = dag.add_task(StatefulTask::new(21));
// Explicit conversion - triggers line 71
let handle: TaskHandle<i32> = builder.into();
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
assert_eq!(dag.get(handle).unwrap(), 42);
}
#[tokio::test]
async fn test_taskbuilder_conversion_assignment() {
// Test the conversion through assignment (different from .into())
struct SimpleTask;
#[task]
impl SimpleTask {
async fn run(&self) -> Vec<i32> {
vec![1, 2, 3]
}
}
let dag = DagRunner::new();
let builder = dag.add_task(SimpleTask);
// Conversion through type annotation and assignment
// This also uses From<TaskBuilder> implementation
let handle: TaskHandle<Vec<i32>> = builder.into();
let id = handle.id;
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
let result = dag.get(handle).unwrap();
assert_eq!(result, vec![1, 2, 3]);
// Verify the ID was preserved correctly through conversion
let another_handle = TaskHandle::<Vec<i32>> {
id,
_phantom: std::marker::PhantomData,
};
assert_eq!(dag.get(another_handle).unwrap(), vec![1, 2, 3]);
}
#[tokio::test]
async fn test_mixed_unit_and_dependent_tasks() {
// Test that unit-type tasks work alongside tasks with dependencies
struct Source;
#[task]
impl Source {
async fn run(&self) -> i32 {
10
}
}
struct Doubler;
#[task]
impl Doubler {
async fn run(&self, input: &i32) -> i32 {
input * 2
}
}
let dag = DagRunner::new();
// Source is unit-type, can be converted
let source_builder = dag.add_task(Source);
let source_handle: TaskHandle<i32> = source_builder.into(); // Covers line 71
// Doubler needs dependencies, so we use it differently
let doubler = dag.add_task(Doubler).depends_on(source_handle);
dag.run(|fut| {
tokio::spawn(fut);
})
.await
.unwrap();
assert_eq!(dag.get(source_handle).unwrap(), 10);
assert_eq!(dag.get(doubler).unwrap(), 20);
}
#[test]
fn test_taskbuilder_conversion_without_running() {
// Test that conversion works even without running the DAG
// This ensures the conversion is purely a type-level operation
struct TestTask;
#[task]
impl TestTask {
async fn run(&self) -> String {
"test".to_string()
}
}
let dag = DagRunner::new();
let builder = dag.add_task(TestTask);
let builder_id = builder.id;
// Convert without running the DAG
let handle: TaskHandle<String> = builder.into();
// Verify the ID was transferred correctly
assert_eq!(handle.id, builder_id);
}
#[test]
fn test_multiple_conversions_preserve_ids() {
// Ensure that multiple conversions maintain correct IDs
struct Task1;
#[task]
impl Task1 {
async fn run(&self) -> i32 {
1
}
}
struct Task2;
#[task]
impl Task2 {
async fn run(&self) -> i32 {
2
}
}
struct Task3;
#[task]
impl Task3 {
async fn run(&self) -> i32 {
3
}
}
let dag = DagRunner::new();
let builder1 = dag.add_task(Task1);
let builder2 = dag.add_task(Task2);
let builder3 = dag.add_task(Task3);
let id1 = builder1.id;
let id2 = builder2.id;
let id3 = builder3.id;
// Convert all builders
let handle1: TaskHandle<i32> = builder1.into();
let handle2: TaskHandle<i32> = builder2.into();
let handle3: TaskHandle<i32> = builder3.into();
// Verify IDs are preserved and sequential
assert_eq!(handle1.id, id1);
assert_eq!(handle2.id, id2);
assert_eq!(handle3.id, id3);
assert_eq!(handle1.id.0 + 1, handle2.id.0);
assert_eq!(handle2.id.0 + 1, handle3.id.0);
}
+3 -1
View File
@@ -309,8 +309,10 @@ async fn test_parallel_branches_with_dependencies() -> DagResult<()> {
b2_1_start - b1_1_start b2_1_start - b1_1_start
}; };
// Use a more generous threshold to account for coverage instrumentation overhead
// 50ms is enough to prove they're parallel (not sequential) while being robust to overhead
assert!( assert!(
branch_start_diff < Duration::from_millis(10), branch_start_diff < Duration::from_millis(50),
"Branches should start in parallel, diff was {:?}", "Branches should start in parallel, diff was {:?}",
branch_start_diff branch_start_diff
); );