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

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

#132 in Concurrency

Download history 33/week @ 2023-11-25 15/week @ 2023-12-02 27/week @ 2023-12-09 73/week @ 2023-12-16 47/week @ 2023-12-23 3/week @ 2023-12-30 14/week @ 2024-01-06 30/week @ 2024-01-13 18/week @ 2024-01-20 14/week @ 2024-01-27 19/week @ 2024-02-03 26/week @ 2024-02-10 99/week @ 2024-02-17 83/week @ 2024-02-24 38/week @ 2024-03-02 18/week @ 2024-03-09

245 downloads per month
Used in 8 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