footing 0.1.4

Feature-gated foundations for Rust backend services
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
729
730
731
732
733
use std::{
    collections::VecDeque,
    sync::{
        Arc, Mutex,
        atomic::{AtomicUsize, Ordering},
    },
    time::{Duration, Instant},
};

use async_trait::async_trait;
use tokio::time;

use crate::{
    clock::{Clock, ManualClock},
    result::{ClassifyError, ErrorKind},
};

use super::*;

fn lease_config(lease_duration: Duration, renew_after: Duration) -> WorkerLeaseConfig {
    WorkerLeaseConfig::new(lease_duration, renew_after)
        .expect("test lease configuration should be valid")
}

fn worker_config(poll_interval: Duration, lease: Option<WorkerLeaseConfig>) -> WorkerConfig {
    WorkerSettings::default()
        .with_poll_interval(poll_interval)
        .with_lease(lease.map(WorkerLeaseSettings::from))
        .build()
        .expect("test worker settings should be valid")
}

fn outbox_config(
    update: impl FnOnce(OutboxWorkerSettings) -> OutboxWorkerSettings,
) -> OutboxWorkerConfig {
    update(OutboxWorkerSettings::default())
        .build()
        .expect("test outbox settings should be valid")
}

struct CountingWorker {
    calls: Arc<AtomicUsize>,
}

struct NamedWorker(&'static str);

struct SlowWorker {
    calls: Arc<AtomicUsize>,
    duration: Duration,
}

struct TestBatchJob;

#[derive(Debug, Clone)]
struct TestOutboxItem {
    id: String,
    attempts: u32,
    action: TestOutboxAction,
}

#[derive(Debug, Clone, Copy)]
enum TestOutboxAction {
    Succeed,
    Skip,
    Retry,
    HandlerError,
    Slow,
}

#[derive(Debug, Clone, Default)]
struct TestOutboxStore {
    items: Arc<Mutex<VecDeque<TestOutboxItem>>>,
    states: Arc<Mutex<Vec<(String, OutboxItemState)>>>,
    dead_letters: Arc<Mutex<Vec<String>>>,
}

#[derive(Debug, Clone, Copy)]
struct TestOutboxHandler;

#[async_trait]
impl Worker for CountingWorker {
    fn name(&self) -> &str {
        "counting"
    }

    async fn run_once(&self) -> Result<WorkerTick, BoxWorkerError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        Ok(WorkerTick::new(1).with_next_delay(Duration::from_millis(1)))
    }
}

#[async_trait]
impl Worker for NamedWorker {
    fn name(&self) -> &str {
        self.0
    }

    async fn run_once(&self) -> Result<WorkerTick, BoxWorkerError> {
        Ok(WorkerTick::new(1).with_next_delay(Duration::from_millis(1)))
    }
}

#[async_trait]
impl Worker for SlowWorker {
    fn name(&self) -> &str {
        "slow"
    }

    async fn run_once(&self) -> Result<WorkerTick, BoxWorkerError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        time::sleep(self.duration).await;
        Ok(WorkerTick::new(1).with_next_delay(Duration::from_secs(1)))
    }
}

#[async_trait]
impl BatchWorkerJob for TestBatchJob {
    fn name(&self) -> &str {
        "test batch"
    }

    async fn run_batch(&self) -> Result<BatchRunSummary, BoxWorkerError> {
        Ok(BatchRunSummary::new()
            .with_claimed(5)
            .with_succeeded(2)
            .with_retrying(1)
            .with_failed(1)
            .with_skipped(1)
            .with_next_delay(Duration::from_millis(5)))
    }
}

impl OutboxItem for TestOutboxItem {
    fn outbox_id(&self) -> String {
        self.id.clone()
    }

    fn outbox_attempts(&self) -> u32 {
        self.attempts
    }
}

#[async_trait]
impl OutboxStore<TestOutboxItem> for TestOutboxStore {
    async fn claim_due(&self, claim: OutboxClaim) -> Result<Vec<TestOutboxItem>, BoxWorkerError> {
        let mut items = self.items.lock().unwrap();
        Ok((0..claim.limit())
            .filter_map(|_| items.pop_front())
            .collect())
    }

    async fn record_state(
        &self,
        item: &TestOutboxItem,
        state: OutboxItemState,
    ) -> Result<(), BoxWorkerError> {
        self.states.lock().unwrap().push((item.id.clone(), state));
        Ok(())
    }

    async fn record_dead_letter(
        &self,
        item: &TestOutboxItem,
        reason: &str,
    ) -> Result<(), BoxWorkerError> {
        self.dead_letters
            .lock()
            .unwrap()
            .push(format!("{}:{reason}", item.id));
        Ok(())
    }
}

#[async_trait]
impl OutboxHandler<TestOutboxItem> for TestOutboxHandler {
    async fn handle(&self, item: &TestOutboxItem) -> Result<OutboxItemState, BoxWorkerError> {
        match item.action {
            TestOutboxAction::Succeed => Ok(OutboxItemState::Succeeded),
            TestOutboxAction::Skip => Ok(OutboxItemState::skipped("not applicable")),
            TestOutboxAction::Retry => {
                Ok(OutboxItemState::retry("temporary", Duration::from_secs(3)))
            }
            TestOutboxAction::HandlerError => Err("delivery failed".into()),
            TestOutboxAction::Slow => {
                time::sleep(Duration::from_millis(50)).await;
                Ok(OutboxItemState::Succeeded)
            }
        }
    }
}

#[test]
fn backoff_grows_until_cap() {
    let config = BackoffConfig::new(Duration::from_secs(1), Duration::from_secs(3), 2)
        .expect("test backoff should be valid");
    let mut backoff = RetryBackoff::new(config);

    assert_eq!(backoff.record_failure(), Duration::from_secs(1));
    assert_eq!(backoff.record_failure(), Duration::from_secs(2));
    assert_eq!(backoff.record_failure(), Duration::from_secs(3));
    backoff.reset();
    assert_eq!(backoff.record_failure(), Duration::from_secs(1));
}

#[cfg(feature = "settings")]
#[test]
fn worker_settings_adapt_to_runtime_config() {
    let settings = WorkerSettings {
        poll_interval: Duration::from_secs(5),
        failure_backoff: BackoffSettings {
            initial_delay: Duration::from_millis(200),
            max_delay: Duration::from_secs(2),
            multiplier: 3,
        },
        lease: Some(WorkerLeaseSettings {
            lease_duration: Duration::from_secs(30),
            renew_after: Duration::from_secs(10),
        }),
        ..WorkerSettings::default()
    };

    settings.validate().expect("settings should validate");
    let config = settings.build().expect("settings should build");

    assert!(config.enabled());
    assert_eq!(config.poll_interval(), Duration::from_secs(5));
    assert_eq!(config.failure_backoff().multiplier(), 3);
    assert_eq!(
        config
            .lease()
            .expect("lease should be configured")
            .renew_after(),
        Duration::from_secs(10)
    );
}

#[cfg(feature = "settings")]
#[test]
fn worker_settings_report_config_paths() {
    let settings = WorkerSettings {
        poll_interval: Duration::ZERO,
        failure_backoff: BackoffSettings {
            initial_delay: Duration::ZERO,
            ..BackoffSettings::default()
        },
        lease: Some(WorkerLeaseSettings::new(
            Duration::MAX,
            Duration::from_secs(1),
        )),
        ..WorkerSettings::default()
    };

    let error = settings
        .validate_at("email.worker")
        .expect_err("invalid worker settings should report paths");
    let paths = error
        .issues()
        .iter()
        .map(|issue| issue.path())
        .collect::<Vec<_>>();

    assert!(paths.contains(&"email.worker.poll_interval"));
    assert!(paths.contains(&"email.worker.failure_backoff.initial_delay"));
    assert!(paths.contains(&"email.worker.lease.lease_duration"));
}

#[tokio::test]
async fn batch_worker_maps_outbox_summary_to_tick() {
    let worker = BatchWorker::new(TestBatchJob);

    let tick = worker.run_once().await.expect("batch should run");

    assert_eq!(tick.processed(), 5);
    assert_eq!(tick.next_delay(), Some(Duration::from_millis(5)));
}

#[test]
fn outbox_state_debug_redacts_handler_reasons() {
    let reason = "private-handler-error-detail";
    let states = [
        OutboxItemState::skipped(reason),
        OutboxItemState::retry(reason, Duration::from_secs(1)),
        OutboxItemState::failed(reason),
    ];

    let output = format!("{states:?}");
    assert!(output.contains("[REDACTED]"));
    assert!(!output.contains(reason));
}

#[tokio::test]
async fn outbox_job_claims_handles_and_records_states() {
    let store = TestOutboxStore::default();
    store.items.lock().unwrap().extend([
        TestOutboxItem {
            id: "one".to_owned(),
            attempts: 0,
            action: TestOutboxAction::Succeed,
        },
        TestOutboxItem {
            id: "two".to_owned(),
            attempts: 0,
            action: TestOutboxAction::Skip,
        },
        TestOutboxItem {
            id: "three".to_owned(),
            attempts: 0,
            action: TestOutboxAction::Retry,
        },
        TestOutboxItem {
            id: "four".to_owned(),
            attempts: 4,
            action: TestOutboxAction::HandlerError,
        },
    ]);
    let job = OutboxJob::new(
        "test outbox",
        store.clone(),
        TestOutboxHandler,
        outbox_config(|settings| settings.with_batch_size(10)),
    );

    let summary = job.run_batch().await.expect("outbox should run");

    assert_eq!(summary.claimed(), 4);
    assert_eq!(summary.succeeded(), 1);
    assert_eq!(summary.skipped(), 1);
    assert_eq!(summary.retrying(), 1);
    assert_eq!(summary.failed(), 1);
    assert_eq!(summary.dead_lettered(), 1);
    assert_eq!(summary.next_delay(), Some(Duration::from_secs(3)));

    let states = store.states.lock().unwrap();
    assert_eq!(states.len(), 4);
    assert!(matches!(states[0].1, OutboxItemState::Succeeded));
    assert!(matches!(states[1].1, OutboxItemState::Skipped { .. }));
    assert!(matches!(states[2].1, OutboxItemState::Retry { .. }));
    assert!(matches!(states[3].1, OutboxItemState::Failed { .. }));
    assert_eq!(store.dead_letters.lock().unwrap().len(), 1);
}

#[tokio::test]
async fn outbox_job_applies_item_timeout_and_batch_timeout() {
    let store = TestOutboxStore::default();
    store.items.lock().unwrap().extend([
        TestOutboxItem {
            id: "slow".to_owned(),
            attempts: 4,
            action: TestOutboxAction::Slow,
        },
        TestOutboxItem {
            id: "never-started".to_owned(),
            attempts: 0,
            action: TestOutboxAction::Succeed,
        },
    ]);
    let job = OutboxJob::new(
        "test outbox",
        store,
        TestOutboxHandler,
        outbox_config(|settings| {
            settings
                .with_batch_size(10)
                .with_item_timeout(Some(Duration::from_millis(1)))
                .with_batch_timeout(Some(Duration::from_millis(1)))
        }),
    );

    let summary = job.run_batch().await.expect("outbox should run");

    assert_eq!(summary.failed(), 1);
    assert!(summary.batch_timed_out());
    assert_eq!(summary.next_delay(), Some(Duration::ZERO));
}

#[cfg(feature = "settings")]
#[test]
fn outbox_worker_settings_adapt_to_runtime_config() {
    let settings = OutboxWorkerSettings::default()
        .with_batch_size(25)
        .with_lease_duration(Duration::from_secs(120))
        .with_max_attempts(7)
        .with_retry_backoff(BackoffSettings::new(
            Duration::from_secs(2),
            Duration::from_secs(30),
            2,
        ))
        .with_item_timeout(Some(Duration::from_secs(10)))
        .with_batch_timeout(Some(Duration::from_secs(60)));

    settings.validate().expect("settings should validate");
    let config = settings.build().expect("settings should build");

    assert_eq!(config.batch_size(), 25);
    assert_eq!(config.lease_duration(), Duration::from_secs(120));
    assert_eq!(config.retry_policy().max_attempts(), 7);
    assert_eq!(
        config.retry_policy().delay_for_attempts(3),
        Duration::from_secs(8)
    );
    assert_eq!(config.item_timeout(), Some(Duration::from_secs(10)));
    assert_eq!(config.batch_timeout(), Some(Duration::from_secs(60)));
}

#[test]
fn worker_errors_are_classified() {
    assert_eq!(
        WorkerConfigError::InvalidOutboxItemTimeout.error_kind(),
        ErrorKind::Validation
    );
    assert_eq!(
        WorkerConfigError::InvalidOutboxBatchTimeout.error_code(),
        "outbox_invalid_batch_timeout"
    );
    assert_eq!(
        WorkerRunError::MissingLeaseStore.error_kind(),
        ErrorKind::Validation
    );
    assert_eq!(
        WorkerRunError::LeaseLost.error_kind(),
        ErrorKind::Unavailable
    );
    assert_eq!(
        WorkerLeaseError::LockPoisoned.error_kind(),
        ErrorKind::Unavailable
    );
}

#[tokio::test]
async fn worker_loop_stops_on_shutdown() {
    let (trigger, token) = shutdown_channel();
    let calls = Arc::new(AtomicUsize::new(0));
    let worker = CountingWorker {
        calls: Arc::clone(&calls),
    };

    let handle = tokio::spawn(run_worker_until_shutdown(
        worker,
        worker_config(Duration::from_millis(1), None),
        token,
    ));
    time::sleep(Duration::from_millis(5)).await;
    trigger.shutdown();
    let summary = handle.await.unwrap().unwrap();

    assert!(summary.successes() > 0);
    assert_eq!(summary.failures(), 0);
    assert_eq!(summary.last_outcome(), Some(WorkerTickOutcome::Success));
    assert_eq!(summary.state(), WorkerRunState::Healthy);
    assert!(calls.load(Ordering::SeqCst) > 0);
}

#[tokio::test]
async fn worker_shutdown_enforces_the_configured_deadline() {
    let (trigger, token) = shutdown_channel();
    let calls = Arc::new(AtomicUsize::new(0));
    let worker = SlowWorker {
        calls: Arc::clone(&calls),
        duration: Duration::from_secs(1),
    };
    let config = WorkerSettings::default()
        .with_poll_interval(Duration::from_millis(1))
        .with_shutdown_timeout(Duration::from_millis(5))
        .build()
        .expect("worker settings should be valid");

    let handle = tokio::spawn(run_worker_until_shutdown(worker, config, token));
    while calls.load(Ordering::SeqCst) == 0 {
        time::sleep(Duration::from_millis(1)).await;
    }
    trigger.shutdown();

    assert!(matches!(
        handle.await.unwrap(),
        Err(WorkerRunError::ShutdownTimedOut)
    ));
}

#[tokio::test]
async fn local_worker_lease_store_acquires_renews_and_releases() {
    let store = LocalWorkerLeaseStore::new();
    let config = lease_config(Duration::from_secs(30), Duration::from_secs(10));

    let lease = store
        .try_acquire("email", "node-a", config)
        .await
        .expect("lease store should work")
        .expect("lease should be acquired");
    assert!(
        store
            .try_acquire("email", "node-b", config)
            .await
            .expect("lease store should work")
            .is_none()
    );

    let renewed = store
        .renew(&lease, config)
        .await
        .expect("lease store should work")
        .expect("lease should renew");
    assert_eq!(renewed.worker(), "email");

    store
        .release(&renewed)
        .await
        .expect("lease store should work");
    assert!(
        store
            .try_acquire("email", "node-b", config)
            .await
            .expect("lease store should work")
            .is_some()
    );
}

#[tokio::test]
async fn local_worker_lease_store_rejects_invalid_inputs() {
    let store = LocalWorkerLeaseStore::new();

    assert!(matches!(
        WorkerLease::new(
            " email ",
            "node-a",
            1,
            Instant::now() + Duration::from_secs(30)
        ),
        Err(WorkerLeaseError::InvalidConfig(
            WorkerConfigError::InvalidWorkerName { .. }
        ))
    ));
    assert!(matches!(
        store
            .try_acquire(
                "",
                "node-a",
                lease_config(Duration::from_secs(30), Duration::from_secs(10)),
            )
            .await,
        Err(WorkerLeaseError::InvalidConfig(
            WorkerConfigError::InvalidWorkerName { .. }
        ))
    ));
    assert!(matches!(
        store
            .try_acquire(
                "email",
                "",
                lease_config(Duration::from_secs(30), Duration::from_secs(10)),
            )
            .await,
        Err(WorkerLeaseError::InvalidConfig(
            WorkerConfigError::InvalidLeaseOwner { .. }
        ))
    ));
    assert!(matches!(
        WorkerLeaseConfig::new(Duration::ZERO, Duration::from_secs(10)),
        Err(WorkerConfigError::InvalidLeaseDuration)
    ));
    assert!(matches!(
        WorkerLeaseConfig::new(Duration::MAX, Duration::from_secs(1)),
        Err(WorkerConfigError::InvalidLeaseDuration)
    ));
}

#[tokio::test]
async fn worker_lease_renewal_uses_injected_runtime_clock() {
    let clock = ManualClock::new();
    let store = LocalWorkerLeaseStore::with_clock(clock.shared());
    let config = lease_config(Duration::from_secs(30), Duration::from_secs(10));
    let lease = store
        .try_acquire("email", "node-a", config)
        .await
        .expect("lease store should work")
        .expect("lease should be acquired");

    let unchanged = ensure_worker_lease(
        &store,
        Some(lease.clone()),
        "email",
        "node-a",
        config,
        clock.now(),
        None,
    )
    .await
    .expect("lease check should work")
    .expect("lease should still be held");
    assert_eq!(unchanged.expires_at(), lease.expires_at());

    clock.advance(Duration::from_secs(25));
    let renewed = ensure_worker_lease(
        &store,
        Some(unchanged),
        "email",
        "node-a",
        config,
        clock.now(),
        None,
    )
    .await
    .expect("lease check should work")
    .expect("lease should renew");

    assert!(renewed.expires_at() > lease.expires_at());
}

#[tokio::test]
async fn leased_worker_requires_store() {
    let (_, token) = shutdown_channel();
    let result = run_worker_until_shutdown(
        CountingWorker {
            calls: Arc::new(AtomicUsize::new(0)),
        },
        worker_config(
            WorkerConfig::default().poll_interval(),
            Some(lease_config(
                Duration::from_secs(30),
                Duration::from_secs(10),
            )),
        ),
        token,
    )
    .await;

    assert!(matches!(result, Err(WorkerRunError::MissingLeaseStore)));
}

#[tokio::test]
async fn worker_loop_rejects_invalid_worker_name_and_lease_owner() {
    let (_, token) = shutdown_channel();
    let result = run_worker_until_shutdown(NamedWorker(""), WorkerConfig::default(), token).await;

    assert!(matches!(
        result,
        Err(WorkerRunError::InvalidConfig(
            WorkerConfigError::InvalidWorkerName { .. }
        ))
    ));

    let store = Arc::new(LocalWorkerLeaseStore::new());
    let (_, token) = shutdown_channel();
    let result = run_worker_until_shutdown_with_options(
        NamedWorker("email"),
        worker_config(
            WorkerConfig::default().poll_interval(),
            Some(lease_config(
                Duration::from_secs(30),
                Duration::from_secs(10),
            )),
        ),
        token,
        WorkerRunOptions::new()
            .with_lease_store("", Arc::clone(&store) as Arc<dyn WorkerLeaseStore>),
    )
    .await;

    assert!(matches!(
        result,
        Err(WorkerRunError::InvalidConfig(
            WorkerConfigError::InvalidLeaseOwner { .. }
        ))
    ));
}

#[tokio::test]
async fn leased_worker_runs_and_releases_on_shutdown() {
    let store = Arc::new(LocalWorkerLeaseStore::new());
    let (trigger, token) = shutdown_channel();
    let calls = Arc::new(AtomicUsize::new(0));
    let worker = CountingWorker {
        calls: Arc::clone(&calls),
    };
    let config = lease_config(Duration::from_secs(30), Duration::from_secs(10));
    let options = WorkerRunOptions::new()
        .with_lease_store("node-a", Arc::clone(&store) as Arc<dyn WorkerLeaseStore>);

    let handle = tokio::spawn(run_worker_until_shutdown_with_options(
        worker,
        worker_config(Duration::from_millis(1), Some(config)),
        token,
        options,
    ));
    time::sleep(Duration::from_millis(5)).await;
    trigger.shutdown();
    let summary = handle.await.unwrap().unwrap();

    assert!(summary.successes() > 0);
    assert!(calls.load(Ordering::SeqCst) > 0);
    assert!(
        store
            .try_acquire("counting", "node-b", config)
            .await
            .expect("lease store should work")
            .is_some()
    );
}

#[tokio::test]
async fn leased_worker_renews_while_tick_is_running() {
    let store = Arc::new(LocalWorkerLeaseStore::new());
    let (trigger, token) = shutdown_channel();
    let calls = Arc::new(AtomicUsize::new(0));
    let worker = SlowWorker {
        calls: Arc::clone(&calls),
        duration: Duration::from_millis(500),
    };
    let lease_config = lease_config(Duration::from_millis(200), Duration::from_millis(150));
    let options = WorkerRunOptions::new()
        .with_lease_store("node-a", Arc::clone(&store) as Arc<dyn WorkerLeaseStore>);

    let handle = tokio::spawn(run_worker_until_shutdown_with_options(
        worker,
        worker_config(Duration::from_secs(1), Some(lease_config)),
        token,
        options,
    ));

    time::sleep(Duration::from_millis(350)).await;
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    assert!(
        store
            .try_acquire("slow", "node-b", lease_config)
            .await
            .expect("lease store should work")
            .is_none(),
        "another owner must not acquire the lease during a long-running tick"
    );

    trigger.shutdown();
    let summary = handle.await.unwrap().unwrap();
    assert_eq!(summary.successes(), 1);
}