Skip to content

Commit ac1c742

Browse files
committed
more assertions, macro fix
1 parent 6576790 commit ac1c742

File tree

6 files changed

+80
-18
lines changed

6 files changed

+80
-18
lines changed

crates/sdk-core/src/abstractions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,14 @@ macro_rules! dbg_panic {
427427
($($arg:tt)*) => {{
428428
let message = format!($($arg)*);
429429
error!("{}", message);
430-
crate::antithesis::assert_always_failure(
430+
crate::antithesis::assert_always_failure!(
431431
"dbg_panic invariant triggered",
432432
::serde_json::json!({
433433
"message": message.clone(),
434434
"file": file!(),
435435
"line": line!(),
436436
"module": module_path!(),
437-
}),
437+
})
438438
);
439439
debug_assert!(false, "{}", message);
440440
}};

crates/sdk-core/src/antithesis.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#[cfg(feature = "antithesis_assertions")]
22
use std::sync::OnceLock;
33

4-
#[cfg(feature = "antithesis_assertions")]
5-
use serde_json::Value;
6-
74
/// Ensure Antithesis is initialized exactly once.
85
#[cfg(feature = "antithesis_assertions")]
96
pub(crate) fn ensure_init() {
@@ -17,9 +14,44 @@ pub(crate) fn ensure_init() {
1714
#[inline]
1815
pub(crate) fn ensure_init() {}
1916

20-
/// Record an always-true assertion failure to Antithesis when an invariant is
21-
/// violated.
2217
#[cfg(feature = "antithesis_assertions")]
23-
pub(crate) fn assert_always_failure(name: &str, metadata: Value) {
24-
::antithesis_sdk::assert_always!(false, name, &metadata);
18+
macro_rules! assert_always {
19+
($message:literal, $condition:expr, $details:expr) => {{
20+
$crate::antithesis::ensure_init();
21+
let details: ::serde_json::Value = $details;
22+
::antithesis_sdk::assert_always!($condition, $message, &details);
23+
}};
24+
($message:literal, $condition:expr) => {{
25+
$crate::antithesis::ensure_init();
26+
::antithesis_sdk::assert_always!($condition, $message);
27+
}};
28+
}
29+
30+
#[cfg(not(feature = "antithesis_assertions"))]
31+
macro_rules! assert_always {
32+
($message:literal, $condition:expr, $details:expr) => {{
33+
let _ = ($message, $condition);
34+
let _ = $details;
35+
}};
36+
($message:literal, $condition:expr) => {{
37+
let _ = ($message, $condition);
38+
}};
39+
}
40+
41+
#[cfg(feature = "antithesis_assertions")]
42+
macro_rules! assert_always_failure {
43+
($message:literal, $details:expr) => {{
44+
$crate::antithesis::assert_always!($message, false, $details);
45+
}};
46+
}
47+
48+
#[cfg(not(feature = "antithesis_assertions"))]
49+
macro_rules! assert_always_failure {
50+
($message:literal, $details:expr) => {{
51+
let _ = $message;
52+
let _ = $details;
53+
}};
2554
}
55+
56+
pub(crate) use assert_always;
57+
pub(crate) use assert_always_failure;

crates/sdk-core/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,13 @@ where
9595
crate::antithesis::ensure_init();
9696
let namespace = worker_config.namespace.clone();
9797
if namespace.is_empty() {
98-
#[cfg(feature = "antithesis_assertions")]
99-
crate::antithesis::assert_always_failure(
98+
crate::antithesis::assert_always_failure!(
10099
"worker namespace must not be empty",
101100
::serde_json::json!({
102101
"namespace": namespace.clone(),
103102
"task_queue": worker_config.task_queue.clone(),
104103
"has_identity_override": worker_config.client_identity_override.is_some(),
105-
}),
104+
})
106105
);
107106
bail!("Worker namespace cannot be empty");
108107
}
@@ -119,13 +118,12 @@ where
119118
let sticky_q = sticky_q_name_for_worker(&client_ident, worker_config.max_cached_workflows);
120119

121120
if client_ident.is_empty() {
122-
#[cfg(feature = "antithesis_assertions")]
123-
crate::antithesis::assert_always_failure(
121+
crate::antithesis::assert_always_failure!(
124122
"client identity must not be empty",
125123
::serde_json::json!({
126124
"namespace": namespace.clone(),
127125
"task_queue": worker_config.task_queue.clone(),
128-
}),
126+
})
129127
);
130128
bail!("Client identity cannot be empty. Either lang or user should be setting this value");
131129
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,18 @@ 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+
crate::antithesis::assert_always!(
198+
"activity cancel yields supported command",
199+
false,
200+
::serde_json::json!({
201+
"activity_seq": self.shared_state.attrs.seq,
202+
"unexpected_command": format!("{x:?}"),
203+
"state": self.state().to_string(),
204+
})
205+
);
206+
panic!("Invalid cancel event response {x:?}");
207+
}
197208
})
198209
.collect();
199210
Ok(res)

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,19 @@ impl ChildWorkflowMachine {
496496
| c @ ChildWorkflowCommand::IssueCancelAfterStarted { .. } => {
497497
self.adapt_response(c, None)
498498
}
499-
x => panic!("Invalid cancel event response {x:?}"),
499+
x => {
500+
crate::antithesis::assert_always!(
501+
"child workflow cancel yields supported command",
502+
false,
503+
::serde_json::json!({
504+
"workflow_id": self.shared_state.workflow_id.clone(),
505+
"run_id": self.shared_state.run_id.clone(),
506+
"unexpected_command": format!("{x:?}"),
507+
"state": self.state().to_string(),
508+
})
509+
);
510+
panic!("Invalid cancel event response {x:?}");
511+
}
500512
})
501513
.flatten_ok()
502514
.try_collect()?;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ 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+
crate::antithesis::assert_always!(
46+
"complete workflow scheduling yields command",
47+
false,
48+
::serde_json::json!({
49+
"unexpected": format!("{unexpected:?}"),
50+
})
51+
);
52+
panic!("complete wf machine on_schedule must produce command");
53+
}
4554
};
4655
NewMachineWithCommand {
4756
command: add_cmd,

0 commit comments

Comments
 (0)