|
| 1 | +//! Antithesis SDK integration for invariant testing. |
| 2 | +//! |
| 3 | +//! This module provides assertion macros that integrate with the Antithesis |
| 4 | +//! testing platform to detect invariant violations during fuzz testing. |
| 5 | +
|
| 6 | +use std::sync::OnceLock; |
| 7 | + |
| 8 | +/// Ensure Antithesis is initialized exactly once. |
| 9 | +pub(crate) fn ensure_init() { |
| 10 | + static INIT: OnceLock<()> = OnceLock::new(); |
| 11 | + INIT.get_or_init(|| { |
| 12 | + ::antithesis_sdk::antithesis_init(); |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +/// Assert that a condition is always true during Antithesis fuzz testing. |
| 17 | +/// Use `false` as the condition to log an invariant violation. |
| 18 | +macro_rules! assert_always { |
| 19 | + ($condition:expr, $message:literal, $details:expr) => {{ |
| 20 | + $crate::antithesis::ensure_init(); |
| 21 | + let details: ::serde_json::Value = $details; |
| 22 | + ::antithesis_sdk::assert_always!($condition, $message, &details); |
| 23 | + }}; |
| 24 | + ($condition:expr, $message:literal) => {{ |
| 25 | + $crate::antithesis::ensure_init(); |
| 26 | + ::antithesis_sdk::assert_always!($condition, $message); |
| 27 | + }}; |
| 28 | +} |
| 29 | + |
| 30 | +/// Assert that a condition is sometimes true during Antithesis fuzz testing. |
| 31 | +/// This checks that the condition occurs at least once across the entire test session. |
| 32 | +macro_rules! assert_sometimes { |
| 33 | + ($condition:expr, $message:literal, $details:expr) => {{ |
| 34 | + $crate::antithesis::ensure_init(); |
| 35 | + let details: ::serde_json::Value = $details; |
| 36 | + ::antithesis_sdk::assert_sometimes!($condition, $message, &details); |
| 37 | + }}; |
| 38 | + ($condition:expr, $message:literal) => {{ |
| 39 | + $crate::antithesis::ensure_init(); |
| 40 | + ::antithesis_sdk::assert_sometimes!($condition, $message); |
| 41 | + }}; |
| 42 | +} |
| 43 | + |
| 44 | +/// Assert that a code location is unreachable during Antithesis fuzz testing. |
| 45 | +/// Use this for code paths that should never be reached (bugs, invariant violations). |
| 46 | +macro_rules! assert_unreachable { |
| 47 | + ($message:literal, $details:expr) => {{ |
| 48 | + $crate::antithesis::ensure_init(); |
| 49 | + let details: ::serde_json::Value = $details; |
| 50 | + ::antithesis_sdk::assert_unreachable!($message, &details); |
| 51 | + }}; |
| 52 | + ($message:literal) => {{ |
| 53 | + $crate::antithesis::ensure_init(); |
| 54 | + ::antithesis_sdk::assert_unreachable!($message); |
| 55 | + }}; |
| 56 | +} |
| 57 | + |
| 58 | +pub(crate) use assert_always; |
| 59 | +pub(crate) use assert_sometimes; |
| 60 | +pub(crate) use assert_unreachable; |
0 commit comments