pub struct FailureSchedule { /* private fields */ }Expand description
A schedule that decides whether a given attempt fails.
Schedules are deterministic by default. The same (schedule, attempt)
pair produces the same outcome across runs and machines.
§Example
use dev_chaos::{FailureMode, FailureSchedule};
let s = FailureSchedule::on_attempts(&[2, 4], FailureMode::IoError);
assert!(s.maybe_fail(1).is_ok());
assert!(s.maybe_fail(2).is_err());Implementations§
Source§impl FailureSchedule
impl FailureSchedule
Sourcepub fn on_attempts(attempts: &[usize], mode: FailureMode) -> Self
pub fn on_attempts(attempts: &[usize], mode: FailureMode) -> Self
Build a schedule that fails on specific attempt numbers (1-indexed).
§Example
use dev_chaos::{FailureMode, FailureSchedule};
let s = FailureSchedule::on_attempts(&[3, 7], FailureMode::Timeout);
assert!(s.maybe_fail(3).is_err());
assert!(s.maybe_fail(4).is_ok());Sourcepub fn every_n(n: usize, mode: FailureMode) -> Self
pub fn every_n(n: usize, mode: FailureMode) -> Self
Build a schedule that fails on every Nth attempt (1-indexed).
§Example
use dev_chaos::{FailureMode, FailureSchedule};
let s = FailureSchedule::every_n(3, FailureMode::Timeout);
assert!(s.maybe_fail(3).is_err());
assert!(s.maybe_fail(6).is_err());Sourcepub fn seeded_random(seed: u64, probability: f64, mode: FailureMode) -> Self
pub fn seeded_random(seed: u64, probability: f64, mode: FailureMode) -> Self
Build a deterministic, seeded “random” schedule.
Each attempt is hashed (with the seed) into a value in [0, 1000)
and fails when that value is below probability * 1000. The
schedule is fully reproducible from the seed.
probability is clamped to [0.0, 1.0].
This is the only non-explicit schedule. Even so, it is strictly reproducible; no real RNG state, no clock, no thread.
§Example
use dev_chaos::{FailureMode, FailureSchedule};
let a = FailureSchedule::seeded_random(42, 0.10, FailureMode::IoError);
let b = FailureSchedule::seeded_random(42, 0.10, FailureMode::IoError);
// Same seed => same outcome at every attempt.
for attempt in 1..=100 {
assert_eq!(a.maybe_fail(attempt).is_err(), b.maybe_fail(attempt).is_err());
}Sourcepub fn maybe_fail(&self, attempt: usize) -> Result<(), InjectedFailure>
pub fn maybe_fail(&self, attempt: usize) -> Result<(), InjectedFailure>
Check whether the given attempt should fail.
Returns Ok(()) if the operation should proceed, or
Err(InjectedFailure) if the schedule fires on this attempt.
Sourcepub fn invocation_count(&self) -> usize
pub fn invocation_count(&self) -> usize
Total invocations of maybe_fail since this schedule was built.
Sourcepub fn mode(&self) -> FailureMode
pub fn mode(&self) -> FailureMode
Mode this schedule injects.