pub struct LabReactor { /* private fields */ }Expand description
A deterministic reactor for testing.
This reactor operates in virtual time and allows test code to inject events at specific points. It’s used by the lab runtime for deterministic testing of async I/O code.
Implementations§
Source§impl LabReactor
impl LabReactor
Sourcepub fn with_chaos(config: ChaosConfig) -> Self
pub fn with_chaos(config: ChaosConfig) -> Self
Creates a new lab reactor with chaos injection enabled.
Sourcepub fn inject_event(&self, token: Token, event: Event, delay: Duration)
pub fn inject_event(&self, token: Token, event: Event, delay: Duration)
Injects an event into the reactor at a specific delay from now.
The event will be delivered when virtual time advances past the delay. This is the primary mechanism for testing I/O-dependent code. Events scheduled at the same time are delivered in insertion order.
§Arguments
token- The token to associate with the eventevent- The event to injectdelay- How far in the future to deliver the event
§Aliases
This method is also known as schedule_event() in the spec.
Sourcepub fn schedule_event(&self, token: Token, event: Event, delay: Duration)
pub fn schedule_event(&self, token: Token, event: Event, delay: Duration)
Alias for inject_event to match the spec terminology.
Schedules an event for future delivery at a specific delay from now.
Sourcepub fn set_ready(&self, token: Token, event: Event)
pub fn set_ready(&self, token: Token, event: Event)
Makes a source immediately ready for the specified event type.
The event will be delivered on the next call to poll().
Multiple calls to set_ready() for the same token append events.
§Arguments
token- The token to make readyevent- The event type (readable, writable, etc.)
Sourcepub fn next_event_time(&self) -> Option<Time>
pub fn next_event_time(&self) -> Option<Time>
Returns the next scheduled event time, if any.
This is useful for driving the lab runtime forward to the next I/O event without relying on wall-clock time.
Sourcepub fn advance_time(&self, duration: Duration)
pub fn advance_time(&self, duration: Duration)
Advances virtual time by the specified duration.
This is useful for testing timeout behavior without going through poll().
Sourcepub fn advance_time_to(&self, target: Time)
pub fn advance_time_to(&self, target: Time)
Advances virtual time to a specific target time.
If the target time is before the current time, this is a no-op.
Events scheduled between the current time and target time will be
delivered on the next poll() call.
§Arguments
target- The target virtual time to advance to
Sourcepub fn chaos_stats(&self) -> ChaosStats
pub fn chaos_stats(&self) -> ChaosStats
Returns a snapshot of global chaos statistics accumulated by this reactor.
Sourcepub fn last_io_error_kind(&self) -> Option<ErrorKind>
pub fn last_io_error_kind(&self) -> Option<ErrorKind>
Returns the last global chaos I/O error kind injected by this reactor.
Sourcepub fn check_and_clear_wake(&self) -> bool
pub fn check_and_clear_wake(&self) -> bool
Checks if the reactor has been woken.
Clears the wake flag and returns its previous value.
Sourcepub fn set_fault_config(&self, token: Token, config: FaultConfig) -> Result<()>
pub fn set_fault_config(&self, token: Token, config: FaultConfig) -> Result<()>
Sets the fault configuration for a specific token.
This enables per-connection fault injection, allowing tests to simulate failures on specific connections while others remain healthy.
§Arguments
token- The token to configure faults forconfig- The fault configuration to apply
§Returns
Returns Err if the token is not registered.
§Example
use asupersync::runtime::reactor::{LabReactor, FaultConfig, Token};
use std::io;
let reactor = LabReactor::new();
let token = Token::new(1);
// ... register token ...
let config = FaultConfig::new()
.with_error_probability(0.5)
.with_error_kinds(vec![io::ErrorKind::ConnectionReset]);
reactor.set_fault_config(token, config)?;Sourcepub fn clear_fault_config(&self, token: Token) -> Result<()>
pub fn clear_fault_config(&self, token: Token) -> Result<()>
Clears fault configuration for a token.
Removes any per-token fault injection, returning to normal behavior.
§Returns
Returns Err if the token is not registered.
Sourcepub fn inject_error(&self, token: Token, kind: ErrorKind) -> Result<()>
pub fn inject_error(&self, token: Token, kind: ErrorKind) -> Result<()>
Injects an immediate error for the next event on a token.
The next event delivered for this token will be converted to an error
event with the specified ErrorKind. This is a one-shot operation;
subsequent events are not affected unless inject_error is called again.
§Arguments
token- The token to inject an error forkind- The error kind to inject
§Returns
Returns Err if the token is not registered.
§Example
use asupersync::runtime::reactor::{LabReactor, Token};
use std::io;
let reactor = LabReactor::new();
let token = Token::new(1);
// ... register token ...
// The next event for this token will be an error
reactor.inject_error(token, io::ErrorKind::BrokenPipe)?;Sourcepub fn inject_close(&self, token: Token) -> Result<()>
pub fn inject_close(&self, token: Token) -> Result<()>
Injects a connection close (HUP) for a token.
Marks the token as closed, simulating the remote end closing the connection. The next poll will deliver HUP even if no readiness is queued for the token.
§Arguments
token- The token to close
§Returns
Returns Err if the token is not registered.
§Example
use asupersync::runtime::reactor::{LabReactor, Token};
let reactor = LabReactor::new();
let token = Token::new(1);
// ... register token ...
// Simulate remote close
reactor.inject_close(token)?;
// Next poll will deliver HUPSourcepub fn partition(&self, token: Token, partitioned: bool) -> Result<()>
pub fn partition(&self, token: Token, partitioned: bool) -> Result<()>
Sets the partition state for a token.
When partitioned, events for this token are dropped rather than delivered, simulating a network partition. This is useful for testing timeout handling and partition recovery.
§Arguments
token- The token to partitionpartitioned-trueto enable partition,falseto disable
§Returns
Returns Err if the token is not registered.
§Example
use asupersync::runtime::reactor::{LabReactor, Token, Interest, Event};
use std::time::Duration;
let reactor = LabReactor::new();
let token = Token::new(1);
// ... register token ...
// Simulate network partition
reactor.partition(token, true)?;
reactor.inject_event(token, Event::readable(token), Duration::ZERO);
// Event will be dropped, not delivered
let mut events = Events::with_capacity(10);
reactor.poll(&mut events, Some(Duration::ZERO))?;
assert!(events.is_empty());
// Restore connectivity
reactor.partition(token, false)?;Sourcepub fn last_injected_error(&self, token: Token) -> Option<ErrorKind>
pub fn last_injected_error(&self, token: Token) -> Option<ErrorKind>
Returns the last error kind injected for a token (for diagnostics).
Trait Implementations§
Source§impl Debug for LabReactor
impl Debug for LabReactor
Source§impl Default for LabReactor
impl Default for LabReactor
Source§impl Reactor for LabReactor
impl Reactor for LabReactor
Source§fn register(
&self,
_source: &dyn Source,
token: Token,
interest: Interest,
) -> Result<()>
fn register( &self, _source: &dyn Source, token: Token, interest: Interest, ) -> Result<()>
Source§fn modify(&self, token: Token, interest: Interest) -> Result<()>
fn modify(&self, token: Token, interest: Interest) -> Result<()>
Source§fn deregister(&self, token: Token) -> Result<()>
fn deregister(&self, token: Token) -> Result<()>
Source§fn poll(&self, events: &mut Events, timeout: Option<Duration>) -> Result<usize>
fn poll(&self, events: &mut Events, timeout: Option<Duration>) -> Result<usize>
timeout. Read moreSource§fn registration_count(&self) -> usize
fn registration_count(&self) -> usize
Auto Trait Implementations§
impl !Freeze for LabReactor
impl !RefUnwindSafe for LabReactor
impl Send for LabReactor
impl Sync for LabReactor
impl Unpin for LabReactor
impl UnsafeUnpin for LabReactor
impl UnwindSafe for LabReactor
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more