1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Property-based tests using proptest to verify invariants
#![allow(clippy::unwrap_used)]
#![allow(clippy::clone_on_ref_ptr)]
#![allow(clippy::expect_used)]
use duroxide::runtime::registry::{ActivityRegistry, OrchestrationRegistry};
use duroxide::*;
use proptest::prelude::*;
use std::sync::Arc;
// ============================================================================
// Test Strategy: Generate arbitrary event sequences and verify invariants
// ============================================================================
/// Generate arbitrary orchestration names
fn arb_orch_name() -> impl Strategy<Value = String> {
prop::string::string_regex("[A-Za-z][A-Za-z0-9]{0,20}").unwrap()
}
/// Generate arbitrary inputs (simple strings for testing)
fn arb_input() -> impl Strategy<Value = String> {
prop::string::string_regex("[a-z0-9]{0,10}").unwrap()
}
// ============================================================================
// Property 1: Event Ordering Invariants
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig::with_cases(20))]
/// Property: Event IDs are monotonically increasing within an execution
///
/// This verifies that event_id sequencing is correct.
#[test]
fn prop_event_ids_monotonic(
orch_name in arb_orch_name(),
input in arb_input(),
) {
let result = tokio::runtime::Runtime::new().unwrap().block_on(async {
// Simple orchestration that does one activity
let activities = ActivityRegistry::builder()
.register("TestActivity", |_ctx, input: String| async move {
Ok(format!("done-{input}"))
})
.build();
let orchestrations = OrchestrationRegistry::builder()
.register(
orch_name.clone(),
|ctx: OrchestrationContext, input: String| async move {
let _ = ctx.schedule_activity("TestActivity", input.clone()).await;
Ok(input)
},
)
.build();
let provider = Arc::new(
providers::sqlite::SqliteProvider::new_in_memory()
.await
.expect("provider creation")
);
let rt = runtime::Runtime::start_with_store(
provider.clone(),
activities,
orchestrations,
)
.await;
let client = Client::new(provider.clone());
let instance = format!("test-{orch_name}");
client
.start_orchestration(&instance, &orch_name, input)
.await
.expect("start");
let _ = client
.wait_for_orchestration(&instance, std::time::Duration::from_secs(5))
.await;
// Trigger shutdown
drop(rt);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
// Get history and verify event_id ordering
let history = provider
.read_history(&instance)
.await
.expect("get history");
// Property: Event IDs should be strictly increasing
let mut prev_id = 0u64;
for event in &history {
let event_id = event.event_id();
if event_id <= prev_id {
return Err(format!("Event IDs not monotonic: {event_id} <= {prev_id}"));
}
prev_id = event_id;
}
// Property: Event IDs should be contiguous (no gaps)
let expected_ids: Vec<u64> = (1..=history.len() as u64).collect();
let actual_ids: Vec<u64> = history.iter().map(|e| e.event_id()).collect();
if actual_ids != expected_ids {
return Err(format!("Event IDs not contiguous: {actual_ids:?} != {expected_ids:?}"));
}
Ok(())
});
prop_assert!(result.is_ok(), "{}", result.unwrap_err());
}
}
// ============================================================================
// Property 2: State Machine Invariants
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig::with_cases(20))]
/// Property: An orchestration instance can only reach a terminal state once
///
/// Terminal states: Completed or Failed
#[test]
fn prop_single_terminal_state(
orch_name in arb_orch_name(),
input in arb_input(),
) {
let result = tokio::runtime::Runtime::new().unwrap().block_on(async {
// Simple successful orchestration
let activities = ActivityRegistry::builder().build();
let orchestrations = OrchestrationRegistry::builder()
.register(
orch_name.clone(),
|_ctx: OrchestrationContext, input: String| async move {
Ok(input)
},
)
.build();
let provider = Arc::new(
providers::sqlite::SqliteProvider::new_in_memory()
.await
.expect("provider creation")
);
let rt = runtime::Runtime::start_with_store(
provider.clone(),
activities,
orchestrations,
)
.await;
let client = Client::new(provider.clone());
let instance = format!("test-{orch_name}");
client
.start_orchestration(&instance, &orch_name, input)
.await
.expect("start");
let _ = client
.wait_for_orchestration(&instance, std::time::Duration::from_secs(5))
.await;
drop(rt);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
// Get history and count terminal events
let history = provider
.read_history(&instance)
.await
.expect("get history");
let terminal_count = history.iter().filter(|e| {
matches!(
&e.kind,
EventKind::OrchestrationCompleted { .. }
| EventKind::OrchestrationFailed { .. }
)
}).count();
// Property: Should have exactly one terminal event
if terminal_count != 1 {
return Err(format!("Expected exactly 1 terminal event, got {terminal_count}"));
}
// Property: Terminal event should be the last event
if let Some(last_event) = history.last() {
let is_terminal = matches!(
&last_event.kind,
EventKind::OrchestrationCompleted { .. } | EventKind::OrchestrationFailed { .. }
);
if !is_terminal {
return Err("Last event should be terminal".to_string());
}
}
Ok(())
});
prop_assert!(result.is_ok(), "{}", result.unwrap_err());
}
}
// ============================================================================
// Property 3: Activity Completion Matching
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig::with_cases(20))]
/// Property: Every ActivityScheduled event should have a corresponding completion
///
/// This verifies the activity lifecycle is complete.
#[test]
fn prop_activity_lifecycle(
orch_name in arb_orch_name(),
input in arb_input(),
activity_count in 1..4usize,
) {
let result = tokio::runtime::Runtime::new().unwrap().block_on(async {
// Register multiple activities using method chaining
let mut activities_builder = ActivityRegistry::builder();
for i in 0..activity_count {
let name = format!("Activity{i}");
activities_builder = activities_builder.register(name.clone(), move |_ctx, input: String| async move {
Ok(format!("done-{input}"))
});
}
let activities = activities_builder.build();
// Orchestration calls all activities
let activity_count_clone = activity_count;
let orchestrations = OrchestrationRegistry::builder()
.register(
orch_name.clone(),
move |ctx: OrchestrationContext, input: String| async move {
for i in 0..activity_count_clone {
let name = format!("Activity{i}");
let _ = ctx.schedule_activity(&name, input.clone()).await;
}
Ok(input)
},
)
.build();
let provider = Arc::new(
providers::sqlite::SqliteProvider::new_in_memory()
.await
.expect("provider creation")
);
let rt = runtime::Runtime::start_with_store(
provider.clone(),
activities,
orchestrations,
)
.await;
let client = Client::new(provider.clone());
let instance = format!("test-{orch_name}");
client
.start_orchestration(&instance, &orch_name, input)
.await
.expect("start");
let _ = client
.wait_for_orchestration(&instance, std::time::Duration::from_secs(10))
.await;
drop(rt);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
// Get history
let history = provider
.read_history(&instance)
.await
.expect("get history");
// Collect scheduled activity event IDs
let scheduled_ids: std::collections::HashSet<u64> = history
.iter()
.filter_map(|e| match &e.kind {
EventKind::ActivityScheduled { .. } => Some(e.event_id),
_ => None,
})
.collect();
// Collect completed activity source event IDs
let completed_source_ids: std::collections::HashSet<u64> = history
.iter()
.filter_map(|e| match &e.kind {
EventKind::ActivityCompleted { .. }
| EventKind::ActivityFailed { .. } => e.source_event_id,
_ => None,
})
.collect();
// Property: Every scheduled activity should have a completion
if scheduled_ids != completed_source_ids {
return Err(format!("All scheduled activities should have completions: scheduled={scheduled_ids:?}, completed={completed_source_ids:?}"));
}
// Property: Counts should match
if scheduled_ids.len() != activity_count {
return Err(format!("Activity count mismatch: expected {} got {}", activity_count, scheduled_ids.len()));
}
Ok(())
});
prop_assert!(result.is_ok(), "{}", result.unwrap_err());
}
}
// ============================================================================
// Property 4: History Event Count Bounds
// ============================================================================
proptest! {
#![proptest_config(ProptestConfig::with_cases(20))]
/// Property: History event count has expected bounds
///
/// For a simple orchestration with N activities, we expect:
/// - Minimum events: 1 start + N scheduled + N completed + 1 orch completed = 2N + 2
#[test]
fn prop_history_event_count_bounds(
orch_name in arb_orch_name(),
input in arb_input(),
activity_count in 0..4usize,
) {
let result = tokio::runtime::Runtime::new().unwrap().block_on(async {
let mut activities_builder = ActivityRegistry::builder();
for i in 0..activity_count {
let name = format!("Activity{i}");
activities_builder = activities_builder.register(name.clone(), move |_ctx, input: String| async move {
Ok(format!("done-{input}"))
});
}
let activities = activities_builder.build();
let activity_count_clone = activity_count;
let orchestrations = OrchestrationRegistry::builder()
.register(
orch_name.clone(),
move |ctx: OrchestrationContext, input: String| async move {
for i in 0..activity_count_clone {
let name = format!("Activity{i}");
let _ = ctx.schedule_activity(&name, input.clone()).await;
}
Ok(input)
},
)
.build();
let provider = Arc::new(
providers::sqlite::SqliteProvider::new_in_memory()
.await
.expect("provider creation")
);
let rt = runtime::Runtime::start_with_store(
provider.clone(),
activities,
orchestrations,
)
.await;
let client = Client::new(provider.clone());
let instance = format!("test-{orch_name}");
client
.start_orchestration(&instance, &orch_name, input)
.await
.expect("start");
let _ = client
.wait_for_orchestration(&instance, std::time::Duration::from_secs(10))
.await;
drop(rt);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let history = provider
.read_history(&instance)
.await
.expect("get history");
// Property: Minimum event count bound
// 1 OrchestrationStarted + N ActivityScheduled + N ActivityCompleted + 1 OrchestrationCompleted
let min_expected = 2 + (activity_count * 2);
if history.len() < min_expected {
return Err(format!("History too short: expected at least {} events, got {}", min_expected, history.len()));
}
// Property: Should not have excessive events (basic sanity check)
// Allow some buffer for potential system events
let max_expected = min_expected + 10;
if history.len() > max_expected {
return Err(format!("History too long: expected at most {} events, got {}", max_expected, history.len()));
}
Ok(())
});
prop_assert!(result.is_ok(), "{}", result.unwrap_err());
}
}