Skip to main content

SyncChildTestHarness

Struct SyncChildTestHarness 

Source
pub struct SyncChildTestHarness<C: RunnableChild> { /* private fields */ }
Expand description

Test harness for synchronous RunnableChild implementations.

Provides run() testing with automatic time measurement.

§Example

use orcs_component::testing::SyncChildTestHarness;
use orcs_component::{Child, RunnableChild, ChildResult, Identifiable, SignalReceiver, Statusable, Status};
use orcs_event::{Signal, SignalResponse};
use serde_json::{json, Value};

struct Worker {
    id: String,
    status: Status,
}

impl Identifiable for Worker {
    fn id(&self) -> &str { &self.id }
}

impl SignalReceiver for Worker {
    fn on_signal(&mut self, signal: &Signal) -> SignalResponse {
        if signal.is_veto() {
            self.abort();
            SignalResponse::Abort
        } else {
            SignalResponse::Handled
        }
    }
    fn abort(&mut self) { self.status = Status::Aborted; }
}

impl Statusable for Worker {
    fn status(&self) -> Status { self.status }
}

impl Child for Worker {}

impl RunnableChild for Worker {
    fn run(&mut self, input: Value) -> ChildResult {
        ChildResult::Ok(json!({"echo": input}))
    }
}

let worker = Worker { id: "worker".into(), status: Status::Idle };
let mut harness = SyncChildTestHarness::new(worker);

let result = harness.run(json!({"task": "test"}));
assert!(result.is_ok());
assert_eq!(harness.run_log().len(), 1);

Implementations§

Source§

impl<C: RunnableChild> SyncChildTestHarness<C>

Source

pub fn new(child: C) -> Self

Creates a new test harness for the given runnable child.

Time measurement is enabled by default.

Source

pub fn without_time_measurement(self) -> Self

Disables time measurement.

Source

pub fn run(&mut self, input: Value) -> ChildResult

Executes run() on the child with the given input.

Source

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

Executes 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.