pub struct LatencyInjector { /* private fields */ }Expand description
Computes per-attempt delays from a LatencyProfile.
LatencyInjector is intentionally side-effect-free: it returns
the delay it would sleep, leaving the actual thread::sleep
(or tokio::time::sleep) up to the caller. This keeps the type
usable in both sync and async contexts.
§Example
use dev_chaos::latency::{LatencyInjector, LatencyProfile};
use std::time::Duration;
let inj = LatencyInjector::new(LatencyProfile::Constant(Duration::from_millis(5)));
assert_eq!(inj.delay_for(1), Duration::from_millis(5));
assert_eq!(inj.delay_for(100), Duration::from_millis(5));Implementations§
Source§impl LatencyInjector
impl LatencyInjector
Sourcepub fn new(profile: LatencyProfile) -> Self
pub fn new(profile: LatencyProfile) -> Self
Build an injector from a profile.
Sourcepub fn delay_for(&self, attempt: usize) -> Duration
pub fn delay_for(&self, attempt: usize) -> Duration
Compute the delay that would be applied at attempt (1-indexed).
Sourcepub fn apply_blocking(&self, attempt: usize)
pub fn apply_blocking(&self, attempt: usize)
Apply the delay synchronously by sleeping the calling thread.
Equivalent to std::thread::sleep(self.delay_for(attempt)).
Sourcepub fn compose_with_schedule(
self,
schedule: FailureSchedule,
) -> LatencyAndFailure
pub fn compose_with_schedule( self, schedule: FailureSchedule, ) -> LatencyAndFailure
Bind this injector to a FailureSchedule
so a single call applies latency and checks for failure injection.
Returns a LatencyAndFailure composer:
- On every attempt, the latency is applied (sync sleep).
- On scheduled-failure attempts, the call returns
Err(InjectedFailure)after the latency has been applied (so the test observes both the slowdown and the failure).
§Example
use dev_chaos::{
latency::{LatencyInjector, LatencyProfile},
FailureMode, FailureSchedule,
};
use std::time::Duration;
let inj = LatencyInjector::new(LatencyProfile::Constant(Duration::ZERO));
let schedule = FailureSchedule::on_attempts(&[2], FailureMode::Timeout);
let composed = inj.compose_with_schedule(schedule);
assert!(composed.apply_blocking(1).is_ok());
assert!(composed.apply_blocking(2).is_err());Auto Trait Implementations§
impl Freeze for LatencyInjector
impl RefUnwindSafe for LatencyInjector
impl Send for LatencyInjector
impl Sync for LatencyInjector
impl Unpin for LatencyInjector
impl UnsafeUnpin for LatencyInjector
impl UnwindSafe for LatencyInjector
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