spikard-http 0.16.0

High-performance HTTP server for Spikard with tower-http middleware stack
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
#![allow(clippy::pedantic, clippy::nursery, clippy::all)]
//! Behavioral tests for background task execution in spikard-http.
//!
//! These tests focus on observable behavior: task completion, timing, resource cleanup,
//! and graceful shutdown patterns. They validate end-to-end behavior rather than
//! implementation details.
//!
//! Test categories:
//! 1. Graceful Shutdown & Draining
//! 2. Shutdown Timeout Behavior
//! 3. Task Success/Failure Observable Outcomes
//! 4. High-Volume Task Queue
//! 5. Task Execution Order Guarantees
//! 6. Concurrent Task Execution
//! 7. Task Cancellation Propagation

use spikard_http::background::{
    BackgroundJobError, BackgroundJobMetadata, BackgroundRuntime, BackgroundSpawnError, BackgroundTaskConfig,
};
use std::borrow::Cow;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant};

/// Verifies that shutdown waits for all in-flight tasks to complete gracefully.
/// Observable behavior: all spawned tasks complete before shutdown returns Ok.
#[tokio::test]
async fn test_graceful_shutdown_drains_all_spawned_tasks() {
    let config = BackgroundTaskConfig {
        max_queue_size: 50,
        max_concurrent_tasks: 5,
        drain_timeout_secs: 10,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let completion_count = Arc::new(AtomicU64::new(0));
    let task_count = 20;

    for _ in 0..task_count {
        let count = completion_count.clone();
        handle
            .spawn(move || {
                let c = count.clone();
                async move {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                    c.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(50)).await;

    let shutdown_result = runtime.shutdown().await;
    assert!(
        shutdown_result.is_ok(),
        "shutdown should succeed when draining in-flight tasks"
    );

    assert_eq!(
        completion_count.load(Ordering::SeqCst),
        task_count,
        "all spawned tasks must complete during graceful shutdown"
    );
}

/// Verifies that shutdown processes queued tasks before returning.
/// Observable behavior: both in-flight and queued tasks complete.
#[tokio::test]
async fn test_graceful_shutdown_processes_both_inflight_and_queued_tasks() {
    let config = BackgroundTaskConfig {
        max_queue_size: 100,
        max_concurrent_tasks: 2,
        drain_timeout_secs: 10,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let completion_count = Arc::new(AtomicU64::new(0));
    let task_count = 15;

    for _ in 0..task_count {
        let count = completion_count.clone();
        handle
            .spawn(move || {
                let c = count.clone();
                async move {
                    tokio::time::sleep(Duration::from_millis(5)).await;
                    c.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    let shutdown_result = runtime.shutdown().await;
    assert!(
        shutdown_result.is_ok(),
        "shutdown should drain both in-flight and queued tasks"
    );

    assert_eq!(
        completion_count.load(Ordering::SeqCst),
        task_count,
        "all tasks including queued ones must complete"
    );
}

/// Verifies that shutdown times out when tasks exceed drain timeout.
/// Observable behavior: shutdown returns error, incomplete tasks remain unfinished.
#[tokio::test]
async fn test_shutdown_timeout_with_long_running_task() {
    let config = BackgroundTaskConfig {
        max_queue_size: 10,
        max_concurrent_tasks: 2,
        drain_timeout_secs: 1,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let task_completed = Arc::new(AtomicBool::new(false));
    let completed_clone = task_completed.clone();

    handle
        .spawn(move || {
            let c = completed_clone.clone();
            async move {
                tokio::time::sleep(Duration::from_secs(10)).await;
                c.store(true, Ordering::SeqCst);
                Ok(())
            }
        })
        .expect("spawn failed");

    tokio::time::sleep(Duration::from_millis(50)).await;

    let shutdown_result = runtime.shutdown().await;
    assert!(
        shutdown_result.is_err(),
        "shutdown should timeout with incomplete long-running task"
    );

    assert!(
        !task_completed.load(Ordering::SeqCst),
        "incomplete task should not complete after shutdown timeout"
    );
}

/// Verifies shutdown respects drain timeout duration.
/// Observable behavior: shutdown duration is approximately the drain_timeout_secs.
#[tokio::test]
async fn test_shutdown_timeout_duration_respected() {
    let drain_timeout_secs = 2;
    let config = BackgroundTaskConfig {
        max_queue_size: 10,
        max_concurrent_tasks: 1,
        drain_timeout_secs,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    handle
        .spawn(|| async {
            tokio::time::sleep(Duration::from_secs(30)).await;
            Ok(())
        })
        .expect("spawn failed");

    tokio::time::sleep(Duration::from_millis(100)).await;

    let shutdown_start = Instant::now();
    let _ = runtime.shutdown().await;
    let shutdown_elapsed = shutdown_start.elapsed();

    assert!(
        shutdown_elapsed >= Duration::from_secs(drain_timeout_secs - 1),
        "shutdown should wait at least drain_timeout"
    );
    assert!(
        shutdown_elapsed < Duration::from_secs(drain_timeout_secs + 2),
        "shutdown should not wait much longer than drain_timeout"
    );
}

/// Verifies that successful tasks complete without affecting other tasks.
/// Observable behavior: task runs to completion, no side effects on runtime.
#[tokio::test]
async fn test_task_success_completes_cleanly() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let success_flag = Arc::new(AtomicBool::new(false));
    let flag_clone = success_flag.clone();

    handle
        .spawn(move || {
            let f = flag_clone.clone();
            async move {
                tokio::time::sleep(Duration::from_millis(10)).await;
                f.store(true, Ordering::SeqCst);
                Ok(())
            }
        })
        .expect("spawn failed");

    tokio::time::sleep(Duration::from_millis(50)).await;

    assert!(
        success_flag.load(Ordering::SeqCst),
        "successful task should execute and set flag"
    );

    let shutdown_result = runtime.shutdown().await;
    assert!(shutdown_result.is_ok(), "shutdown should succeed after successful task");
}

/// Verifies that failed tasks log failure but don't crash the runtime.
/// Observable behavior: failed task executes, runtime continues accepting new tasks.
#[tokio::test]
async fn test_task_failure_doesnt_crash_runtime() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let failure_count = Arc::new(AtomicU64::new(0));
    let success_count = Arc::new(AtomicU64::new(0));

    {
        let f = failure_count.clone();
        handle
            .spawn(move || {
                let fail = f.clone();
                async move {
                    fail.fetch_add(1, Ordering::SeqCst);
                    Err(BackgroundJobError::from("task error"))
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(50)).await;

    {
        let s = success_count.clone();
        handle
            .spawn(move || {
                let succ = s.clone();
                async move {
                    succ.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(100)).await;

    assert_eq!(failure_count.load(Ordering::SeqCst), 1, "failed task should execute");
    assert_eq!(
        success_count.load(Ordering::SeqCst),
        1,
        "task after failure should also execute"
    );

    let shutdown_result = runtime.shutdown().await;
    assert!(
        shutdown_result.is_ok(),
        "runtime should shutdown cleanly after failed tasks"
    );
}

/// Verifies that mixed success/failure tasks all execute during shutdown.
/// Observable behavior: shutdown drains both successful and failed tasks.
#[tokio::test]
async fn test_shutdown_drains_mixed_success_and_failure_tasks() {
    let config = BackgroundTaskConfig {
        max_queue_size: 100,
        max_concurrent_tasks: 5,
        drain_timeout_secs: 10,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let success_count = Arc::new(AtomicU64::new(0));
    let failure_count = Arc::new(AtomicU64::new(0));
    let task_count = 20;

    for i in 0..task_count {
        if i % 2 == 0 {
            let s = success_count.clone();
            handle
                .spawn(move || {
                    let succ = s.clone();
                    async move {
                        succ.fetch_add(1, Ordering::SeqCst);
                        Ok(())
                    }
                })
                .expect("spawn failed");
        } else {
            let f = failure_count.clone();
            handle
                .spawn(move || {
                    let fail = f.clone();
                    async move {
                        fail.fetch_add(1, Ordering::SeqCst);
                        Err(BackgroundJobError::from("intentional failure"))
                    }
                })
                .expect("spawn failed");
        }
    }

    let shutdown_result = runtime.shutdown().await;
    assert!(shutdown_result.is_ok(), "shutdown should drain all tasks");

    assert_eq!(
        success_count.load(Ordering::SeqCst),
        10,
        "all successful tasks should execute"
    );
    assert_eq!(
        failure_count.load(Ordering::SeqCst),
        10,
        "all failing tasks should execute"
    );
}

/// Verifies that high-volume queues are processed without resource exhaustion.
/// Observable behavior: 10K tasks all complete within drain timeout.
#[tokio::test]
async fn test_high_volume_queue_10k_tasks() {
    let task_count = 10_000;
    let config = BackgroundTaskConfig {
        max_queue_size: 15_000,
        max_concurrent_tasks: 50,
        drain_timeout_secs: 60,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let completion_count = Arc::new(AtomicU64::new(0));

    for _ in 0..task_count {
        let count = completion_count.clone();
        let result = handle.spawn(move || {
            let c = count.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        });
        assert!(result.is_ok(), "spawn should succeed for high-volume queue");
    }

    let shutdown_result = runtime.shutdown().await;
    assert!(shutdown_result.is_ok(), "shutdown should complete high-volume queue");

    assert_eq!(
        completion_count.load(Ordering::SeqCst),
        task_count as u64,
        "all 10K tasks must execute"
    );
}

/// Verifies queue full behavior under high spawn rate.
/// Observable behavior: QueueFull errors when queue capacity exceeded.
#[tokio::test]
async fn test_high_volume_queue_overflow_behavior() {
    let config = BackgroundTaskConfig {
        max_queue_size: 10,
        max_concurrent_tasks: 50,
        drain_timeout_secs: 10,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let blocking_counter = Arc::new(AtomicU64::new(0));
    let spawned_count = Arc::new(AtomicU64::new(0));

    let mut overflow_error_count = 0;
    for _ in 0..50 {
        let counter = blocking_counter.clone();
        let spawned = spawned_count.clone();
        let result = handle.spawn(move || {
            let c = counter.clone();
            let s = spawned.clone();
            async move {
                s.fetch_add(1, Ordering::SeqCst);
                tokio::time::sleep(Duration::from_millis(100)).await;
                c.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        });

        if let Err(BackgroundSpawnError::QueueFull) = result {
            overflow_error_count += 1;
        }
    }

    assert!(
        overflow_error_count > 0,
        "should see queue full errors when exceeding capacity"
    );

    runtime.shutdown().await.expect("shutdown should succeed");
}

/// Verifies that multiple tasks execute to completion (order not guaranteed, but all complete).
/// Observable behavior: all tasks execute despite concurrent nature.
#[tokio::test]
async fn test_task_execution_order_all_complete() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let execution_log = Arc::new(tokio::sync::Mutex::new(Vec::new()));
    let task_count = 100;

    for i in 0..task_count {
        let log = execution_log.clone();
        handle
            .spawn(move || {
                let l = log.clone();
                async move {
                    l.lock().await.push(i);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(200)).await;

    let log = execution_log.lock().await;

    assert_eq!(log.len(), task_count, "all spawned tasks should execute");

    for i in 0..task_count {
        let count = log.iter().filter(|&&x| x == i).count();
        assert_eq!(count, 1, "task {} should execute exactly once", i);
    }

    runtime.shutdown().await.expect("shutdown should succeed");
}

/// Verifies FIFO-like behavior when concurrency is limited.
/// Observable behavior: with 1 concurrent task, tasks execute sequentially.
#[tokio::test]
async fn test_sequential_execution_with_single_concurrency() {
    let config = BackgroundTaskConfig {
        max_queue_size: 100,
        max_concurrent_tasks: 1,
        drain_timeout_secs: 30,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let execution_order = Arc::new(tokio::sync::Mutex::new(Vec::new()));
    let task_count = 10;

    for i in 0..task_count {
        let order = execution_order.clone();
        handle
            .spawn(move || {
                let o = order.clone();
                async move {
                    o.lock().await.push(i);
                    tokio::time::sleep(Duration::from_millis(5)).await;
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    let shutdown_result = runtime.shutdown().await;
    assert!(shutdown_result.is_ok(), "shutdown should succeed");

    let order = execution_order.lock().await;

    assert_eq!(order.len(), task_count, "all tasks should execute");
}

/// Verifies that concurrent limit is respected during execution.
/// Observable behavior: peak concurrent tasks <= configured limit.
#[tokio::test]
async fn test_concurrent_execution_respects_limit() {
    let config = BackgroundTaskConfig {
        max_queue_size: 100,
        max_concurrent_tasks: 5,
        drain_timeout_secs: 10,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let active_count = Arc::new(AtomicU64::new(0));
    let peak_count = Arc::new(AtomicU64::new(0));
    let task_count = 20;

    for _ in 0..task_count {
        let active = active_count.clone();
        let peak = peak_count.clone();

        handle
            .spawn(move || {
                let a = active.clone();
                let p = peak.clone();

                async move {
                    let current = a.fetch_add(1, Ordering::SeqCst) + 1;

                    let mut peak_val = p.load(Ordering::SeqCst);
                    while current > peak_val {
                        if p.compare_exchange(peak_val, current, Ordering::SeqCst, Ordering::SeqCst)
                            .is_ok()
                        {
                            break;
                        }
                        peak_val = p.load(Ordering::SeqCst);
                    }

                    tokio::time::sleep(Duration::from_millis(100)).await;
                    a.fetch_sub(1, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(300)).await;

    let peak = peak_count.load(Ordering::SeqCst);

    assert!(
        peak <= 5,
        "peak concurrent tasks ({}) should not exceed limit of 5",
        peak
    );

    runtime.shutdown().await.expect("shutdown should succeed");
}

/// Verifies tasks can run concurrently and interact safely.
/// Observable behavior: multiple tasks run simultaneously without data races.
#[tokio::test]
async fn test_concurrent_tasks_safe_interaction() {
    let config = BackgroundTaskConfig {
        max_queue_size: 100,
        max_concurrent_tasks: 10,
        drain_timeout_secs: 10,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let shared_value = Arc::new(AtomicU64::new(0));
    let task_count = 50;

    for _ in 0..task_count {
        let val = shared_value.clone();
        handle
            .spawn(move || {
                let v = val.clone();
                async move {
                    v.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(200)).await;

    assert_eq!(
        shared_value.load(Ordering::SeqCst),
        task_count as u64,
        "concurrent increments should all complete"
    );

    runtime.shutdown().await.expect("shutdown should succeed");
}

/// Verifies that shutdown immediately stops accepting new tasks.
/// Observable behavior: spawn after shutdown signal returns error.
#[tokio::test]
async fn test_spawn_fails_after_shutdown_initiated() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let handle_clone = handle.clone();

    runtime.shutdown().await.expect("shutdown should succeed");

    tokio::time::sleep(Duration::from_millis(50)).await;

    let result = handle_clone.spawn(|| async { Ok(()) });
    assert!(result.is_err(), "spawn after shutdown should fail");
}

/// Verifies that incomplete tasks are cancelled when shutdown times out.
/// Observable behavior: incomplete task never completes after timeout.
#[tokio::test]
async fn test_incomplete_task_cancelled_on_timeout() {
    let config = BackgroundTaskConfig {
        max_queue_size: 10,
        max_concurrent_tasks: 1,
        drain_timeout_secs: 1,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let task_started = Arc::new(AtomicBool::new(false));
    let task_completed = Arc::new(AtomicBool::new(false));
    let started = task_started.clone();
    let completed = task_completed.clone();

    handle
        .spawn(move || {
            let s = started.clone();
            let c = completed.clone();
            async move {
                s.store(true, Ordering::SeqCst);
                tokio::time::sleep(Duration::from_secs(10)).await;
                c.store(true, Ordering::SeqCst);
                Ok(())
            }
        })
        .expect("spawn failed");

    tokio::time::sleep(Duration::from_millis(100)).await;

    assert!(task_started.load(Ordering::SeqCst), "task should have started");

    let shutdown_result = runtime.shutdown().await;

    assert!(shutdown_result.is_err(), "shutdown should timeout with incomplete task");

    assert!(
        !task_completed.load(Ordering::SeqCst),
        "incomplete task should not complete after shutdown timeout"
    );
}

/// Verifies task cancellation doesn't affect other tasks.
/// Observable behavior: other tasks complete normally even if one is cancelled.
#[tokio::test]
async fn test_task_cancellation_doesnt_affect_others() {
    let config = BackgroundTaskConfig {
        max_queue_size: 100,
        max_concurrent_tasks: 5,
        drain_timeout_secs: 1,
    };

    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let short_task_completed = Arc::new(AtomicBool::new(false));
    let long_task_started = Arc::new(AtomicBool::new(false));

    {
        let c = short_task_completed.clone();
        handle
            .spawn(move || {
                let completed = c.clone();
                async move {
                    tokio::time::sleep(Duration::from_millis(50)).await;
                    completed.store(true, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    {
        let s = long_task_started.clone();
        handle
            .spawn(move || {
                let started = s.clone();
                async move {
                    started.store(true, Ordering::SeqCst);
                    tokio::time::sleep(Duration::from_secs(30)).await;
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(100)).await;

    let shutdown_result = runtime.shutdown().await;
    assert!(shutdown_result.is_err(), "shutdown should timeout due to long task");

    assert!(
        short_task_completed.load(Ordering::SeqCst),
        "short task should have completed before timeout"
    );
    assert!(
        long_task_started.load(Ordering::SeqCst),
        "long task should have started before timeout"
    );
}

/// Verifies immediate shutdown with no tasks.
/// Observable behavior: shutdown succeeds quickly with empty queue.
#[tokio::test]
async fn test_shutdown_with_no_tasks() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;

    let start = Instant::now();
    let result = runtime.shutdown().await;
    let elapsed = start.elapsed();

    assert!(result.is_ok(), "shutdown should succeed with no tasks");
    assert!(
        elapsed < Duration::from_secs(1),
        "shutdown with no tasks should be fast"
    );
}

/// Verifies task metadata is preserved (metadata doesn't affect execution).
/// Observable behavior: tasks with metadata execute successfully.
#[tokio::test]
async fn test_task_metadata_preserved_execution() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    let executed = Arc::new(AtomicBool::new(false));
    let executed_clone = executed.clone();

    let metadata = BackgroundJobMetadata {
        name: Cow::Owned("test_task".to_string()),
        request_id: Some("req-123".to_string()),
    };

    let future = async move {
        executed_clone.store(true, Ordering::SeqCst);
        Ok(())
    };

    handle.spawn_with_metadata(future, metadata).expect("spawn failed");

    tokio::time::sleep(Duration::from_millis(50)).await;

    assert!(executed.load(Ordering::SeqCst), "task with metadata should execute");

    runtime.shutdown().await.expect("shutdown should succeed");
}

/// Verifies that multiple handles to the same runtime work correctly.
/// Observable behavior: multiple handle clones spawn tasks independently.
#[tokio::test]
async fn test_multiple_handle_clones_spawn_independently() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;
    let handle1 = runtime.handle();
    let handle2 = runtime.handle();

    let count = Arc::new(AtomicU64::new(0));

    {
        let c = count.clone();
        handle1
            .spawn(move || {
                let counter = c.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    {
        let c = count.clone();
        handle2
            .spawn(move || {
                let counter = c.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            })
            .expect("spawn failed");
    }

    tokio::time::sleep(Duration::from_millis(100)).await;

    assert_eq!(
        count.load(Ordering::SeqCst),
        2,
        "tasks from multiple handles should all execute"
    );

    runtime.shutdown().await.expect("shutdown should succeed");
}

/// Verifies that resource cleanup occurs after shutdown.
/// Observable behavior: runtime can be dropped safely after shutdown.
#[tokio::test]
async fn test_resource_cleanup_after_shutdown() {
    let config = BackgroundTaskConfig::default();
    let runtime = BackgroundRuntime::start(config).await;
    let handle = runtime.handle();

    handle
        .spawn(|| async {
            tokio::time::sleep(Duration::from_millis(10)).await;
            Ok(())
        })
        .expect("spawn failed");

    let shutdown_result = runtime.shutdown().await;
    assert!(shutdown_result.is_ok(), "shutdown should complete successfully");

    drop(handle);
}