chaos_rs/lib.rs
1//! # chaos_rs
2//!
3//! **Minimal chaos testing library for Rust.**
4//!
5//! `chaos_rs` provides macros for injecting failures, panics, and delays during testing,
6//! enabling you to validate your code's resilience under adverse conditions.
7//!
8//! ## Features
9//! - **Fail injection**: Return errors from tagged failpoints (`maybe_fail!`).
10//! - **Panic simulation**: Trigger panics when failpoints are enabled (`maybe_panic!`).
11//! - **Sleep injection**: Add artificial delays for timing tests (`maybe_sleep!`) and async with
12//! (`maybe_sleep_async!`).
13//! - **Assertion helpers**: Verify that failpoints behave as expected (`with_failpoint!`) or
14//! (`with_failpoint_async!`) for async.
15//!
16//! ## Example
17//! ```rust
18//! fn do_work() -> Result<&'static str, String> {
19//! chaos_rs::maybe_fail!("db_error", "Database connection failed".into());
20//! Ok("success")
21//! }
22//! ```
23
24pub mod __failpoint_internal;
25mod macros;
26
27#[cfg(test)]
28mod tests {
29 use crate::*;
30 use std::time::Instant;
31
32 #[test]
33 fn test_maybe_fail() {
34 fn example() -> Result<&'static str, String> {
35 maybe_fail!("fail_test");
36 Ok("ok")
37 }
38
39 assert_eq!(example().unwrap(), "ok");
40
41 with_failpoint!("fail_test", error, example());
42 }
43
44 #[test]
45 fn test_maybe_panic() {
46 fn risky() {
47 maybe_panic!("panic_test");
48 }
49
50 risky();
51
52 with_failpoint!("panic_test", panic, risky());
53 }
54
55 #[test]
56 fn test_maybe_sleep() {
57 fn slow() {
58 maybe_sleep!("sleep_test", 50);
59 }
60
61 let start = Instant::now();
62 slow();
63 assert!(start.elapsed().as_millis() < 10);
64
65 with_failpoint!("sleep_test", 50, 10, slow());
66 }
67}