Skip to main content

AsyncChildTestHarness

Struct AsyncChildTestHarness 

Source
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>

Source

pub fn new(child: C) -> Self

Creates a new test harness for the given async runnable child.

Time measurement is enabled by default.

Source

pub fn without_time_measurement(self) -> Self

Disables time measurement.

Source

pub fn with_default_timeout(self, timeout: Duration) -> Self

Sets the default timeout for run operations.

Source

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.

Source

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.

Source

pub async fn run_json<T: Serialize>(&mut self, input: T) -> ChildResult

Executes async run() with a serializable input.

Source

pub fn run_log(&self) -> &[RunRecord]

Returns the run log for snapshot testing.

Source

pub fn clear_all_logs(&mut self)

Clears all logs (both run and signal).

Source

pub fn child(&self) -> &C

Returns a reference to the child under test.

Source

pub fn child_mut(&mut self) -> &mut C

Returns a mutable reference to the child under test.

Source

pub fn id(&self) -> &str

Returns the child’s ID.

Source

pub fn status(&self) -> Status

Returns the current status of the child.

Source

pub fn send_signal(&mut self, signal: Signal) -> SignalResponse

Sends a signal to the child.

Source

pub fn veto(&mut self) -> SignalResponse

Sends a Veto signal to the child.

Source

pub fn cancel(&mut self) -> SignalResponse

Sends a Cancel signal to the child.

Source

pub fn abort(&mut self)

Calls abort() on the child.

Source

pub fn signal_log(&self) -> &[SignalRecord]

Returns the signal log.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.