fastapi-core 0.3.0

Core types and traits for the FastAPI Rust framework
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! Graceful shutdown coordination for the server.
//!
//! This module provides graceful shutdown using asupersync's structured concurrency:
//!
//! # Shutdown Phases
//!
//! 1. **Stop accepting**: Server stops accepting new connections
//! 2. **Shutdown flag**: New requests receive 503 Service Unavailable
//! 3. **Grace period**: In-flight requests get remaining time to complete
//! 4. **Cancellation**: Remaining requests cancelled after grace period
//! 5. **Shutdown hooks**: Registered cleanup callbacks run
//! 6. **Region close**: Server region fully closed
//!
//! # Signal Handling
//!
//! - SIGTERM/SIGINT triggers graceful shutdown
//! - Second signal forces immediate exit
//! - Custom signals supported via configuration
//!
//! # Example
//!
//! ```ignore
//! use fastapi_core::shutdown::{ShutdownController, GracefulShutdown};
//! use std::time::Duration;
//!
//! async fn run_server() {
//!     let controller = ShutdownController::new();
//!
//!     // Register with signals
//!     controller.listen_for_signals();
//!
//!     // Run with graceful shutdown
//!     let shutdown = GracefulShutdown::new(controller.subscribe())
//!         .grace_period(Duration::from_secs(30));
//!
//!     let result = shutdown.run(server_main()).await;
//!     match result {
//!         ShutdownOutcome::Completed(r) => println!("Server completed: {:?}", r),
//!         ShutdownOutcome::GracefulShutdown => println!("Graceful shutdown"),
//!         ShutdownOutcome::ForcedShutdown => println!("Forced shutdown"),
//!     }
//! }
//! ```

use asupersync::{Budget, CancelReason};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::task::{Context, Poll, Waker};
use std::time::Duration;

// ============================================================================
// Shutdown Phase State Machine
// ============================================================================

/// Current phase of the shutdown process.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ShutdownPhase {
    /// Server is running normally.
    Running = 0,
    /// Stop accepting new connections.
    StopAccepting = 1,
    /// Shutdown flag set; new requests get 503.
    ShutdownFlagged = 2,
    /// Grace period for in-flight requests.
    GracePeriod = 3,
    /// Cancelling remaining requests.
    Cancelling = 4,
    /// Running shutdown hooks.
    RunningHooks = 5,
    /// Server fully stopped.
    Stopped = 6,
}

impl ShutdownPhase {
    /// Returns true if the server should stop accepting connections.
    #[must_use]
    pub fn should_reject_connections(self) -> bool {
        self as u8 >= Self::StopAccepting as u8
    }

    /// Returns true if new requests should receive 503.
    #[must_use]
    pub fn should_reject_requests(self) -> bool {
        self as u8 >= Self::ShutdownFlagged as u8
    }

    /// Returns true if the server is in a shutdown state.
    #[must_use]
    pub fn is_shutting_down(self) -> bool {
        self as u8 >= Self::StopAccepting as u8
    }

    /// Returns true if cleanup is complete.
    #[must_use]
    pub fn is_stopped(self) -> bool {
        self == Self::Stopped
    }
}

impl From<u8> for ShutdownPhase {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Running,
            1 => Self::StopAccepting,
            2 => Self::ShutdownFlagged,
            3 => Self::GracePeriod,
            4 => Self::Cancelling,
            5 => Self::RunningHooks,
            _ => Self::Stopped,
        }
    }
}

// ============================================================================
// Shutdown State (Shared)
// ============================================================================

/// Shared state for shutdown coordination.
struct ShutdownState {
    /// Current shutdown phase.
    phase: AtomicU8,
    /// Flag for forced shutdown (second signal).
    forced: AtomicBool,
    /// Wakers waiting for shutdown.
    wakers: parking_lot::Mutex<Vec<Waker>>,
    /// Shutdown hooks to run.
    hooks: parking_lot::Mutex<Vec<ShutdownHook>>,
    /// In-flight request count.
    in_flight: std::sync::atomic::AtomicUsize,
}

impl ShutdownState {
    fn new() -> Self {
        Self {
            phase: AtomicU8::new(ShutdownPhase::Running as u8),
            forced: AtomicBool::new(false),
            wakers: parking_lot::Mutex::new(Vec::new()),
            hooks: parking_lot::Mutex::new(Vec::new()),
            in_flight: std::sync::atomic::AtomicUsize::new(0),
        }
    }

    fn phase(&self) -> ShutdownPhase {
        ShutdownPhase::from(self.phase.load(Ordering::Acquire))
    }

    fn set_phase(&self, phase: ShutdownPhase) {
        self.phase.store(phase as u8, Ordering::Release);
        self.wake_all();
    }

    fn try_advance_phase(&self, from: ShutdownPhase, to: ShutdownPhase) -> bool {
        self.phase
            .compare_exchange(from as u8, to as u8, Ordering::AcqRel, Ordering::Acquire)
            .is_ok()
    }

    fn is_forced(&self) -> bool {
        self.forced.load(Ordering::Acquire)
    }

    fn set_forced(&self) {
        self.forced.store(true, Ordering::Release);
        self.wake_all();
    }

    fn wake_all(&self) {
        let wakers = std::mem::take(&mut *self.wakers.lock());
        for waker in wakers {
            waker.wake();
        }
    }

    fn register_waker(&self, waker: &Waker) {
        let mut wakers = self.wakers.lock();
        if !wakers.iter().any(|w| w.will_wake(waker)) {
            wakers.push(waker.clone());
        }
    }

    fn increment_in_flight(&self) -> usize {
        self.in_flight.fetch_add(1, Ordering::AcqRel) + 1
    }

    fn decrement_in_flight(&self) -> usize {
        self.in_flight.fetch_sub(1, Ordering::AcqRel) - 1
    }

    fn in_flight_count(&self) -> usize {
        self.in_flight.load(Ordering::Acquire)
    }
}

// ============================================================================
// Shutdown Controller
// ============================================================================

/// Controller for initiating and coordinating shutdown.
///
/// Create one controller per server and share it via [`subscribe()`](Self::subscribe).
#[derive(Clone)]
pub struct ShutdownController {
    state: Arc<ShutdownState>,
}

impl ShutdownController {
    /// Create a new shutdown controller.
    #[must_use]
    pub fn new() -> Self {
        Self {
            state: Arc::new(ShutdownState::new()),
        }
    }

    /// Create a receiver to wait for shutdown signals.
    #[must_use]
    pub fn subscribe(&self) -> ShutdownReceiver {
        ShutdownReceiver {
            state: Arc::clone(&self.state),
        }
    }

    /// Get the current shutdown phase.
    #[must_use]
    pub fn phase(&self) -> ShutdownPhase {
        self.state.phase()
    }

    /// Check if shutdown has been initiated.
    #[must_use]
    pub fn is_shutting_down(&self) -> bool {
        self.state.phase().is_shutting_down()
    }

    /// Check if forced shutdown was requested.
    #[must_use]
    pub fn is_forced(&self) -> bool {
        self.state.is_forced()
    }

    /// Initiate graceful shutdown.
    ///
    /// This begins the shutdown sequence. If shutdown is already in progress,
    /// calling this again triggers forced shutdown.
    pub fn shutdown(&self) {
        let current = self.state.phase();
        if current == ShutdownPhase::Running {
            self.state.set_phase(ShutdownPhase::StopAccepting);
        } else if !self.state.is_forced() {
            // Second signal = forced shutdown
            self.state.set_forced();
        }
    }

    /// Force immediate shutdown without grace period.
    pub fn force_shutdown(&self) {
        self.state.set_forced();
        self.state.set_phase(ShutdownPhase::Cancelling);
    }

    /// Advance to the next shutdown phase.
    ///
    /// Returns `true` if the phase was advanced.
    pub fn advance_phase(&self) -> bool {
        let current = self.state.phase();
        let next = match current {
            ShutdownPhase::Running => ShutdownPhase::StopAccepting,
            ShutdownPhase::StopAccepting => ShutdownPhase::ShutdownFlagged,
            ShutdownPhase::ShutdownFlagged => ShutdownPhase::GracePeriod,
            ShutdownPhase::GracePeriod => ShutdownPhase::Cancelling,
            ShutdownPhase::Cancelling => ShutdownPhase::RunningHooks,
            ShutdownPhase::RunningHooks => ShutdownPhase::Stopped,
            ShutdownPhase::Stopped => return false,
        };
        self.state.try_advance_phase(current, next)
    }

    /// Register a shutdown hook to run during the RunningHooks phase.
    ///
    /// Hooks run in LIFO order (last registered runs first).
    pub fn register_hook<F>(&self, hook: F)
    where
        F: FnOnce() + Send + 'static,
    {
        let mut hooks = self.state.hooks.lock();
        hooks.push(ShutdownHook::Sync(Box::new(hook)));
    }

    /// Register an async shutdown hook.
    pub fn register_async_hook<F, Fut>(&self, hook: F)
    where
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut hooks = self.state.hooks.lock();
        hooks.push(ShutdownHook::AsyncFactory(Box::new(move || {
            Box::pin(hook())
        })));
    }

    /// Pop and return the next shutdown hook to run.
    ///
    /// Returns `None` when all hooks have been run.
    pub fn pop_hook(&self) -> Option<ShutdownHook> {
        let mut hooks = self.state.hooks.lock();
        hooks.pop()
    }

    /// Returns the number of registered hooks.
    #[must_use]
    pub fn hook_count(&self) -> usize {
        self.state.hooks.lock().len()
    }

    /// Track a new in-flight request.
    ///
    /// Returns a guard that decrements the count when dropped.
    #[must_use]
    pub fn track_request(&self) -> InFlightGuard {
        self.state.increment_in_flight();
        InFlightGuard {
            state: Arc::clone(&self.state),
        }
    }

    /// Get the current in-flight request count.
    #[must_use]
    pub fn in_flight_count(&self) -> usize {
        self.state.in_flight_count()
    }
}

impl Default for ShutdownController {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Shutdown Receiver
// ============================================================================

/// Receiver for shutdown signals.
///
/// Obtained from [`ShutdownController::subscribe()`].
#[derive(Clone)]
pub struct ShutdownReceiver {
    state: Arc<ShutdownState>,
}

impl ShutdownReceiver {
    /// Wait for shutdown to be initiated.
    ///
    /// Returns immediately if shutdown is already in progress.
    pub async fn wait(&self) {
        ShutdownWaitFuture { state: &self.state }.await
    }

    /// Get the current shutdown phase.
    #[must_use]
    pub fn phase(&self) -> ShutdownPhase {
        self.state.phase()
    }

    /// Check if shutdown has been initiated.
    #[must_use]
    pub fn is_shutting_down(&self) -> bool {
        self.state.phase().is_shutting_down()
    }

    /// Check if forced shutdown was requested.
    #[must_use]
    pub fn is_forced(&self) -> bool {
        self.state.is_forced()
    }
}

/// Future for waiting on shutdown.
struct ShutdownWaitFuture<'a> {
    state: &'a ShutdownState,
}

impl Future for ShutdownWaitFuture<'_> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.state.phase().is_shutting_down() {
            Poll::Ready(())
        } else {
            self.state.register_waker(cx.waker());
            // Double-check after registering
            if self.state.phase().is_shutting_down() {
                Poll::Ready(())
            } else {
                Poll::Pending
            }
        }
    }
}

// ============================================================================
// In-Flight Guard
// ============================================================================

/// RAII guard for tracking in-flight requests.
///
/// Decrements the in-flight count when dropped.
pub struct InFlightGuard {
    state: Arc<ShutdownState>,
}

impl Drop for InFlightGuard {
    fn drop(&mut self) {
        self.state.decrement_in_flight();
    }
}

// ============================================================================
// Shutdown Hooks
// ============================================================================

/// A shutdown hook to run during cleanup.
pub enum ShutdownHook {
    /// Synchronous cleanup function.
    Sync(Box<dyn FnOnce() + Send>),
    /// Factory for async cleanup future.
    AsyncFactory(Box<dyn FnOnce() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send>),
}

impl ShutdownHook {
    /// Run the hook synchronously.
    ///
    /// For async hooks, this returns the future to await.
    pub fn run(self) -> Option<Pin<Box<dyn Future<Output = ()> + Send>>> {
        match self {
            Self::Sync(f) => {
                f();
                None
            }
            Self::AsyncFactory(f) => Some(f()),
        }
    }
}

// ============================================================================
// Graceful Shutdown Builder
// ============================================================================

/// Configuration for graceful shutdown.
#[derive(Clone)]
pub struct GracefulConfig {
    /// Grace period for in-flight requests.
    pub grace_period: Duration,
    /// Budget allocated to cleanup operations.
    pub cleanup_budget: Budget,
    /// Log shutdown events.
    pub log_events: bool,
}

impl Default for GracefulConfig {
    fn default() -> Self {
        Self {
            grace_period: Duration::from_secs(30),
            cleanup_budget: Budget::new()
                .with_poll_quota(500)
                .with_deadline(asupersync::Time::from_secs(5)),
            log_events: true,
        }
    }
}

/// Builder for graceful shutdown.
pub struct GracefulShutdown {
    receiver: ShutdownReceiver,
    config: GracefulConfig,
}

impl GracefulShutdown {
    /// Create a new graceful shutdown builder.
    #[must_use]
    pub fn new(receiver: ShutdownReceiver) -> Self {
        Self {
            receiver,
            config: GracefulConfig::default(),
        }
    }

    /// Set the grace period for in-flight requests.
    #[must_use]
    pub fn grace_period(mut self, duration: Duration) -> Self {
        self.config.grace_period = duration;
        self
    }

    /// Set the cleanup budget.
    #[must_use]
    pub fn cleanup_budget(mut self, budget: Budget) -> Self {
        self.config.cleanup_budget = budget;
        self
    }

    /// Enable or disable event logging.
    #[must_use]
    pub fn log_events(mut self, enabled: bool) -> Self {
        self.config.log_events = enabled;
        self
    }

    /// Run a future with graceful shutdown support.
    ///
    /// Returns when either:
    /// - The future completes normally
    /// - Graceful shutdown completes
    /// - Forced shutdown is triggered
    pub async fn run<F, T>(self, fut: F) -> ShutdownOutcome<T>
    where
        F: Future<Output = T>,
    {
        use std::pin::pin;
        use std::task::Poll;

        let mut fut = pin!(fut);

        std::future::poll_fn(|cx| {
            // Drive the main future forward first; if it completes we return immediately.
            if let Poll::Ready(v) = fut.as_mut().poll(cx) {
                return Poll::Ready(ShutdownOutcome::Completed(v));
            }

            // If shutdown has started, return the appropriate outcome.
            //
            // Note: `forced` is an orthogonal flag; if it is set we treat it as higher priority.
            if self.receiver.state.is_forced() {
                return Poll::Ready(ShutdownOutcome::ForcedShutdown);
            }
            if self.receiver.state.phase().is_shutting_down() {
                return Poll::Ready(ShutdownOutcome::GracefulShutdown);
            }

            // Register to be woken on any shutdown phase/forced transition.
            self.receiver.state.register_waker(cx.waker());
            // Double-check after registering to avoid missing a transition.
            if self.receiver.state.is_forced() {
                Poll::Ready(ShutdownOutcome::ForcedShutdown)
            } else if self.receiver.state.phase().is_shutting_down() {
                Poll::Ready(ShutdownOutcome::GracefulShutdown)
            } else {
                Poll::Pending
            }
        })
        .await
    }

    /// Get the configuration.
    #[must_use]
    pub fn config(&self) -> &GracefulConfig {
        &self.config
    }
}

/// Outcome of running with graceful shutdown.
#[derive(Debug)]
pub enum ShutdownOutcome<T> {
    /// The future completed normally.
    Completed(T),
    /// Graceful shutdown was triggered.
    GracefulShutdown,
    /// Forced shutdown was triggered (second signal).
    ForcedShutdown,
}

impl<T> ShutdownOutcome<T> {
    /// Returns `true` if the future completed normally.
    #[must_use]
    pub fn is_completed(&self) -> bool {
        matches!(self, Self::Completed(_))
    }

    /// Returns `true` if shutdown was triggered.
    #[must_use]
    pub fn is_shutdown(&self) -> bool {
        matches!(self, Self::GracefulShutdown | Self::ForcedShutdown)
    }

    /// Returns `true` if forced shutdown was triggered.
    #[must_use]
    pub fn is_forced(&self) -> bool {
        matches!(self, Self::ForcedShutdown)
    }

    /// Extract the completed value, if any.
    #[must_use]
    pub fn into_completed(self) -> Option<T> {
        match self {
            Self::Completed(v) => Some(v),
            _ => None,
        }
    }
}

// ============================================================================
// Request Budget Subdivision
// ============================================================================

/// Calculate subdivided budget for a request during grace period.
///
/// During graceful shutdown, in-flight requests get a proportional share
/// of the remaining grace period.
///
/// # Arguments
///
/// * `grace_remaining` - Time remaining in the grace period
/// * `in_flight_count` - Number of in-flight requests
/// * `original_budget` - The request's original budget (if any)
///
/// # Returns
///
/// A budget that is the minimum of:
/// - The remaining grace period divided by in-flight count
/// - The original budget (if provided)
#[must_use]
pub fn subdivide_grace_budget(
    grace_remaining: Duration,
    in_flight_count: usize,
    original_budget: Option<Budget>,
) -> Budget {
    use asupersync::Time;

    let count = in_flight_count.max(1);
    let per_request = grace_remaining / count as u32;

    // Convert Duration to Time (nanoseconds since epoch, but used as a relative deadline)
    let deadline_nanos = per_request.as_nanos() as u64;
    let grace_budget = Budget::new().with_deadline(Time::from_nanos(deadline_nanos));

    match original_budget {
        Some(original) => original.meet(grace_budget),
        None => grace_budget,
    }
}

// ============================================================================
// Cancel Reason for Shutdown
// ============================================================================

/// Create a cancel reason for server shutdown.
#[must_use]
pub fn shutdown_cancel_reason() -> CancelReason {
    CancelReason::shutdown()
}

/// Create a cancel reason for grace period expiry.
#[must_use]
pub fn grace_expired_cancel_reason() -> CancelReason {
    CancelReason::timeout()
}

// ============================================================================
// Request Context Integration
// ============================================================================

/// Extension trait for checking shutdown status from request context.
pub trait ShutdownAware {
    /// Check if the server is shutting down.
    fn is_shutting_down(&self) -> bool;

    /// Get the current shutdown phase, if available.
    fn shutdown_phase(&self) -> Option<ShutdownPhase>;
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shutdown_phase_transitions() {
        assert!(!ShutdownPhase::Running.should_reject_connections());
        assert!(ShutdownPhase::StopAccepting.should_reject_connections());
        assert!(ShutdownPhase::ShutdownFlagged.should_reject_requests());
        assert!(ShutdownPhase::GracePeriod.is_shutting_down());
        assert!(ShutdownPhase::Stopped.is_stopped());
    }

    #[test]
    fn controller_basic() {
        let controller = ShutdownController::new();
        assert_eq!(controller.phase(), ShutdownPhase::Running);
        assert!(!controller.is_shutting_down());

        controller.shutdown();
        assert_eq!(controller.phase(), ShutdownPhase::StopAccepting);
        assert!(controller.is_shutting_down());
    }

    #[test]
    fn controller_double_shutdown_forces() {
        let controller = ShutdownController::new();
        controller.shutdown();
        assert!(!controller.is_forced());

        controller.shutdown();
        assert!(controller.is_forced());
    }

    #[test]
    fn controller_advance_phase() {
        let controller = ShutdownController::new();

        assert!(controller.advance_phase());
        assert_eq!(controller.phase(), ShutdownPhase::StopAccepting);

        assert!(controller.advance_phase());
        assert_eq!(controller.phase(), ShutdownPhase::ShutdownFlagged);

        assert!(controller.advance_phase());
        assert_eq!(controller.phase(), ShutdownPhase::GracePeriod);

        assert!(controller.advance_phase());
        assert_eq!(controller.phase(), ShutdownPhase::Cancelling);

        assert!(controller.advance_phase());
        assert_eq!(controller.phase(), ShutdownPhase::RunningHooks);

        assert!(controller.advance_phase());
        assert_eq!(controller.phase(), ShutdownPhase::Stopped);

        // Can't advance past Stopped
        assert!(!controller.advance_phase());
    }

    #[test]
    fn in_flight_tracking() {
        let controller = ShutdownController::new();
        assert_eq!(controller.in_flight_count(), 0);

        let guard1 = controller.track_request();
        assert_eq!(controller.in_flight_count(), 1);

        let guard2 = controller.track_request();
        assert_eq!(controller.in_flight_count(), 2);

        drop(guard1);
        assert_eq!(controller.in_flight_count(), 1);

        drop(guard2);
        assert_eq!(controller.in_flight_count(), 0);
    }

    #[test]
    fn shutdown_hooks_lifo() {
        let controller = ShutdownController::new();
        let order: Arc<parking_lot::Mutex<Vec<i32>>> =
            Arc::new(parking_lot::Mutex::new(Vec::new()));

        let order1 = Arc::clone(&order);
        controller.register_hook(move || order1.lock().push(1));

        let order2 = Arc::clone(&order);
        controller.register_hook(move || order2.lock().push(2));

        let order3 = Arc::clone(&order);
        controller.register_hook(move || order3.lock().push(3));

        assert_eq!(controller.hook_count(), 3);

        // Pop and run in LIFO order
        while let Some(hook) = controller.pop_hook() {
            hook.run();
        }

        assert_eq!(*order.lock(), vec![3, 2, 1]);
    }

    #[test]
    fn subdivide_grace_budget_basic() {
        let grace = Duration::from_secs(30);
        let budget = subdivide_grace_budget(grace, 3, None);

        // Each request gets 10 seconds
        assert!(budget.deadline.is_some());
    }

    #[test]
    fn subdivide_grace_budget_respects_original() {
        use asupersync::Time;

        let grace = Duration::from_secs(30);
        let original = Budget::new().with_deadline(Time::from_secs(5));
        let budget = subdivide_grace_budget(grace, 3, Some(original));

        // Should respect the tighter (original) deadline
        assert!(budget.deadline.is_some());
    }

    #[test]
    fn receiver_is_shutting_down() {
        let controller = ShutdownController::new();
        let receiver = controller.subscribe();

        assert!(!receiver.is_shutting_down());

        controller.shutdown();
        assert!(receiver.is_shutting_down());
    }

    #[test]
    fn graceful_shutdown_run_completed() {
        let controller = ShutdownController::new();
        let shutdown = GracefulShutdown::new(controller.subscribe());

        let rt = asupersync::runtime::RuntimeBuilder::current_thread()
            .build()
            .expect("runtime must build");
        let out = rt.block_on(async { shutdown.run(async { 42i32 }).await });

        assert!(matches!(out, ShutdownOutcome::Completed(42)));
    }

    #[test]
    fn graceful_shutdown_run_graceful_shutdown() {
        let controller = ShutdownController::new();
        controller.shutdown();
        let shutdown = GracefulShutdown::new(controller.subscribe());

        let rt = asupersync::runtime::RuntimeBuilder::current_thread()
            .build()
            .expect("runtime must build");
        let out = rt.block_on(async { shutdown.run(std::future::pending::<i32>()).await });

        assert!(matches!(out, ShutdownOutcome::GracefulShutdown));
    }

    #[test]
    fn graceful_shutdown_run_forced_shutdown() {
        let controller = ShutdownController::new();
        controller.force_shutdown();
        let shutdown = GracefulShutdown::new(controller.subscribe());

        let rt = asupersync::runtime::RuntimeBuilder::current_thread()
            .build()
            .expect("runtime must build");
        let out = rt.block_on(async { shutdown.run(std::future::pending::<i32>()).await });

        assert!(matches!(out, ShutdownOutcome::ForcedShutdown));
    }

    #[test]
    fn shutdown_outcome_accessors() {
        let completed: ShutdownOutcome<i32> = ShutdownOutcome::Completed(42);
        assert!(completed.is_completed());
        assert!(!completed.is_shutdown());
        assert_eq!(completed.into_completed(), Some(42));

        let graceful: ShutdownOutcome<i32> = ShutdownOutcome::GracefulShutdown;
        assert!(!graceful.is_completed());
        assert!(graceful.is_shutdown());
        assert!(!graceful.is_forced());

        let forced: ShutdownOutcome<i32> = ShutdownOutcome::ForcedShutdown;
        assert!(forced.is_shutdown());
        assert!(forced.is_forced());
    }

    #[test]
    fn cancel_reasons() {
        let shutdown = shutdown_cancel_reason();
        assert!(shutdown.is_shutdown());

        let grace = grace_expired_cancel_reason();
        assert!(!grace.is_shutdown());
    }
}