taskvisor 0.4.1

Task supervisor for Tokio: restarts background tasks on failure with exponential backoff and jitter, graceful shutdown, and lifecycle events
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! Controller admission-policy integration tests (requires feature `controller`).

mod common;

use std::sync::{Arc, Mutex};
use std::time::Duration;

use common::*;
use taskvisor::prelude::*;
use taskvisor::{ControllerConfig, ControllerError, ControllerSpec, SlotStatusKind};

fn served_controller(cfg: ControllerConfig) -> (SupervisorHandle, Arc<EventCollector>) {
    let collector = EventCollector::new();
    let subs: Vec<Arc<dyn Subscribe>> = vec![collector.clone() as Arc<dyn Subscribe>];
    let sup = Supervisor::builder(SupervisorConfig {
        grace: Duration::from_secs(5),
        ..Default::default()
    })
    .with_subscribers(subs)
    .with_controller(cfg)
    .build();
    (sup.serve(), collector)
}

fn logging_once(name: &str, log: Arc<Mutex<Vec<String>>>) -> TaskRef {
    let n = name.to_string();
    TaskFn::arc(name, move |_ctx: TaskContext| {
        let log = log.clone();
        let n = n.clone();
        async move {
            log.lock().unwrap().push(n);
            Ok(())
        }
    })
}

#[tokio::test(flavor = "current_thread")]
async fn submit_and_watch_resolves_completed_for_admitted_task() {
    let (handle, _collector) = served_controller(ControllerConfig::default());

    with_timeout(10, async {
        let (id, waiter) = handle
            .submit_and_watch(
                ControllerSpec::queue(TaskSpec::once(make_ok_once("watched-ok"))).with_slot("s"),
            )
            .await
            .expect("submit_and_watch ok");
        assert_eq!(waiter.id(), id);

        let outcome = waiter.wait().await.expect("waiter errored");
        assert!(
            matches!(outcome, TaskOutcome::Completed),
            "an admitted task that succeeds must resolve Completed, got {outcome:?}"
        );

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn submit_and_watch_resolves_rejected_on_drop_if_running() {
    let (handle, _collector) = served_controller(ControllerConfig::default());

    with_timeout(10, async {
        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("occupant-w")))
                    .with_slot("s"),
            )
            .await
            .expect("first submit ok");
        assert!(
            poll_until(Duration::from_secs(2), || async {
                handle.is_alive("occupant-w").await
            })
            .await
        );

        let (_id, waiter) = handle
            .submit_and_watch(
                ControllerSpec::drop_if_running(TaskSpec::restartable(make_coop("dropped-w")))
                    .with_slot("s"),
            )
            .await
            .expect("submit_and_watch accepted into channel");

        match waiter.wait().await.expect("waiter errored") {
            TaskOutcome::Rejected { reason } => {
                assert!(
                    reason.contains("dropped"),
                    "rejection reason must explain why: {reason}"
                );
            }
            other => panic!("expected Rejected, got {other:?}"),
        }

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn submit_and_watch_resolves_rejected_when_removed_from_queue() {
    let (handle, collector) = served_controller(ControllerConfig::default());

    with_timeout(10, async {
        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("occupant-rm")))
                    .with_slot("s"),
            )
            .await
            .expect("first submit ok");
        assert!(
            poll_until(Duration::from_secs(2), || async {
                handle.is_alive("occupant-rm").await
            })
            .await
        );

        let (victim_id, waiter) = handle
            .submit_and_watch(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("queued-victim-w")))
                    .with_slot("s"),
            )
            .await
            .expect("queued submit_and_watch ok");
        assert!(
            poll_until(Duration::from_secs(2), || async {
                collector
                    .find_all(EventKind::ControllerSubmitted)
                    .iter()
                    .any(|e| e.id == Some(victim_id))
            })
            .await
        );
        handle.remove(victim_id).expect("remove accepted");

        match waiter.wait().await.expect("waiter errored") {
            TaskOutcome::Rejected { reason } => {
                assert_eq!(&*reason, "removed_from_queue");
            }
            other => panic!("expected Rejected, got {other:?}"),
        }

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn submit_returns_task_id_carried_by_events() {
    let (handle, collector) = served_controller(ControllerConfig::default());

    with_timeout(10, async {
        let id = handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("id-task"))).with_slot("s"),
            )
            .await
            .expect("submit ok");

        assert!(
            poll_until(Duration::from_secs(2), || async {
                collector
                    .find_all(EventKind::TaskAdded)
                    .iter()
                    .any(|e| e.id == Some(id))
            })
            .await,
            "TaskAdded must carry the id returned by submit()"
        );
        assert!(
            collector
                .find_all(EventKind::ControllerSubmitted)
                .iter()
                .any(|e| e.id == Some(id)),
            "ControllerSubmitted must carry the submitted id"
        );

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn rejected_submission_event_carries_its_id() {
    let (handle, collector) = served_controller(ControllerConfig::default());

    with_timeout(10, async {
        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("occupant-rej")))
                    .with_slot("s"),
            )
            .await
            .expect("first submit ok");
        assert!(
            poll_until(Duration::from_secs(2), || async {
                handle.is_alive("occupant-rej").await
            })
            .await
        );

        let rejected_id = handle
            .submit(
                ControllerSpec::drop_if_running(TaskSpec::restartable(make_coop("dropped")))
                    .with_slot("s"),
            )
            .await
            .expect("submit accepted into channel");

        assert!(
            poll_until(Duration::from_secs(2), || async {
                collector
                    .find_all(EventKind::ControllerRejected)
                    .iter()
                    .any(|e| e.id == Some(rejected_id))
            })
            .await,
            "ControllerRejected must carry the id of the dropped submission"
        );

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn remove_of_queued_submission_purges_it_before_start() {
    let (handle, collector) = served_controller(ControllerConfig::default());

    with_timeout(10, async {
        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("occupant-q")))
                    .with_slot("s"),
            )
            .await
            .expect("first submit ok");
        assert!(
            poll_until(Duration::from_secs(2), || async {
                handle.is_alive("occupant-q").await
            })
            .await
        );

        let victim_id = handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("queued-victim")))
                    .with_slot("s"),
            )
            .await
            .expect("second submit ok");

        assert!(
            poll_until(Duration::from_secs(2), || async {
                collector
                    .find_all(EventKind::ControllerSubmitted)
                    .iter()
                    .any(|e| e.id == Some(victim_id))
            })
            .await,
            "queued submission must be confirmed before removal"
        );

        handle.remove(victim_id).expect("remove accepted");
        assert!(
            poll_until(Duration::from_secs(2), || async {
                collector
                    .find_all(EventKind::ControllerRejected)
                    .iter()
                    .any(|e| {
                        e.id == Some(victim_id) && e.reason.as_deref() == Some("removed_from_queue")
                    })
            })
            .await,
            "controller must confirm the queued spec was purged"
        );

        assert!(
            handle
                .cancel_by_label("occupant-q")
                .await
                .expect("cancel occupant")
        );
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert!(
            collector
                .by_label("queued-victim")
                .iter()
                .all(|e| e.kind != EventKind::TaskStarting),
            "a removed queued submission must never start"
        );

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn shutdown_does_not_start_queued_tasks() {
    let (handle, collector) = served_controller(ControllerConfig::default());

    with_timeout(10, async {
        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("occupant"))).with_slot("s"),
            )
            .await
            .expect("first submit ok");
        assert!(
            poll_until(Duration::from_secs(2), || async {
                handle.is_alive("occupant").await
            })
            .await,
            "occupant must be running before queueing the next task"
        );

        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("queued"))).with_slot("s"),
            )
            .await
            .expect("second submit ok");

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;

    assert!(
        collector.by_label("queued").is_empty()
            || collector
                .by_label("queued")
                .iter()
                .all(|e| e.kind != EventKind::TaskStarting),
        "queued task must not start during shutdown"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn submit_without_controller_via_new_returns_not_configured() {
    let sup = Supervisor::new(SupervisorConfig::default(), vec![]);
    let handle = sup.serve();
    let spec = TaskSpec::once(make_ok_once("t"));
    with_timeout(5, async {
        assert_eq!(
            handle.submit(ControllerSpec::queue(spec.clone())).await,
            Err(ControllerError::NotConfigured)
        );
        assert_eq!(
            handle.try_submit(ControllerSpec::queue(spec)),
            Err(ControllerError::NotConfigured)
        );
        assert!(handle.list().await.is_empty());
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn submit_without_controller_via_plain_builder_returns_not_configured() {
    let sup = Supervisor::builder(SupervisorConfig::default())
        .with_subscribers(vec![])
        .build();
    let handle = sup.serve();
    with_timeout(5, async {
        assert_eq!(
            handle.try_submit(ControllerSpec::drop_if_running(TaskSpec::once(
                make_ok_once("t")
            ))),
            Err(ControllerError::NotConfigured)
        );
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn idle_submit_admits_emits_submitted_then_running_transition() {
    let (handle, collector) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let spec = TaskSpec::restartable(make_coop("runner-7"));
        let id = handle
            .submit(ControllerSpec::queue(spec).with_slot("web"))
            .await
            .unwrap();

        assert!(
            poll_until(Duration::from_secs(3), || async {
                handle.is_alive("runner-7").await
                    && collector.by_label("web").iter().any(|e| {
                        e.kind == EventKind::ControllerSlotTransition
                            && e.reason.as_deref() == Some("admitting→running")
                    })
            })
            .await
        );

        assert!(collector.by_label("web").iter().any(|e| {
            e.kind == EventKind::ControllerSubmitted
                && e.reason
                    .as_deref()
                    .is_some_and(|r| r.contains("status=admitting"))
        }));
        for e in collector.by_label("web") {
            if e.kind == EventKind::ControllerSubmitted {
                assert_eq!(
                    e.id,
                    Some(id),
                    "ControllerSubmitted must carry the submission TaskId"
                );
            }
        }
        assert!(
            collector
                .by_label("runner-7")
                .iter()
                .any(|e| { e.kind == EventKind::TaskStarting && e.id == Some(id) }),
            "the lifecycle must run under the id minted at submit()"
        );

        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn queue_three_drains_in_fifo_order() {
    let (handle, _c) = served_controller(ControllerConfig::default());
    let log = Arc::new(Mutex::new(Vec::<String>::new()));
    with_timeout(10, async {
        for name in ["t1", "t2", "t3"] {
            let spec = TaskSpec::once(logging_once(name, log.clone()));
            handle
                .submit(ControllerSpec::queue(spec).with_slot("q"))
                .await
                .unwrap();
        }
        assert!(
            poll_until(Duration::from_secs(5), || async {
                log.lock().unwrap().len() == 3
            })
            .await
        );
        assert_eq!(*log.lock().unwrap(), vec!["t1", "t2", "t3"]);
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn replace_supersedes_running_latest_wins() {
    let (handle, collector) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let run1 = TaskSpec::restartable(make_coop("run-1"));
        handle
            .submit(ControllerSpec::replace(run1).with_slot("s"))
            .await
            .unwrap();
        assert!(
            poll_until(Duration::from_secs(3), || async {
                handle.is_alive("run-1").await
            })
            .await
        );

        let run2 = TaskSpec::restartable(make_coop("run-2"));
        handle
            .submit(ControllerSpec::replace(run2).with_slot("s"))
            .await
            .unwrap();

        assert!(
            poll_until(Duration::from_secs(4), || async {
                let snap = handle.snapshot().await;
                snap.iter().any(|n| &**n == "run-2") && !snap.iter().any(|n| &**n == "run-1")
            })
            .await,
            "latest-wins: run-2 alive, run-1 gone"
        );

        assert!(collector.by_label("s").iter().any(|e| {
            e.kind == EventKind::ControllerSlotTransition
                && e.reason.as_deref() == Some("running→terminating (replace)")
        }));
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn drop_if_running_rejects_while_busy_silently() {
    let (handle, collector) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let first = TaskSpec::restartable(make_coop("first"));
        handle
            .submit(ControllerSpec::drop_if_running(first).with_slot("s"))
            .await
            .unwrap();
        assert!(
            poll_until(Duration::from_secs(3), || async {
                handle.is_alive("first").await
            })
            .await
        );

        let second = TaskSpec::restartable(make_coop("second"));
        handle
            .submit(ControllerSpec::drop_if_running(second).with_slot("s"))
            .await
            .unwrap();

        assert!(
            poll_until(Duration::from_secs(3), || async {
                collector.by_label("s").iter().any(|e| {
                    e.kind == EventKind::ControllerRejected
                        && e.reason
                            .as_deref()
                            .is_some_and(|r| r.contains("dropped: slot busy"))
                })
            })
            .await
        );
        assert!(
            !handle.is_alive("second").await,
            "busy slot must reject the second task"
        );
        assert!(handle.is_alive("first").await);
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn drop_if_running_admits_when_slot_idle() {
    let (handle, collector) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let solo = TaskSpec::restartable(make_coop("solo"));
        handle
            .submit(ControllerSpec::drop_if_running(solo).with_slot("s"))
            .await
            .unwrap();
        assert!(
            poll_until(Duration::from_secs(3), || async {
                handle.is_alive("solo").await
            })
            .await
        );

        assert!(
            collector
                .by_label("s")
                .iter()
                .any(|e| e.kind == EventKind::ControllerSubmitted)
        );
        assert_eq!(collector.count(EventKind::ControllerRejected), 0);
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn same_name_distinct_slots_both_admitted() {
    let (handle, _c) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let w1 = TaskSpec::restartable(make_coop("w1"));
        let w2 = TaskSpec::restartable(make_coop("w2"));
        handle
            .submit(ControllerSpec::queue(w1).with_slot("s1"))
            .await
            .unwrap();
        handle
            .submit(ControllerSpec::queue(w2).with_slot("s2"))
            .await
            .unwrap();

        assert!(
            poll_until(Duration::from_secs(4), || async {
                handle.is_alive("w1").await && handle.is_alive("w2").await
            })
            .await,
            "distinct slot keys run independently"
        );
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn submit_and_watch_duplicate_name_distinct_slots_resolves_rejected() {
    let (handle, _c) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let first = TaskSpec::restartable(make_coop("dup"));
        handle
            .submit(ControllerSpec::queue(first).with_slot("s1"))
            .await
            .expect("first submit accepted");

        assert!(
            poll_until(Duration::from_secs(4), || async {
                handle.is_alive("dup").await
            })
            .await,
            "first task must be registered before the duplicate is submitted"
        );

        let (_id, waiter) = handle
            .submit_and_watch(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("dup"))).with_slot("s2"),
            )
            .await
            .expect("second submit_and_watch accepted into channel");

        let result = waiter.wait().await;
        assert!(
            matches!(result, Ok(TaskOutcome::Rejected { .. })),
            "duplicate task name in a distinct slot must resolve Ok(Rejected), got {result:?}"
        );

        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn replace_into_idle_slot_behaves_as_plain_admit() {
    let (handle, collector) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let x = TaskSpec::restartable(make_coop("x"));
        handle
            .submit(ControllerSpec::replace(x).with_slot("s"))
            .await
            .unwrap();
        assert!(
            poll_until(Duration::from_secs(3), || async {
                handle.is_alive("x").await
            })
            .await
        );

        assert!(
            poll_until(Duration::from_secs(2), || async {
                collector.by_label("s").iter().any(|e| {
                    e.kind == EventKind::ControllerSlotTransition
                        && e.reason.as_deref() == Some("admitting→running")
                })
            })
            .await
        );
        assert!(collector.by_label("s").iter().all(|e| {
            e.kind != EventKind::ControllerSlotTransition
                || e.reason.as_deref() != Some("running→terminating (replace)")
        }));
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn slot_freed_and_reusable_after_task_completes() {
    let (handle, _collector) = served_controller(ControllerConfig::default());
    with_timeout(10, async {
        let first = TaskSpec::once(make_ok_once("first"));
        handle
            .submit(ControllerSpec::queue(first).with_slot("s"))
            .await
            .unwrap();
        assert!(
            poll_until(Duration::from_secs(3), || async {
                !handle.is_alive("first").await
            })
            .await
        );
        assert!(
            poll_until(Duration::from_secs(3), || async {
                if !handle.is_alive("second").await {
                    let second = TaskSpec::restartable(make_coop("second"));
                    let _ = handle
                        .submit(ControllerSpec::drop_if_running(second).with_slot("s"))
                        .await;
                }
                handle.is_alive("second").await
            })
            .await,
            "freed slot must eventually admit a DropIfRunning submission"
        );
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn queue_full_rejects_with_controller_rejected_event() {
    let (handle, collector) = served_controller(ControllerConfig {
        queue_capacity: 1024,
        max_slot_queue: 1,
    });
    with_timeout(10, async {
        let running = TaskSpec::restartable(make_coop("r"));
        handle
            .submit(ControllerSpec::queue(running).with_slot("s"))
            .await
            .unwrap();
        assert!(
            poll_until(Duration::from_secs(3), || async {
                handle.is_alive("r").await
            })
            .await
        );

        let p1 = TaskSpec::restartable(make_coop("p1"));
        let p2 = TaskSpec::restartable(make_coop("p2"));
        handle
            .submit(ControllerSpec::queue(p1).with_slot("s"))
            .await
            .unwrap();
        handle
            .submit(ControllerSpec::queue(p2).with_slot("s"))
            .await
            .unwrap();
        assert!(
            poll_until(Duration::from_secs(3), || async {
                collector.by_label("s").iter().any(|e| {
                    e.kind == EventKind::ControllerRejected
                        && e.reason
                            .as_deref()
                            .is_some_and(|r| r.contains("queue_full"))
                })
            })
            .await
        );
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn try_submit_full_when_queue_capacity_saturated() {
    let (handle, _c) = served_controller(ControllerConfig {
        queue_capacity: 1,
        max_slot_queue: 100,
    });
    with_timeout(10, async {
        let mut saw_full = false;
        for i in 0..256u32 {
            let spec = TaskSpec::once(make_ok_once("q"));
            let _ = i;
            if let Err(ControllerError::Full) =
                handle.try_submit(ControllerSpec::queue(spec).with_slot("q"))
            {
                saw_full = true;
                break;
            }
        }
        assert!(
            saw_full,
            "saturated intake channel must yield ControllerError::Full"
        );
        let _ = handle.shutdown().await;
    })
    .await;
}

#[tokio::test(flavor = "current_thread")]
async fn controller_snapshot_reports_running_slot_and_queue_depth() {
    let (handle, _collector) = served_controller(ControllerConfig {
        queue_capacity: 16,
        max_slot_queue: 4,
    });

    with_timeout(10, async {
        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("occupant-snap")))
                    .with_slot("s"),
            )
            .await
            .expect("submit occupant ok");
        handle
            .submit(
                ControllerSpec::queue(TaskSpec::restartable(make_coop("queued-snap")))
                    .with_slot("s"),
            )
            .await
            .expect("submit queued ok");

        let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
        let mut observed = false;
        while tokio::time::Instant::now() < deadline {
            if let Some(snap) = handle.controller_snapshot().await
                && let Some(view) = snap.slot("s")
                && view.status == SlotStatusKind::Running
                && view.queue_depth == 1
                && snap.running_count() == 1
                && snap.total_queued() == 1
            {
                observed = true;
                break;
            }
            tokio::time::sleep(Duration::from_millis(20)).await;
        }
        assert!(
            observed,
            "controller_snapshot must report slot 's' Running with queue_depth 1"
        );

        handle.shutdown().await.expect("shutdown ok");
    })
    .await;
}