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>
impl<C: RunnableChild> SyncChildTestHarness<C>
Sourcepub fn new(child: C) -> Self
pub fn new(child: C) -> Self
Creates a new test harness for the given 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 run(&mut self, input: Value) -> ChildResult
pub fn run(&mut self, input: Value) -> ChildResult
Executes run() on the child with the given input.
Sourcepub fn run_json<T: Serialize>(&mut self, input: T) -> ChildResult
pub fn run_json<T: Serialize>(&mut self, input: T) -> ChildResult
Executes 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 SyncChildTestHarness<C>where
C: Freeze,
impl<C> RefUnwindSafe for SyncChildTestHarness<C>where
C: RefUnwindSafe,
impl<C> Send for SyncChildTestHarness<C>
impl<C> Sync for SyncChildTestHarness<C>
impl<C> Unpin for SyncChildTestHarness<C>where
C: Unpin,
impl<C> UnsafeUnpin for SyncChildTestHarness<C>where
C: UnsafeUnpin,
impl<C> UnwindSafe for SyncChildTestHarness<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