chaos-rs

Crates.iochaos-rs
lib.rschaos-rs
version0.1.4
created_at2025-07-30 13:43:40.826339+00
updated_at2025-07-30 14:07:33.220513+00
descriptionMinimal library for chaos testing
homepage
repositoryhttps://github.com/ConeDjordjic/chaos-rs
max_upload_size
id1773547
size18,386
Nemanja Djordjic (ConeDjordjic)

documentation

README

chaos-rs

Crates.io License: MIT

Minimal chaos testing library for Rust.

Overview

chaos-rs provides macros for injecting failures, panics, and delays during testing, enabling you to validate your code's resilience under adverse conditions.

Features

  • Fail injection: Return errors from tagged failpoints
  • Panic simulation: Trigger panics when failpoints are enabled
  • Sleep injection: Add artificial delays for timing tests (sync and async)
  • Assertion helpers: Verify that failpoints behave as expected

Usage

Add to your Cargo.toml:

[dependencies]
chaos-rs = "0.1.4"

Basic Examples

Inject failures:

fn do_work() -> Result<&'static str, String> {
    chaos_rs::maybe_fail!("db_error", "Database connection failed".into());
    Ok("success")
}

Simulate panics:

fn critical_section() {
    chaos_rs::maybe_panic!("unexpected_panic");
    // ... critical code
}

Add delays:

fn slow_operation() {
    chaos_rs::maybe_sleep!("slow_io", 500);
    // ... operation
}

async fn async_operation() {
    chaos_rs::maybe_sleep_async!("slow_async", 200);
    // ... async operation
}

Testing Failpoints

#[test]
fn test_error_handling() {
    chaos_rs::with_failpoint!("db_error", error, do_work());
}

#[test]
fn test_panic_handling() {
    chaos_rs::with_failpoint!("unexpected_panic", panic, {
        critical_section();
    });
}

#[test]
fn test_timing() {
    chaos_rs::with_failpoint!("slow_io", 500, 50, {
        slow_operation(); // Should sleep 450-550ms
    });
}

License

MIT

Commit count: 0

cargo fmt