with_failpoint

Macro with_failpoint 

Source
macro_rules! with_failpoint {
    ($tag:literal, panic, $code:expr) => { ... };
    ($tag:literal, error, $code:expr) => { ... };
    ($tag:literal, $min_ms:literal, $tolerance_ms:literal, $code:expr) => { ... };
}
Expand description

Runs a code block with a failpoint enabled and validates its effect.

Supported modes:

  • panic: Expects the code to panic when the failpoint is active.
  • error: Expects the code to return Err when the failpoint is active.
  • Sleep validation: Verifies that code sleeps somewhere in the range of min_ms - tolerance and min_ms + tolerance when failpoint is active.

§Examples

Expects a panic:

chaos_rs::with_failpoint!("panic_test", panic, {
    chaos_rs::maybe_panic!("panic_test");
});

Expects an error:

chaos_rs::with_failpoint!("error_test", error, {
    fn test() -> Result<(), ()> {
        chaos_rs::maybe_fail!("error_test", ());
        Ok(())
    }
    test()
});

Expects the operation to sleep for 200 ± 50ms (150 - 250 range):

chaos_rs::with_failpoint!("sleep_test", 200, 50, {
    chaos_rs::maybe_sleep!("sleep_test", 200);
});