allframe-core 0.1.28

AllFrame core - complete web framework with HTTP/2 server, REST/GraphQL/gRPC, DI, CQRS
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
//! Graceful shutdown utilities
//!
//! This module provides utilities for handling graceful shutdown of services,
//! including signal handling, timeout management, and task cancellation.
//!
//! # Example
//!
//! ```rust,no_run
//! use allframe_core::shutdown::{GracefulShutdown, ShutdownSignal};
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() {
//!     let shutdown = GracefulShutdown::new();
//!
//!     // Spawn a task that will be cancelled on shutdown
//!     let mut token = shutdown.token();
//!     tokio::spawn(async move {
//!         loop {
//!             tokio::select! {
//!                 _ = token.cancelled() => {
//!                     println!("Task cancelled");
//!                     break;
//!                 }
//!                 _ = tokio::time::sleep(Duration::from_secs(1)) => {
//!                     println!("Working...");
//!                 }
//!             }
//!         }
//!     });
//!
//!     // Wait for shutdown signal
//!     shutdown.wait().await;
//! }
//! ```

use std::{future::Future, pin::Pin, sync::Arc, time::Duration};

use tokio::sync::{broadcast, watch};

/// Shutdown signal types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShutdownSignal {
    /// SIGINT (Ctrl+C)
    Interrupt,
    /// SIGTERM
    Terminate,
    /// Manual shutdown request
    Manual,
}

impl std::fmt::Display for ShutdownSignal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ShutdownSignal::Interrupt => write!(f, "SIGINT"),
            ShutdownSignal::Terminate => write!(f, "SIGTERM"),
            ShutdownSignal::Manual => write!(f, "Manual"),
        }
    }
}

/// A token that can be used to check if shutdown has been requested
#[derive(Clone)]
pub struct ShutdownToken {
    receiver: watch::Receiver<bool>,
}

impl ShutdownToken {
    /// Check if shutdown has been requested
    pub fn is_shutdown(&self) -> bool {
        *self.receiver.borrow()
    }

    /// Wait until shutdown is requested
    pub async fn cancelled(&mut self) {
        // Wait for the value to become true
        let _ = self.receiver.wait_for(|v| *v).await;
    }
}

/// Builder for configuring graceful shutdown
pub struct GracefulShutdownBuilder {
    timeout: Duration,
    on_signal: Option<Box<dyn Fn(ShutdownSignal) + Send + Sync>>,
}

impl Default for GracefulShutdownBuilder {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            on_signal: None,
        }
    }
}

impl GracefulShutdownBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the shutdown timeout
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set a callback to be called when a shutdown signal is received
    pub fn on_signal<F>(mut self, callback: F) -> Self
    where
        F: Fn(ShutdownSignal) + Send + Sync + 'static,
    {
        self.on_signal = Some(Box::new(callback));
        self
    }

    /// Build the graceful shutdown handler
    pub fn build(self) -> GracefulShutdown {
        let on_signal: Option<Arc<dyn Fn(ShutdownSignal) + Send + Sync>> = self
            .on_signal
            .map(|f| Arc::from(f) as Arc<dyn Fn(ShutdownSignal) + Send + Sync>);
        GracefulShutdown {
            timeout: self.timeout,
            on_signal,
            shutdown_tx: watch::channel(false).0,
            signal_tx: broadcast::channel(1).0,
        }
    }
}

/// Graceful shutdown handler
///
/// Provides utilities for handling graceful shutdown of services.
pub struct GracefulShutdown {
    timeout: Duration,
    on_signal: Option<Arc<dyn Fn(ShutdownSignal) + Send + Sync>>,
    shutdown_tx: watch::Sender<bool>,
    signal_tx: broadcast::Sender<ShutdownSignal>,
}

impl GracefulShutdown {
    /// Create a new graceful shutdown handler with default settings
    pub fn new() -> Self {
        GracefulShutdownBuilder::new().build()
    }

    /// Create a builder for configuring the shutdown handler
    pub fn builder() -> GracefulShutdownBuilder {
        GracefulShutdownBuilder::new()
    }

    /// Get the shutdown timeout
    pub fn timeout(&self) -> Duration {
        self.timeout
    }

    /// Get a token that can be used to check shutdown status
    pub fn token(&self) -> ShutdownToken {
        ShutdownToken {
            receiver: self.shutdown_tx.subscribe(),
        }
    }

    /// Subscribe to shutdown signals
    pub fn subscribe(&self) -> broadcast::Receiver<ShutdownSignal> {
        self.signal_tx.subscribe()
    }

    /// Trigger a manual shutdown
    pub fn shutdown(&self) {
        let _ = self.shutdown_tx.send(true);
        let _ = self.signal_tx.send(ShutdownSignal::Manual);
        if let Some(ref callback) = self.on_signal {
            callback(ShutdownSignal::Manual);
        }
    }

    /// Wait for a shutdown signal (SIGINT or SIGTERM)
    ///
    /// Returns the signal that triggered the shutdown.
    pub async fn wait(&self) -> ShutdownSignal {
        let signal = wait_for_signal().await;

        // Notify subscribers
        let _ = self.shutdown_tx.send(true);
        let _ = self.signal_tx.send(signal);

        // Call the callback if set
        if let Some(ref callback) = self.on_signal {
            callback(signal);
        }

        signal
    }

    /// Wait for shutdown with a timeout
    ///
    /// If the timeout expires before shutdown completes, returns None.
    pub async fn wait_with_timeout(&self) -> Option<ShutdownSignal> {
        tokio::select! {
            signal = self.wait() => Some(signal),
            _ = tokio::time::sleep(self.timeout) => None,
        }
    }

    /// Run a future until shutdown is requested
    ///
    /// Returns the result of the future if it completes before shutdown,
    /// or None if shutdown was requested first.
    pub async fn run_until_shutdown<F, T>(&self, future: F) -> Option<T>
    where
        F: Future<Output = T>,
    {
        let mut token = self.token();
        tokio::select! {
            result = future => Some(result),
            _ = token.cancelled() => None,
        }
    }

    /// Spawn a task that will be cancelled on shutdown
    pub fn spawn<F>(&self, name: &str, future: F) -> tokio::task::JoinHandle<Option<()>>
    where
        F: Future<Output = ()> + Send + 'static,
    {
        let mut token = self.token();
        let name = name.to_string();
        tokio::spawn(async move {
            tokio::select! {
                _ = future => {
                    Some(())
                }
                _ = token.cancelled() => {
                    #[cfg(feature = "otel")]
                    tracing::info!(task = %name, "Task cancelled due to shutdown");
                    #[cfg(not(feature = "otel"))]
                    let _ = name;
                    None
                }
            }
        })
    }
}

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

/// Wait for a shutdown signal (SIGINT or SIGTERM)
async fn wait_for_signal() -> ShutdownSignal {
    #[cfg(unix)]
    {
        use tokio::signal::unix::{signal, SignalKind};

        let mut sigint =
            signal(SignalKind::interrupt()).expect("Failed to register SIGINT handler");
        let mut sigterm =
            signal(SignalKind::terminate()).expect("Failed to register SIGTERM handler");

        tokio::select! {
            _ = sigint.recv() => ShutdownSignal::Interrupt,
            _ = sigterm.recv() => ShutdownSignal::Terminate,
        }
    }

    #[cfg(not(unix))]
    {
        tokio::signal::ctrl_c()
            .await
            .expect("Failed to register Ctrl+C handler");
        ShutdownSignal::Interrupt
    }
}

/// Shutdown guard that triggers shutdown when dropped
///
/// Useful for ensuring cleanup happens even on panic.
pub struct ShutdownGuard {
    shutdown: Arc<GracefulShutdown>,
}

impl ShutdownGuard {
    /// Create a new shutdown guard
    pub fn new(shutdown: Arc<GracefulShutdown>) -> Self {
        Self { shutdown }
    }
}

impl Drop for ShutdownGuard {
    fn drop(&mut self) {
        self.shutdown.shutdown();
    }
}

/// Extension trait for futures that adds shutdown awareness
pub trait ShutdownExt: Future + Sized {
    /// Run this future until completion or shutdown
    fn with_shutdown(
        self,
        shutdown: &GracefulShutdown,
    ) -> Pin<Box<dyn Future<Output = Option<Self::Output>> + Send + '_>>
    where
        Self: Send + 'static,
        Self::Output: Send,
    {
        let future = self;
        let mut token = shutdown.token();
        Box::pin(async move {
            tokio::select! {
                result = future => Some(result),
                _ = token.cancelled() => None,
            }
        })
    }
}

impl<F: Future> ShutdownExt for F {}

/// Shutdown-aware task spawner
///
/// A wrapper around `GracefulShutdown` for spawning named tasks that respect
/// shutdown signals. Provides logging and automatic cancellation on shutdown.
///
/// # Example
///
/// ```rust,no_run
/// use allframe_core::shutdown::{GracefulShutdown, ShutdownAwareTaskSpawner};
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() {
///     let shutdown = Arc::new(GracefulShutdown::new());
///     let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());
///
///     // Spawn a task that will be cancelled on shutdown
///     spawner.spawn("my_task", || async {
///         loop {
///             tokio::time::sleep(std::time::Duration::from_secs(1)).await;
///             println!("Working...");
///         }
///     });
///
///     // Shutdown will cancel the spawned task
///     shutdown.shutdown();
/// }
/// ```
pub struct ShutdownAwareTaskSpawner {
    shutdown: Arc<GracefulShutdown>,
}

impl ShutdownAwareTaskSpawner {
    /// Create a new shutdown-aware task spawner
    pub fn new(shutdown: Arc<GracefulShutdown>) -> Self {
        Self { shutdown }
    }

    /// Get a reference to the underlying shutdown handler
    pub fn shutdown(&self) -> &Arc<GracefulShutdown> {
        &self.shutdown
    }

    /// Spawn a task that will be cancelled on shutdown
    ///
    /// The task will be logged when starting, completing, and cancelling.
    pub fn spawn<F, Fut>(&self, task_name: &str, future: F) -> tokio::task::JoinHandle<()>
    where
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send,
    {
        let mut token = self.shutdown.token();
        let task_name = task_name.to_string();

        tokio::spawn(async move {
            #[cfg(feature = "otel")]
            tracing::info!(task = %task_name, "Starting task");

            let task_future = future();

            tokio::select! {
                _ = task_future => {
                    #[cfg(feature = "otel")]
                    tracing::info!(task = %task_name, "Task completed normally");
                }
                _ = token.cancelled() => {
                    #[cfg(feature = "otel")]
                    tracing::info!(task = %task_name, "Task cancelled due to shutdown");
                }
            }

            #[cfg(feature = "otel")]
            tracing::info!(task = %task_name, "Task finished");

            // Suppress unused variable warning when otel is disabled
            #[cfg(not(feature = "otel"))]
            let _ = task_name;
        })
    }

    /// Spawn a long-running background task
    ///
    /// This is an alias for `spawn` - both handle shutdown the same way.
    /// Use this to semantically indicate the task is intended to run
    /// for the lifetime of the application.
    pub fn spawn_background<F, Fut>(
        &self,
        task_name: &str,
        future: F,
    ) -> tokio::task::JoinHandle<()>
    where
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = ()> + Send,
    {
        self.spawn(task_name, future)
    }

    /// Spawn a task and return its result (if it completes before shutdown)
    pub fn spawn_with_result<F, Fut, T>(
        &self,
        task_name: &str,
        future: F,
    ) -> tokio::task::JoinHandle<Option<T>>
    where
        F: FnOnce() -> Fut + Send + 'static,
        Fut: Future<Output = T> + Send,
        T: Send + 'static,
    {
        let mut token = self.shutdown.token();
        let task_name = task_name.to_string();

        tokio::spawn(async move {
            #[cfg(feature = "otel")]
            tracing::info!(task = %task_name, "Starting task");

            let task_future = future();

            let result = tokio::select! {
                result = task_future => {
                    #[cfg(feature = "otel")]
                    tracing::info!(task = %task_name, "Task completed normally");
                    Some(result)
                }
                _ = token.cancelled() => {
                    #[cfg(feature = "otel")]
                    tracing::info!(task = %task_name, "Task cancelled due to shutdown");
                    None
                }
            };

            #[cfg(feature = "otel")]
            tracing::info!(task = %task_name, "Task finished");

            // Suppress unused variable warning when otel is disabled
            #[cfg(not(feature = "otel"))]
            let _ = task_name;

            result
        })
    }
}

impl Clone for ShutdownAwareTaskSpawner {
    fn clone(&self) -> Self {
        Self {
            shutdown: self.shutdown.clone(),
        }
    }
}

/// Extension trait for `GracefulShutdown` providing additional cleanup
/// functionality
///
/// This trait adds a `perform_shutdown` method that runs cleanup functions
/// during the shutdown sequence with proper error logging.
///
/// # Example
///
/// ```rust,no_run
/// use allframe_core::shutdown::{GracefulShutdown, GracefulShutdownExt};
///
/// async fn cleanup_resources() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
///     // Close database connections, flush buffers, etc.
///     Ok(())
/// }
///
/// #[tokio::main]
/// async fn main() {
///     let shutdown = GracefulShutdown::new();
///
///     // Trigger shutdown and run cleanup
///     shutdown.perform_shutdown(cleanup_resources).await.unwrap();
/// }
/// ```
pub trait GracefulShutdownExt {
    /// Perform graceful shutdown sequence with cleanup
    ///
    /// Runs the provided cleanup function and logs any errors that occur.
    /// This method is designed to be called when shutdown is triggered.
    fn perform_shutdown<F, Fut, E>(
        &self,
        cleanup_fn: F,
    ) -> impl Future<Output = Result<(), E>> + Send
    where
        F: FnOnce() -> Fut + Send,
        Fut: Future<Output = Result<(), E>> + Send,
        E: std::fmt::Display + Send;
}

impl GracefulShutdownExt for GracefulShutdown {
    async fn perform_shutdown<F, Fut, E>(&self, cleanup_fn: F) -> Result<(), E>
    where
        F: FnOnce() -> Fut + Send,
        Fut: Future<Output = Result<(), E>> + Send,
        E: std::fmt::Display + Send,
    {
        #[cfg(feature = "otel")]
        tracing::info!("Starting graceful shutdown sequence");

        // Run custom cleanup function
        #[cfg(feature = "otel")]
        tracing::info!("Running cleanup functions");

        let result = cleanup_fn().await;

        if let Err(ref e) = result {
            #[cfg(feature = "otel")]
            tracing::error!(error = %e, "Cleanup function failed");

            // Suppress unused variable warning when otel is disabled
            #[cfg(not(feature = "otel"))]
            let _ = e;
        }

        #[cfg(feature = "otel")]
        tracing::info!("Graceful shutdown completed");

        result
    }
}

/// Extension trait for `Arc<GracefulShutdown>` providing the same functionality
impl GracefulShutdownExt for Arc<GracefulShutdown> {
    async fn perform_shutdown<F, Fut, E>(&self, cleanup_fn: F) -> Result<(), E>
    where
        F: FnOnce() -> Fut + Send,
        Fut: Future<Output = Result<(), E>> + Send,
        E: std::fmt::Display + Send,
    {
        self.as_ref().perform_shutdown(cleanup_fn).await
    }
}

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

    #[test]
    fn test_shutdown_signal_display() {
        assert_eq!(ShutdownSignal::Interrupt.to_string(), "SIGINT");
        assert_eq!(ShutdownSignal::Terminate.to_string(), "SIGTERM");
        assert_eq!(ShutdownSignal::Manual.to_string(), "Manual");
    }

    #[tokio::test]
    async fn test_shutdown_token() {
        let shutdown = GracefulShutdown::new();
        let token = shutdown.token();

        assert!(!token.is_shutdown());

        shutdown.shutdown();

        assert!(token.is_shutdown());
    }

    #[tokio::test]
    async fn test_manual_shutdown() {
        let shutdown = GracefulShutdown::new();
        let mut rx = shutdown.subscribe();

        shutdown.shutdown();

        let signal = rx.recv().await.unwrap();
        assert_eq!(signal, ShutdownSignal::Manual);
    }

    #[tokio::test]
    async fn test_shutdown_callback() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let called = Arc::new(AtomicBool::new(false));
        let called_clone = called.clone();

        let shutdown = GracefulShutdown::builder()
            .on_signal(move |_| {
                called_clone.store(true, Ordering::SeqCst);
            })
            .build();

        shutdown.shutdown();

        assert!(called.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn test_run_until_shutdown() {
        let shutdown = GracefulShutdown::new();

        // Future completes before shutdown
        let result = shutdown.run_until_shutdown(async { 42 }).await;
        assert_eq!(result, Some(42));
    }

    #[tokio::test]
    async fn test_run_until_shutdown_cancelled() {
        let shutdown = GracefulShutdown::new();
        let token = shutdown.token();

        // Trigger shutdown before running the future
        shutdown.shutdown();

        // Token should now be shutdown
        assert!(token.is_shutdown());
    }

    #[tokio::test]
    async fn test_builder_timeout() {
        let shutdown = GracefulShutdown::builder()
            .timeout(Duration::from_secs(60))
            .build();

        assert_eq!(shutdown.timeout(), Duration::from_secs(60));
    }

    #[tokio::test]
    async fn test_spawn_task() {
        let shutdown = GracefulShutdown::new();
        let counter = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let counter_clone = counter.clone();

        let handle = shutdown.spawn("test_task", async move {
            counter_clone.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        });

        // Wait for the task to complete
        let result = handle.await.unwrap();
        assert_eq!(result, Some(()));
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_shutdown_aware_spawner_task_completes() {
        let shutdown = Arc::new(GracefulShutdown::new());
        let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());

        let counter = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let counter_clone = counter.clone();

        let handle = spawner.spawn("test_task", move || async move {
            counter_clone.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        });

        handle.await.unwrap();
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_shutdown_aware_spawner_task_cancelled() {
        let shutdown = Arc::new(GracefulShutdown::new());
        let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());

        let counter = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let counter_clone = counter.clone();

        // Spawn a task that will sleep for a long time
        let handle = spawner.spawn("long_task", move || async move {
            tokio::time::sleep(Duration::from_secs(60)).await;
            counter_clone.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        });

        // Trigger shutdown immediately
        shutdown.shutdown();

        // Task should complete (due to cancellation)
        handle.await.unwrap();

        // Counter should NOT have been incremented (task was cancelled)
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn test_shutdown_aware_spawner_with_result() {
        let shutdown = Arc::new(GracefulShutdown::new());
        let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());

        let handle = spawner.spawn_with_result("compute_task", || async { 42 });

        let result = handle.await.unwrap();
        assert_eq!(result, Some(42));
    }

    #[tokio::test]
    async fn test_shutdown_aware_spawner_with_result_cancelled() {
        let shutdown = Arc::new(GracefulShutdown::new());
        let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());

        let handle = spawner.spawn_with_result("long_compute", || async {
            tokio::time::sleep(Duration::from_secs(60)).await;
            42
        });

        // Trigger shutdown immediately
        shutdown.shutdown();

        let result = handle.await.unwrap();
        assert_eq!(result, None); // Cancelled, no result
    }

    #[tokio::test]
    async fn test_shutdown_aware_spawner_clone() {
        let shutdown = Arc::new(GracefulShutdown::new());
        let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());
        let spawner2 = spawner.clone();

        // Both spawners share the same shutdown
        assert!(Arc::ptr_eq(spawner.shutdown(), spawner2.shutdown()));
    }

    #[tokio::test]
    async fn test_graceful_shutdown_ext_success() {
        let shutdown = GracefulShutdown::new();

        let result: Result<(), &str> = shutdown.perform_shutdown(|| async { Ok(()) }).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_graceful_shutdown_ext_error() {
        let shutdown = GracefulShutdown::new();

        let result: Result<(), &str> = shutdown
            .perform_shutdown(|| async { Err("cleanup failed") })
            .await;

        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), "cleanup failed");
    }

    #[tokio::test]
    async fn test_graceful_shutdown_ext_with_arc() {
        let shutdown = Arc::new(GracefulShutdown::new());

        let result: Result<(), &str> = shutdown.perform_shutdown(|| async { Ok(()) }).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_shutdown_aware_spawner_background() {
        let shutdown = Arc::new(GracefulShutdown::new());
        let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());

        let counter = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let counter_clone = counter.clone();

        // spawn_background is an alias for spawn
        let handle = spawner.spawn_background("bg_task", move || async move {
            counter_clone.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        });

        handle.await.unwrap();
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
    }
}