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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
use crate::provider_validation::{Event, EventKind, ExecutionMetadata, WorkItem, start_item};
use crate::provider_validations::ProviderFactory;
use crate::providers::TagFilter;
use std::sync::Arc;
use std::time::Duration;

fn provider_lock_timeout<F: ProviderFactory>(factory: &F) -> Duration {
    factory.lock_timeout()
}

/// Test 4.1: Lock Expires After Timeout
/// Goal: Verify locks expire and instance becomes available again.
pub async fn test_lock_expires_after_timeout<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing lock expiration: lock expires after timeout");
    let provider = factory.create_provider().await;
    let lock_timeout = provider_lock_timeout(factory);

    // Setup: create and fetch item
    provider
        .enqueue_for_orchestrator(start_item("instance-A"), None)
        .await
        .unwrap();
    let (_item, lock_token, _attempt_count) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();

    // Verify lock is held
    assert!(
        provider
            .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
            .await
            .unwrap()
            .is_none()
    );

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

    // Instance should be available again
    let (item2, lock_token2, _attempt_count2) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(item2.instance, "instance-A");
    assert_ne!(lock_token2, lock_token, "Should have new lock token");

    // Original lock token should no longer work
    let result = provider
        .ack_orchestration_item(
            &lock_token,
            1,
            vec![],
            vec![],
            vec![],
            ExecutionMetadata::default(),
            vec![],
        )
        .await;
    assert!(result.is_err());
    tracing::info!("✓ Test passed: lock expiration verified");
}

/// Test 4.2: Abandon Releases Lock Immediately
/// Goal: Verify abandon releases lock without waiting for expiration.
pub async fn test_abandon_releases_lock_immediately<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing lock expiration: abandon releases lock immediately");
    let provider = factory.create_provider().await;
    let lock_timeout = provider_lock_timeout(factory);

    // Setup: create and fetch item
    provider
        .enqueue_for_orchestrator(start_item("instance-A"), None)
        .await
        .unwrap();
    let (_item, lock_token, _attempt_count) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();

    // Verify lock is held
    assert!(
        provider
            .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
            .await
            .unwrap()
            .is_none()
    );

    // Abandon the lock
    provider
        .abandon_orchestration_item(&lock_token, None, false)
        .await
        .unwrap();

    // Lock should be released immediately (don't need to wait for expiration)
    let (item2, _lock_token2, _attempt_count2) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(item2.instance, "instance-A");
    tracing::info!("✓ Test passed: abandon releases lock verified");
}

/// Test 4.3: Lock Renewal on Ack
/// Goal: Verify successful ack releases lock immediately.
pub async fn test_lock_renewal_on_ack<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing lock expiration: lock renewal on ack");
    let provider = factory.create_provider().await;
    let lock_timeout = provider_lock_timeout(factory);

    // Setup: create and fetch item
    provider
        .enqueue_for_orchestrator(start_item("instance-A"), None)
        .await
        .unwrap();
    let (_item, _lock_token, _attempt_count) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();

    // Verify lock is held
    assert!(
        provider
            .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
            .await
            .unwrap()
            .is_none()
    );

    // Enqueue another item for the same instance while locked
    provider
        .enqueue_for_orchestrator(start_item("instance-A"), None)
        .await
        .unwrap();

    // Item should not be available yet (lock still held)
    assert!(
        provider
            .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
            .await
            .unwrap()
            .is_none()
    );

    // Ack successfully
    provider
        .ack_orchestration_item(
            &_lock_token,
            1,
            vec![Event::with_event_id(
                1,
                "instance-A".to_string(),
                1,
                None,
                EventKind::OrchestrationStarted {
                    name: "TestOrch".to_string(),
                    version: "1.0.0".to_string(),
                    input: "{}".to_string(),
                    parent_instance: None,
                    parent_id: None,
                    carry_forward_events: None,
                    initial_custom_status: None,
                },
            )],
            vec![],
            vec![],
            ExecutionMetadata::default(),
            vec![],
        )
        .await
        .unwrap();

    // The new item should be available immediately after ack
    let (item2, _lock_token2, _attempt_count2) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(item2.instance, "instance-A");
    tracing::info!("✓ Test passed: lock renewal on ack verified");
}

/// Test 4.4: Concurrent Lock Attempts Respect Expiration
/// Goal: Verify multiple dispatchers respect lock expiration times.
pub async fn test_concurrent_lock_attempts_respect_expiration<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing lock expiration: concurrent lock attempts respect expiration");
    let provider = Arc::new(factory.create_provider().await);
    let lock_timeout = provider_lock_timeout(factory);

    // Setup: create and fetch item
    provider
        .enqueue_for_orchestrator(start_item("instance-A"), None)
        .await
        .unwrap();
    let (_item, _lock_token, _attempt_count) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();

    // Spawn multiple concurrent fetchers
    let handles: Vec<_> = (0..5)
        .map(|i| {
            let provider = provider.clone();
            tokio::spawn(async move {
                // Stagger the attempts slightly
                tokio::time::sleep(Duration::from_millis(i * 50)).await;
                provider
                    .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
                    .await
                    .unwrap()
            })
        })
        .collect();

    // Wait a bit before expiration
    tokio::time::sleep(Duration::from_millis(200)).await;

    // All should still return None (lock held)
    let results = futures::future::join_all(handles).await;
    for result in results {
        assert!(result.unwrap().is_none());
    }

    // Wait for lock expiration
    let wait = lock_timeout.checked_sub(Duration::from_millis(200)).unwrap_or_default() + Duration::from_millis(100);
    tokio::time::sleep(wait).await;

    // Now one should succeed
    let (item2, _lock_token2, _attempt_count2) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(item2.instance, "instance-A");
    tracing::info!("✓ Test passed: concurrent lock attempts respect expiration verified");
}

/// Test 4.5: Worker Lock Renewal Success
/// Goal: Verify worker lock can be renewed with valid token
pub async fn test_worker_lock_renewal_success<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing worker lock renewal: renewal succeeds with valid token");
    let provider = factory.create_provider().await;
    let lock_timeout = provider_lock_timeout(factory);

    use crate::providers::{TagFilter, WorkItem};

    // Enqueue and fetch work item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "test-instance".to_string(),
            execution_id: 1,
            id: 1,
            name: "TestActivity".to_string(),
            input: "test".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .unwrap();

    let (_item, token, _) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();
    tracing::info!("Fetched work item with lock token: {}", token);

    // Verify item is locked (can't fetch again)
    assert!(
        provider
            .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
            .await
            .unwrap()
            .is_none()
    );

    // Wait a bit to simulate activity in progress
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Renew lock
    provider.renew_work_item_lock(&token, lock_timeout).await.unwrap();
    tracing::info!("Successfully renewed lock");

    // Item should still be locked
    assert!(
        provider
            .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
            .await
            .unwrap()
            .is_none()
    );

    tracing::info!("✓ Test passed: worker lock renewal success verified");
}

/// Test 4.6: Worker Lock Renewal Invalid Token
/// Goal: Verify renewal fails with invalid token
pub async fn test_worker_lock_renewal_invalid_token<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing worker lock renewal: renewal fails with invalid token");
    let provider = factory.create_provider().await;

    // Try to renew with invalid token
    let result = provider
        .renew_work_item_lock("invalid-token-123", Duration::from_secs(30))
        .await;
    assert!(result.is_err(), "Should fail with invalid token");
    tracing::info!("✓ Test passed: invalid token rejection verified");
}

/// Test 4.7: Worker Lock Renewal After Expiration
/// Goal: Verify renewal fails after lock expires
pub async fn test_worker_lock_renewal_after_expiration<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing worker lock renewal: renewal fails after expiration");
    let provider = factory.create_provider().await;

    use crate::providers::{TagFilter, WorkItem};

    // Enqueue and fetch work item with short timeout
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "test-instance".to_string(),
            execution_id: 1,
            id: 1,
            name: "TestActivity".to_string(),
            input: "test".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .unwrap();

    let short_timeout = Duration::from_secs(1);
    let (_item, token, _) = provider
        .fetch_work_item(short_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();
    tracing::info!("Fetched work item with 1s timeout");

    // Wait for lock to expire
    tokio::time::sleep(factory.lock_timeout() + Duration::from_millis(100)).await;

    // Try to renew expired lock
    let result = provider.renew_work_item_lock(&token, factory.lock_timeout()).await;
    assert!(result.is_err(), "Should fail to renew expired lock");
    tracing::info!("✓ Test passed: expired lock renewal rejection verified");
}

/// Test 4.8: Worker Lock Renewal Extends Timeout
/// Goal: Verify renewal properly extends lock timeout
pub async fn test_worker_lock_renewal_extends_timeout<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing worker lock renewal: renewal extends timeout");
    let provider = Arc::new(factory.create_provider().await);
    let lock_timeout = factory.lock_timeout();

    use crate::provider_validation::start_item;
    use crate::providers::{ExecutionMetadata, TagFilter, WorkItem};
    use crate::{Event, EventKind, INITIAL_EVENT_ID, INITIAL_EXECUTION_ID};

    let instance = "test-lock-renewal-extends";

    // 1. Create a running orchestration (required for renewal to work)
    provider
        .enqueue_for_orchestrator(start_item(instance), None)
        .await
        .unwrap();
    let (_item, orch_token, _) = provider
        .fetch_orchestration_item(Duration::from_secs(30), Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();

    // Create the ActivityExecute work item to enqueue
    let activity_item = WorkItem::ActivityExecute {
        instance: instance.to_string(),
        execution_id: INITIAL_EXECUTION_ID,
        id: 1,
        name: "TestActivity".to_string(),
        input: "test".to_string(),
        session_id: None,
        tag: None,
    };

    // Ack with Running status and enqueue the activity
    provider
        .ack_orchestration_item(
            &orch_token,
            INITIAL_EXECUTION_ID,
            vec![Event::with_event_id(
                INITIAL_EVENT_ID,
                instance.to_string(),
                INITIAL_EXECUTION_ID,
                None,
                EventKind::OrchestrationStarted {
                    name: "TestOrch".to_string(),
                    version: "1.0.0".to_string(),
                    input: "{}".to_string(),
                    parent_instance: None,
                    parent_id: None,
                    carry_forward_events: None,
                    initial_custom_status: None,
                },
            )],
            vec![activity_item],
            vec![],
            ExecutionMetadata {
                orchestration_name: Some("TestOrch".to_string()),
                status: Some("Running".to_string()),
                ..Default::default()
            },
            vec![],
        )
        .await
        .unwrap();

    // 2. Fetch the activity work item
    let (_item, token, _) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();
    tracing::info!("Fetched work item with {:?} timeout", lock_timeout);

    // Wait 60% of lock timeout before renewing
    let pre_renewal_wait = lock_timeout.mul_f64(0.6);
    tokio::time::sleep(pre_renewal_wait).await;

    // Renew lock for full timeout duration
    // At t=0.6x: renew extends lock to t=0.6x + 1.0x = expires at t=1.6x from start
    provider.renew_work_item_lock(&token, lock_timeout).await.unwrap();
    tracing::info!("Renewed lock at ~0.6x timeout mark, now expires at ~1.6x");

    // Wait remaining time to reach exactly 1.0x from start
    // Without renewal, lock would have expired here
    // With renewal at 0.6x for 1.0x more, lock expires at 1.6x
    let post_renewal_wait = lock_timeout - pre_renewal_wait;
    tokio::time::sleep(post_renewal_wait).await;

    // Item should still be locked (we're at 1.0x, renewal extended to 1.6x)
    let result = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap();
    assert!(result.is_none(), "Item should still be locked after renewal");

    tracing::info!("✓ Test passed: lock timeout extension verified");
}

/// Test 4.9: Worker Lock Renewal After Ack Fails
/// Goal: Verify renewal fails after item has been acked
pub async fn test_worker_lock_renewal_after_ack<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing worker lock renewal: renewal fails after ack");
    let provider = factory.create_provider().await;

    use crate::providers::{TagFilter, WorkItem};

    // Enqueue and fetch work item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "test-instance".to_string(),
            execution_id: 1,
            id: 1,
            name: "TestActivity".to_string(),
            input: "test".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .unwrap();

    let lock_timeout = factory.lock_timeout();
    let (_item, token, _) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();
    tracing::info!("Fetched work item");

    // Ack the work item
    provider
        .ack_work_item(
            &token,
            Some(WorkItem::ActivityCompleted {
                instance: "test-instance".to_string(),
                execution_id: 1,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await
        .unwrap();
    tracing::info!("Acked work item");

    // Try to renew after ack
    let result = provider.renew_work_item_lock(&token, lock_timeout).await;
    assert!(result.is_err(), "Should fail to renew after ack");
    tracing::info!("✓ Test passed: renewal after ack rejection verified");
}

/// Test: abandon_work_item releases lock immediately
/// Goal: Verify abandoning a work item releases its lock without waiting for expiration.
pub async fn test_abandon_work_item_releases_lock<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing: abandon_work_item releases lock immediately");
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a work item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "test-abandon-work".to_string(),
            execution_id: 1,
            id: 1,
            name: "TestActivity".to_string(),
            input: "test".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .unwrap();

    // Fetch the work item
    let (item, token, _) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();
    assert!(matches!(item, WorkItem::ActivityExecute { .. }));

    // Verify lock is held - no items available
    assert!(
        provider
            .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
            .await
            .unwrap()
            .is_none(),
        "Item should be locked"
    );

    // Abandon the work item
    provider.abandon_work_item(&token, None, false).await.unwrap();

    // Item should be immediately available again
    let (item2, token2, _) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();
    assert!(matches!(item2, WorkItem::ActivityExecute { .. }));
    assert_ne!(token, token2, "Should have new lock token");

    // Clean up
    provider
        .ack_work_item(
            &token2,
            Some(WorkItem::ActivityCompleted {
                instance: "test-abandon-work".to_string(),
                execution_id: 1,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await
        .unwrap();

    tracing::info!("✓ Test passed: abandon_work_item releases lock immediately");
}

/// Test: abandon_work_item with delay prevents immediate refetch
/// Goal: Verify delay parameter makes the item invisible until delay expires.
pub async fn test_abandon_work_item_with_delay<F: ProviderFactory>(factory: &F) {
    tracing::info!("→ Testing: abandon_work_item with delay prevents immediate refetch");
    let provider = factory.create_provider().await;
    let lock_timeout = factory.lock_timeout();

    // Enqueue a work item
    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "test-abandon-delay".to_string(),
            execution_id: 1,
            id: 1,
            name: "TestActivity".to_string(),
            input: "test".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .unwrap();

    // Fetch the work item
    let (_item, token, _) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();

    // Abandon with 500ms delay
    let delay = Duration::from_millis(500);
    provider.abandon_work_item(&token, Some(delay), false).await.unwrap();

    // Item should NOT be immediately available (delayed)
    assert!(
        provider
            .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
            .await
            .unwrap()
            .is_none(),
        "Item should be delayed, not immediately available"
    );

    // Wait for delay to expire
    tokio::time::sleep(delay + Duration::from_millis(100)).await;

    // Now item should be available
    let (item2, token2, _) = provider
        .fetch_work_item(lock_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();
    assert!(matches!(item2, WorkItem::ActivityExecute { .. }));

    // Clean up
    provider
        .ack_work_item(
            &token2,
            Some(WorkItem::ActivityCompleted {
                instance: "test-abandon-delay".to_string(),
                execution_id: 1,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await
        .unwrap();

    tracing::info!("✓ Test passed: abandon_work_item with delay verified");
}

/// G2: Worker ack fails after lock expiry
/// Goal: Verify that ack_work_item fails when the worker lock has expired,
/// even if no other worker has reclaimed the item.
/// (Mirrors test_lock_expiration_during_ack which tests the orchestration side.)
pub async fn test_worker_ack_fails_after_lock_expiry<F: ProviderFactory>(factory: &F) {
    let provider = factory.create_provider().await;

    provider
        .enqueue_for_worker(WorkItem::ActivityExecute {
            instance: "ack-expiry-test".to_string(),
            execution_id: 1,
            id: 1,
            name: "TestActivity".to_string(),
            input: "test".to_string(),
            session_id: None,
            tag: None,
        })
        .await
        .unwrap();

    let short_timeout = Duration::from_secs(1);
    let (_item, token, _) = provider
        .fetch_work_item(short_timeout, Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap()
        .unwrap();

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

    // Ack with the expired token — must fail
    let result = provider
        .ack_work_item(
            &token,
            Some(WorkItem::ActivityCompleted {
                instance: "ack-expiry-test".to_string(),
                execution_id: 1,
                id: 1,
                result: "done".to_string(),
            }),
        )
        .await;
    assert!(result.is_err(), "Ack must fail when worker lock has expired");

    // The item should still be in the queue (not deleted) and refetchable
    let refetch = provider
        .fetch_work_item(Duration::from_secs(5), Duration::ZERO, None, &TagFilter::default())
        .await
        .unwrap();
    assert!(
        refetch.is_some(),
        "Item should still be refetchable after expired ack was rejected"
    );

    tracing::info!("✓ Test passed: worker ack after lock expiry correctly rejected");
}

/// G7: Orchestration lock renewal fails after expiry
/// Goal: Verify renew_orchestration_item_lock fails when the lock has expired.
/// (Mirrors test_worker_lock_renewal_after_expiration which tests the worker side.)
pub async fn test_orchestration_lock_renewal_after_expiration<F: ProviderFactory>(factory: &F) {
    let provider = factory.create_provider().await;
    let lock_timeout = provider_lock_timeout(factory);

    // Enqueue and fetch an orchestration item
    provider
        .enqueue_for_orchestrator(start_item("orch-renewal-expiry"), None)
        .await
        .unwrap();
    let (_item, token, _) = provider
        .fetch_orchestration_item(lock_timeout, Duration::ZERO, None)
        .await
        .unwrap()
        .unwrap();

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

    // Renewal should fail
    let result = provider.renew_orchestration_item_lock(&token, lock_timeout).await;
    assert!(result.is_err(), "Should fail to renew expired orchestration lock");

    tracing::info!("✓ Test passed: orchestration lock renewal after expiry rejected");
}