Skip to main content

LatencyInjector

Struct LatencyInjector 

Source
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

Source

pub fn new(profile: LatencyProfile) -> Self

Build an injector from a profile.

Source

pub fn delay_for(&self, attempt: usize) -> Duration

Compute the delay that would be applied at attempt (1-indexed).

Source

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

Source

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§

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.