pub struct AsyncChildTestHarness<C: AsyncRunnableChild> { /* private fields */ }Expand description
Test harness for asynchronous AsyncRunnableChild implementations.
Provides async run() testing with automatic time measurement and timeout support.
§Example
ⓘ
use orcs_component::testing::AsyncChildTestHarness;
use orcs_component::{Child, AsyncRunnableChild, ChildResult, async_trait};
use serde_json::{json, Value};
use std::time::Duration;
struct AsyncWorker { /* ... */ }
#[async_trait]
impl AsyncRunnableChild for AsyncWorker {
async fn run(&mut self, input: Value) -> ChildResult {
tokio::time::sleep(Duration::from_millis(10)).await;
ChildResult::Ok(json!({"processed": input}))
}
}
#[tokio::test]
async fn test_async_worker() {
let worker = AsyncWorker::new();
let mut harness = AsyncChildTestHarness::new(worker)
.with_default_timeout(Duration::from_secs(5));
let result = harness.run(json!({"task": "test"})).await;
assert!(result.is_ok());
// Test timeout
let slow_result = harness
.run_with_timeout(json!({}), Duration::from_millis(1))
.await;
assert!(slow_result.is_err());
}Implementations§
Source§impl<C: AsyncRunnableChild> AsyncChildTestHarness<C>
impl<C: AsyncRunnableChild> AsyncChildTestHarness<C>
Sourcepub fn new(child: C) -> Self
pub fn new(child: C) -> Self
Creates a new test harness for the given async runnable child.
Time measurement is enabled by default.
Sourcepub fn without_time_measurement(self) -> Self
pub fn without_time_measurement(self) -> Self
Disables time measurement.
Sourcepub fn with_default_timeout(self, timeout: Duration) -> Self
pub fn with_default_timeout(self, timeout: Duration) -> Self
Sets the default timeout for run operations.
Sourcepub async fn run(&mut self, input: Value) -> ChildResult
pub async fn run(&mut self, input: Value) -> ChildResult
Executes async run() on the child with the given input.
If a default timeout is set, it will be applied.
Sourcepub async fn run_with_timeout(
&mut self,
input: Value,
timeout: Duration,
) -> Result<ChildResult, TimeoutError>
pub async fn run_with_timeout( &mut self, input: Value, timeout: Duration, ) -> Result<ChildResult, TimeoutError>
Executes async run() with a timeout.
Returns Err(TimeoutError) if the operation times out.
Sourcepub async fn run_json<T: Serialize>(&mut self, input: T) -> ChildResult
pub async fn run_json<T: Serialize>(&mut self, input: T) -> ChildResult
Executes async run() with a serializable input.
Sourcepub fn clear_all_logs(&mut self)
pub fn clear_all_logs(&mut self)
Clears all logs (both run and signal).
Sourcepub fn send_signal(&mut self, signal: Signal) -> SignalResponse
pub fn send_signal(&mut self, signal: Signal) -> SignalResponse
Sends a signal to the child.
Sourcepub fn veto(&mut self) -> SignalResponse
pub fn veto(&mut self) -> SignalResponse
Sends a Veto signal to the child.
Sourcepub fn cancel(&mut self) -> SignalResponse
pub fn cancel(&mut self) -> SignalResponse
Sends a Cancel signal to the child.
Sourcepub fn signal_log(&self) -> &[SignalRecord]
pub fn signal_log(&self) -> &[SignalRecord]
Returns the signal log.
Auto Trait Implementations§
impl<C> Freeze for AsyncChildTestHarness<C>where
C: Freeze,
impl<C> RefUnwindSafe for AsyncChildTestHarness<C>where
C: RefUnwindSafe,
impl<C> Send for AsyncChildTestHarness<C>
impl<C> Sync for AsyncChildTestHarness<C>
impl<C> Unpin for AsyncChildTestHarness<C>where
C: Unpin,
impl<C> UnsafeUnpin for AsyncChildTestHarness<C>where
C: UnsafeUnpin,
impl<C> UnwindSafe for AsyncChildTestHarness<C>where
C: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more