#testing #injection #fault #reliability #crash #io-error

fault-injection

Simple fault injection, concurrency delay injection, and error source annotation

9 stable releases

1.0.10 Aug 5, 2023
1.0.9 Jun 19, 2023
1.0.7 Aug 13, 2022
1.0.4 Jul 3, 2022
0.1.0 May 16, 2022

#137 in Concurrency

Download history 171/week @ 2024-07-20 182/week @ 2024-07-27 145/week @ 2024-08-03 116/week @ 2024-08-10 207/week @ 2024-08-17 110/week @ 2024-08-24 126/week @ 2024-08-31 153/week @ 2024-09-07 203/week @ 2024-09-14 164/week @ 2024-09-21 95/week @ 2024-09-28 120/week @ 2024-10-05 113/week @ 2024-10-12 116/week @ 2024-10-19 120/week @ 2024-10-26 178/week @ 2024-11-02

547 downloads per month
Used in 11 crates (4 directly)

MIT/Apache

10KB
83 lines

fault-injection

docs

Similar to the try! macro or ? operator, but externally controllable to inject faults during testing. Unlike the try! macro or ? operator, this additionally annotates the description of the error to include the crate, file name, and line number where the error originated from to facilitate quick debugging. It is specialized to work with io::Result types, and will return an io::Error for faults, with into() called similar to the try! macro or ? operator. Decrements the FAULT_INJECT_COUNTER by 1 (it is set to u64::MAX by default), and if it hits 0, returns an io::Error with a kind of Other. If SLEEPINESS is set to something other than 0, this macro will also inject weakly pseudorandom delays for facilitating a basic form of concurrency testing.

Examples

use std::io;

use fault_injection::{fallible, set_trigger_function, FAULT_INJECT_COUNTER};

fn trigger_fn(crate_name: &str, file_name: &str, line_number: u32) {
    println!(
        "fault injected at {} {} {}",
        crate_name, file_name, line_number
    );
}

fn do_io() -> io::Result<()> {
    Ok(())
}

// this will return an injected error
fn use_it() -> std::io::Result<()> {
    set_trigger_function(trigger_fn);
    FAULT_INJECT_COUNTER.store(1, std::sync::atomic::Ordering::Release);

    fallible!(do_io());

    Ok(())
}

No runtime deps