duroxide 0.1.27

Durable code execution framework for Rust
Documentation
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Poison Message Detection Validation Tests
//!
//! These tests validate that providers correctly track attempt counts
//! for poison message detection.

use std::time::Duration;

use crate::INITIAL_EXECUTION_ID;
use crate::provider_validations::ProviderFactory;
use crate::providers::{TagFilter, WorkItem};

use super::ExecutionMetadata;

/// Test that orchestration item attempt_count is 1 on first fetch
pub async fn orchestration_attempt_count_starts_at_one(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a start orchestration
    provider
        .enqueue_for_orchestrator(
            WorkItem::StartOrchestration {
                instance: "poison-test-1".to_string(),
                orchestration: "TestOrch".to_string(),
                input: "{}".to_string(),
                version: Some("1.0.0".to_string()),
                parent_instance: None,
                parent_id: None,
                execution_id: INITIAL_EXECUTION_ID,
            },
            None,
        )
        .await
        .expect("enqueue should succeed");

    // Fetch the item
    let (_item, lock_token, attempt_count) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");

    // First fetch should have attempt_count = 1
    assert_eq!(attempt_count, 1, "First fetch should have attempt_count = 1");

    // Ack the item
    provider
        .ack_orchestration_item(
            &lock_token,
            INITIAL_EXECUTION_ID,
            vec![],
            vec![],
            vec![],
            ExecutionMetadata::default(),
            vec![],
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: orchestration attempt_count starts at 1");
}

/// Test that orchestration attempt_count increments on each fetch after abandon
pub async fn orchestration_attempt_count_increments_on_refetch(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a start orchestration
    provider
        .enqueue_for_orchestrator(
            WorkItem::StartOrchestration {
                instance: "poison-test-2".to_string(),
                orchestration: "TestOrch".to_string(),
                input: "{}".to_string(),
                version: Some("1.0.0".to_string()),
                parent_instance: None,
                parent_id: None,
                execution_id: INITIAL_EXECUTION_ID,
            },
            None,
        )
        .await
        .expect("enqueue should succeed");

    // First fetch - attempt_count = 1
    let (_item1, lock_token1, attempt_count1) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt_count1, 1, "First fetch should have attempt_count = 1");

    // Abandon the item
    provider
        .abandon_orchestration_item(&lock_token1, None, false)
        .await
        .expect("abandon should succeed");

    // Second fetch - attempt_count = 2
    let (_item2, lock_token2, attempt_count2) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt_count2, 2, "Second fetch should have attempt_count = 2");

    // Abandon again
    provider
        .abandon_orchestration_item(&lock_token2, None, false)
        .await
        .expect("abandon should succeed");

    // Third fetch - attempt_count = 3
    let (_item3, lock_token3, attempt_count3) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt_count3, 3, "Third fetch should have attempt_count = 3");

    // Clean up
    provider
        .ack_orchestration_item(
            &lock_token3,
            INITIAL_EXECUTION_ID,
            vec![],
            vec![],
            vec![],
            ExecutionMetadata::default(),
            vec![],
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: orchestration attempt_count increments on refetch");
}

/// Test that worker item attempt_count is 1 on first fetch
pub async fn worker_attempt_count_starts_at_one(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a worker item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "poison-test-3".to_string(),
            execution_id: INITIAL_EXECUTION_ID,
            id: 1,
            name: "TestActivity".to_string(),
            input: "{}".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .expect("enqueue should succeed");

    // Fetch the item
    let (item, token, attempt_count) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");

    // First fetch should have attempt_count = 1
    assert_eq!(attempt_count, 1, "First fetch should have attempt_count = 1");

    // Ack the item
    provider
        .ack_work_item(
            &token,
            Some(WorkItem::ActivityCompleted {
                instance: "poison-test-3".to_string(),
                execution_id: INITIAL_EXECUTION_ID,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await
        .expect("ack should succeed");

    assert!(matches!(item, WorkItem::ActivityExecute { .. }));

    tracing::info!("✓ Test passed: worker attempt_count starts at 1");
}

/// Test that worker attempt_count increments when lock expires and item is refetched
pub async fn worker_attempt_count_increments_on_lock_expiry(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let short_timeout = Duration::from_secs(1);

    // Enqueue a worker item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "poison-test-4".to_string(),
            execution_id: INITIAL_EXECUTION_ID,
            id: 1,
            name: "TestActivity".to_string(),
            input: "{}".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .expect("enqueue should succeed");

    // First fetch with short lock timeout - attempt_count = 1
    let (_item1, _token1, attempt_count1) = provider
        .fetch_work_item(short_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt_count1, 1, "First fetch should have attempt_count = 1");

    // Wait for lock to expire
    tokio::time::sleep(Duration::from_millis(1100)).await;

    // Second fetch after lock expiry - attempt_count = 2
    let (_item2, token2, attempt_count2) = provider
        .fetch_work_item(short_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present after lock expiry");
    assert_eq!(
        attempt_count2, 2,
        "Second fetch after lock expiry should have attempt_count = 2"
    );

    // Clean up
    provider
        .ack_work_item(
            &token2,
            Some(WorkItem::ActivityCompleted {
                instance: "poison-test-4".to_string(),
                execution_id: INITIAL_EXECUTION_ID,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: worker attempt_count increments on lock expiry");
}

/// Test that attempt_count is tracked per message, not globally
pub async fn attempt_count_is_per_message(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue two different worker items
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "poison-test-5a".to_string(),
            execution_id: INITIAL_EXECUTION_ID,
            id: 1,
            name: "Activity1".to_string(),
            input: "{}".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .expect("enqueue should succeed");

    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "poison-test-5b".to_string(),
            execution_id: INITIAL_EXECUTION_ID,
            id: 2,
            name: "Activity2".to_string(),
            input: "{}".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .expect("enqueue should succeed");

    // Fetch first item
    let (item1, token1, attempt1) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt1, 1, "First item first fetch should have attempt_count = 1");

    // Fetch second item
    let (item2, token2, attempt2) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt2, 1, "Second item first fetch should have attempt_count = 1");

    // Both items should have independent attempt counts
    let id1 = match &item1 {
        WorkItem::ActivityExecute { id, .. } => *id,
        _ => panic!("Expected ActivityExecute"),
    };
    let id2 = match &item2 {
        WorkItem::ActivityExecute { id, .. } => *id,
        _ => panic!("Expected ActivityExecute"),
    };
    assert_ne!(id1, id2, "Should be different items");

    // Clean up
    provider
        .ack_work_item(
            &token1,
            Some(WorkItem::ActivityCompleted {
                instance: "poison-test-5a".to_string(),
                execution_id: INITIAL_EXECUTION_ID,
                id: id1,
                result: "done".to_string(),
            }),
        )
        .await
        .expect("ack should succeed");

    provider
        .ack_work_item(
            &token2,
            Some(WorkItem::ActivityCompleted {
                instance: "poison-test-5b".to_string(),
                execution_id: INITIAL_EXECUTION_ID,
                id: id2,
                result: "done".to_string(),
            }),
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: attempt_count is tracked per message");
}

/// Test that abandon_work_item with ignore_attempt=true decrements attempt count
pub async fn abandon_work_item_ignore_attempt_decrements(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a worker item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "poison-test-ignore-1".to_string(),
            execution_id: INITIAL_EXECUTION_ID,
            id: 1,
            name: "TestActivity".to_string(),
            input: "{}".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .expect("enqueue should succeed");

    // First fetch - attempt_count = 1
    let (_item1, token1, attempt1) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt1, 1, "First fetch should have attempt_count = 1");

    // Abandon WITHOUT ignore_attempt
    provider
        .abandon_work_item(&token1, None, false)
        .await
        .expect("abandon should succeed");

    // Second fetch - attempt_count = 2
    let (_item2, token2, attempt2) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt2, 2, "Second fetch should have attempt_count = 2");

    // Abandon WITH ignore_attempt=true - decrements to 1
    provider
        .abandon_work_item(&token2, None, true)
        .await
        .expect("abandon with ignore_attempt should succeed");

    // Third fetch - attempt_count = 2 (1 stored + 1 from new fetch)
    let (_item3, token3, attempt3) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(
        attempt3, 2,
        "Third fetch should have attempt_count = 2 (decremented then re-incremented)"
    );

    // Clean up
    provider
        .ack_work_item(
            &token3,
            Some(WorkItem::ActivityCompleted {
                instance: "poison-test-ignore-1".to_string(),
                execution_id: INITIAL_EXECUTION_ID,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: abandon_work_item ignore_attempt decrements attempt count");
}

/// Test that abandon_orchestration_item with ignore_attempt=true decrements attempt count
pub async fn abandon_orchestration_item_ignore_attempt_decrements(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a start orchestration
    provider
        .enqueue_for_orchestrator(
            WorkItem::StartOrchestration {
                instance: "poison-test-ignore-2".to_string(),
                orchestration: "TestOrch".to_string(),
                input: "{}".to_string(),
                version: Some("1.0.0".to_string()),
                parent_instance: None,
                parent_id: None,
                execution_id: INITIAL_EXECUTION_ID,
            },
            None,
        )
        .await
        .expect("enqueue should succeed");

    // First fetch - attempt_count = 1
    let (_item1, token1, attempt1) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt1, 1, "First fetch should have attempt_count = 1");

    // Abandon WITHOUT ignore_attempt
    provider
        .abandon_orchestration_item(&token1, None, false)
        .await
        .expect("abandon should succeed");

    // Second fetch - attempt_count = 2
    let (_item2, token2, attempt2) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt2, 2, "Second fetch should have attempt_count = 2");

    // Abandon WITH ignore_attempt=true - decrements to 1
    provider
        .abandon_orchestration_item(&token2, None, true)
        .await
        .expect("abandon with ignore_attempt should succeed");

    // Third fetch - attempt_count = 2 (1 stored + 1 from new fetch)
    let (_item3, token3, attempt3) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(
        attempt3, 2,
        "Third fetch should have attempt_count = 2 (decremented then re-incremented)"
    );

    // Clean up
    provider
        .ack_orchestration_item(
            &token3,
            INITIAL_EXECUTION_ID,
            vec![],
            vec![],
            vec![],
            ExecutionMetadata::default(),
            vec![],
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: abandon_orchestration_item ignore_attempt decrements attempt count");
}

/// Test that ignore_attempt never allows attempt count to go below 0
pub async fn ignore_attempt_never_goes_negative(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a worker item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "poison-test-never-neg".to_string(),
            execution_id: INITIAL_EXECUTION_ID,
            id: 1,
            name: "TestActivity".to_string(),
            input: "{}".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .expect("enqueue should succeed");

    // First fetch - attempt_count = 1
    let (_item1, token1, attempt1) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt1, 1, "First fetch should have attempt_count = 1");

    // Abandon with ignore_attempt=true - decrements to 0
    provider
        .abandon_work_item(&token1, None, true)
        .await
        .expect("abandon with ignore_attempt should succeed");

    // Second fetch - attempt_count = 1 (0 + 1)
    let (_item2, token2, attempt2) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt2, 1, "Second fetch should have attempt_count = 1");

    // Abandon with ignore_attempt=true again - should stay at 0, not go negative
    provider
        .abandon_work_item(&token2, None, true)
        .await
        .expect("abandon with ignore_attempt should succeed");

    // Third fetch - attempt_count = 1 (max(0, 0-1) + 1 = 0 + 1 = 1)
    let (_item3, token3, attempt3) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(
        attempt3, 1,
        "Attempt count should not go below 0 - third fetch should have attempt_count = 1"
    );

    // Clean up
    provider
        .ack_work_item(
            &token3,
            Some(WorkItem::ActivityCompleted {
                instance: "poison-test-never-neg".to_string(),
                execution_id: INITIAL_EXECUTION_ID,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: ignore_attempt never allows attempt count to go negative");
}

/// Test that max attempt_count is returned when batch contains messages with different counts
///
/// This validates the scenario where:
/// 1. An orchestration item is fetched, incrementing attempt_count for its messages
/// 2. New messages arrive for the same instance while lock is held
/// 3. Lock expires (or is abandoned) and instance is re-fetched
/// 4. The returned attempt_count should be the MAX across all messages in the batch
pub async fn max_attempt_count_across_message_batch(factory: &dyn ProviderFactory) {
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue initial start message
    provider
        .enqueue_for_orchestrator(
            WorkItem::StartOrchestration {
                instance: "poison-batch-test".to_string(),
                orchestration: "TestOrch".to_string(),
                input: "{}".to_string(),
                version: Some("1.0.0".to_string()),
                parent_instance: None,
                parent_id: None,
                execution_id: INITIAL_EXECUTION_ID,
            },
            None,
        )
        .await
        .expect("enqueue should succeed");

    // First fetch - attempt_count = 1 for the start message
    let (_item1, lock_token1, attempt1) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt1, 1, "First fetch should have attempt_count = 1");
    assert_eq!(_item1.messages.len(), 1, "Should have 1 message");

    // Abandon without processing (simulating a crash/failure)
    provider
        .abandon_orchestration_item(&lock_token1, None, false)
        .await
        .expect("abandon should succeed");

    // Second fetch - attempt_count = 2 for the start message
    let (_item2, lock_token2, attempt2) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(attempt2, 2, "Second fetch should have attempt_count = 2");

    // While lock is held, enqueue a new message (e.g., activity completion)
    // This simulates a message arriving while an orchestration is being processed
    provider
        .enqueue_for_orchestrator(
            WorkItem::ActivityCompleted {
                instance: "poison-batch-test".to_string(),
                execution_id: INITIAL_EXECUTION_ID,
                id: 1,
                result: "activity result".to_string(),
            },
            None,
        )
        .await
        .expect("enqueue should succeed");

    // Abandon again (simulating another failure)
    provider
        .abandon_orchestration_item(&lock_token2, None, false)
        .await
        .expect("abandon should succeed");

    // Third fetch - now we have 2 messages:
    // - Start message: attempt_count = 3
    // - Activity completion: attempt_count = 1 (first time being fetched)
    // The returned attempt_count should be MAX(3, 1) = 3
    let (item3, lock_token3, attempt3) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .expect("fetch should succeed")
        .expect("item should be present");
    assert_eq!(item3.messages.len(), 2, "Should have 2 messages in the batch");
    assert_eq!(
        attempt3, 3,
        "Attempt count should be MAX across messages (3), not the newer message's count (1)"
    );

    // Clean up - ack the orchestration
    provider
        .ack_orchestration_item(
            &lock_token3,
            INITIAL_EXECUTION_ID,
            vec![],
            vec![],
            vec![],
            ExecutionMetadata::default(),
            vec![],
        )
        .await
        .expect("ack should succeed");

    tracing::info!("✓ Test passed: max attempt_count across message batch verified");
}