Skip to content

Commit ada2e54

Browse files
committed
gate compilation at module and call site, use dbg_panic macro at state machines instead of direct calls to antithesis assert_always (dbg_panic just logs in non-release builds with no antithesis_assertions feature flag anyways)
1 parent 43a8a7a commit ada2e54

File tree

9 files changed

+70
-8
lines changed

9 files changed

+70
-8
lines changed

crates/sdk-core-c-bridge/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ thiserror = { workspace = true }
5555
cbindgen = { version = "0.29", default-features = false }
5656

5757
[features]
58+
antithesis_assertions = ["temporalio-sdk-core/antithesis_assertions"]
5859
xz2-static = ["xz2/static"]

crates/sdk-core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ tokio-console = ["console-subscriber"]
2727
ephemeral-server = ["dep:flate2", "dep:reqwest", "dep:tar", "dep:zip"]
2828
debug-plugin = ["dep:reqwest"]
2929
test-utilities = ["dep:assert_matches", "dep:bimap"]
30+
antithesis_assertions = ["dep:antithesis_sdk"]
3031

3132
[dependencies]
3233
anyhow = "1.0"
34+
antithesis_sdk = { version = "0.2.1", optional = true, default-features = false, features = ["full"] }
3335
assert_matches = { version = "1.5", optional = true }
3436
bimap = { version = "0.6.3", optional = true }
3537
async-trait = "0.1"

crates/sdk-core/src/abstractions.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,23 @@ impl<SK: SlotKind> OwnedMeteredSemPermit<SK> {
423423
pub(crate) struct UsedMeteredSemPermit<SK: SlotKind>(#[allow(dead_code)] OwnedMeteredSemPermit<SK>);
424424

425425
macro_rules! dbg_panic {
426-
($($arg:tt)*) => {
427-
error!($($arg)*);
428-
debug_assert!(false, $($arg)*);
429-
};
426+
($($arg:tt)*) => {{
427+
let message = format!($($arg)*);
428+
error!("{}", message);
429+
430+
#[cfg(feature = "antithesis_assertions")]
431+
crate::antithesis::assert_always!(
432+
false,
433+
"dbg_panic invariant triggered",
434+
::serde_json::json!({
435+
"message": message,
436+
"file": file!(),
437+
"line": line!(),
438+
"module": module_path!(),
439+
})
440+
);
441+
debug_assert!(false, "{}", message);
442+
}};
430443
}
431444
pub(crate) use dbg_panic;
432445

crates/sdk-core/src/antithesis.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
pub(crate) use assert_always;

crates/sdk-core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern crate tracing;
1212
extern crate core;
1313

1414
mod abstractions;
15+
#[cfg(feature = "antithesis_assertions")]
16+
mod antithesis;
1517
#[cfg(feature = "debug-plugin")]
1618
pub mod debug_client;
1719
#[cfg(feature = "ephemeral-server")]

crates/sdk-core/src/worker/workflow/machines/activity_state_machine.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ impl ActivityMachine {
193193
ActivityMachineCommand::Cancel(details) => {
194194
vec![self.create_cancelation_resolve(details).into()]
195195
}
196-
x => panic!("Invalid cancel event response {x:?}"),
196+
x => {
197+
dbg_panic!("Invalid cancel event response {x:?}");
198+
panic!("Invalid cancel event response {x:?}");
199+
}
197200
})
198201
.collect();
199202
Ok(res)

crates/sdk-core/src/worker/workflow/machines/child_workflow_state_machine.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{
33
WFMachinesAdapter, WFMachinesError, fsm, workflow_machines::MachineResponse,
44
};
55
use crate::{
6+
abstractions::dbg_panic,
67
internal_flags::CoreInternalFlags,
78
worker::workflow::{InternalFlagsRef, machines::HistEventData},
89
};
@@ -496,7 +497,10 @@ impl ChildWorkflowMachine {
496497
| c @ ChildWorkflowCommand::IssueCancelAfterStarted { .. } => {
497498
self.adapt_response(c, None)
498499
}
499-
x => panic!("Invalid cancel event response {x:?}"),
500+
x => {
501+
dbg_panic!("Invalid cancel event response {x:?}");
502+
panic!("Invalid cancel event response {x:?}");
503+
}
500504
})
501505
.flatten_ok()
502506
.try_collect()?;

crates/sdk-core/src/worker/workflow/machines/complete_workflow_state_machine.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{
22
EventInfo, NewMachineWithCommand, OnEventWrapper, StateMachine, TransitionResult,
33
WFMachinesAdapter, WFMachinesError, fsm, workflow_machines::MachineResponse,
44
};
5-
use crate::worker::workflow::machines::HistEventData;
5+
use crate::{abstractions::dbg_panic, worker::workflow::machines::HistEventData};
66
use std::convert::TryFrom;
77
use temporalio_common::protos::{
88
coresdk::workflow_commands::CompleteWorkflowExecution,
@@ -41,7 +41,10 @@ pub(super) fn complete_workflow(attribs: CompleteWorkflowExecution) -> NewMachin
4141
.pop()
4242
{
4343
Some(CompleteWFCommand::AddCommand(c)) => c,
44-
_ => panic!("complete wf machine on_schedule must produce command"),
44+
unexpected => {
45+
dbg_panic!("complete wf machine on_schedule must produce command: {unexpected:?}");
46+
panic!("complete wf machine on_schedule must produce command");
47+
}
4548
};
4649
NewMachineWithCommand {
4750
command: add_cmd,

crates/sdk/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ version = "0.1"
4242
path = "../client"
4343
version = "0.1"
4444

45+
[features]
46+
default = []
47+
antithesis_assertions = ["temporalio-sdk-core/antithesis_assertions"]
48+
4549
[lints]
4650
workspace = true

0 commit comments

Comments
 (0)