amaters-server 0.2.2

AmateRS server binary
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
use super::*;
use std::thread::sleep;

// ---- Original tests (preserved) ----

#[test]
fn test_health_checker_creation() {
    let checker = HealthChecker::new();
    assert_eq!(checker.status(), HealthStatus::Starting);
    assert!(!checker.is_ready());
    assert!(checker.is_alive());
}

#[test]
fn test_set_status() {
    let checker = HealthChecker::new();

    checker.set_status(HealthStatus::Healthy);
    assert_eq!(checker.status(), HealthStatus::Healthy);

    checker.set_status(HealthStatus::ShuttingDown);
    assert_eq!(checker.status(), HealthStatus::ShuttingDown);

    checker.set_status(HealthStatus::Unhealthy);
    assert_eq!(checker.status(), HealthStatus::Unhealthy);
}

#[test]
fn test_component_health() {
    let checker = HealthChecker::new();

    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);
    checker.set_cluster_healthy(true);
    checker.set_status(HealthStatus::Healthy);

    assert!(checker.is_ready());
    assert!(checker.is_alive());
}

#[test]
fn test_not_ready_when_components_unhealthy() {
    let checker = HealthChecker::new();

    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(false); // Storage not healthy

    assert!(!checker.is_ready());
}

#[test]
fn test_uptime() {
    let checker = HealthChecker::new();
    sleep(Duration::from_millis(100));

    let uptime = checker.uptime_seconds();
    // Uptime should be a reasonable value (u64 is always >= 0)
    assert!(uptime < 1000); // Should be less than 1000 seconds
}

#[test]
fn test_health_response() {
    let checker = HealthChecker::new();
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);
    checker.set_status(HealthStatus::Healthy);

    let health = checker.get_health();
    assert_eq!(health.status, HealthStatus::Healthy);
    assert_eq!(health.components.len(), 3);
    assert_eq!(health.version, env!("CARGO_PKG_VERSION"));
}

#[test]
fn test_health_json() {
    let checker = HealthChecker::new();
    let json = checker.get_health_json();
    assert!(json.is_ok());

    let json_str = json.expect("JSON serialization failed");
    assert!(json_str.contains("status"));
    assert!(json_str.contains("version"));
    assert!(json_str.contains("components"));
}

#[test]
fn test_is_alive() {
    let checker = HealthChecker::new();

    checker.set_status(HealthStatus::Starting);
    assert!(checker.is_alive());

    checker.set_status(HealthStatus::Healthy);
    assert!(checker.is_alive());

    checker.set_status(HealthStatus::ShuttingDown);
    assert!(!checker.is_alive());

    checker.set_status(HealthStatus::Unhealthy);
    assert!(!checker.is_alive());
}

// ---- Deep probe tests ----

/// A simple test probe that always returns healthy
struct AlwaysHealthyProbe;

#[async_trait]
impl DeepHealthCheck for AlwaysHealthyProbe {
    async fn check(&self) -> HealthProbeResult {
        HealthProbeResult {
            status: ProbeStatus::Healthy,
            latency_ms: 0.1,
            message: "always healthy".to_string(),
        }
    }
}

/// A probe that returns unhealthy
struct AlwaysUnhealthyProbe;

#[async_trait]
impl DeepHealthCheck for AlwaysUnhealthyProbe {
    async fn check(&self) -> HealthProbeResult {
        HealthProbeResult {
            status: ProbeStatus::Unhealthy,
            latency_ms: 5.0,
            message: "always unhealthy".to_string(),
        }
    }
}

/// A probe that returns degraded
struct AlwaysDegradedProbe;

#[async_trait]
impl DeepHealthCheck for AlwaysDegradedProbe {
    async fn check(&self) -> HealthProbeResult {
        HealthProbeResult {
            status: ProbeStatus::Degraded,
            latency_ms: 2.0,
            message: "always degraded".to_string(),
        }
    }
}

#[tokio::test]
async fn test_deep_probe_execution_and_result_reporting() {
    let checker = HealthChecker::new();
    checker.register_probe("test_healthy", Arc::new(AlwaysHealthyProbe));
    checker.register_probe("test_unhealthy", Arc::new(AlwaysUnhealthyProbe));

    let results = checker.run_probes().await;
    assert_eq!(results.len(), 2);

    let healthy = results.get("test_healthy").expect("missing healthy probe");
    assert_eq!(healthy.status, ProbeStatus::Healthy);
    assert_eq!(healthy.message, "always healthy");

    let unhealthy = results
        .get("test_unhealthy")
        .expect("missing unhealthy probe");
    assert_eq!(unhealthy.status, ProbeStatus::Unhealthy);
    assert_eq!(unhealthy.message, "always unhealthy");
}

#[tokio::test]
async fn test_storage_probe_passes_with_valid_storage() {
    let dir = std::env::temp_dir().join("amaters_health_test_storage");
    let _ = std::fs::create_dir_all(&dir);

    let probe = StorageProbe::new(dir.clone());
    let result = probe.check().await;

    assert_eq!(result.status, ProbeStatus::Healthy);
    assert!(result.latency_ms >= 0.0);
    assert!(result.message.contains("OK"));

    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_storage_probe_fails_with_invalid_path() {
    let probe = StorageProbe::new(std::path::PathBuf::from(
        "/nonexistent_path_for_health_check_test_12345",
    ));
    let result = probe.check().await;
    assert_eq!(result.status, ProbeStatus::Unhealthy);
}

#[tokio::test]
async fn test_wal_probe_passes() {
    let dir = std::env::temp_dir().join("amaters_health_test_wal");
    let _ = std::fs::create_dir_all(&dir);

    let probe = WalProbe::new(dir.clone());
    let result = probe.check().await;

    assert_eq!(result.status, ProbeStatus::Healthy);
    assert!(result.message.contains("appendable"));

    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_disk_space_probe_healthy() {
    // Threshold of 1 byte — should always pass on a running system
    let probe = DiskSpaceProbe::new(std::env::temp_dir(), 1);
    let result = probe.check().await;
    assert_eq!(result.status, ProbeStatus::Healthy);
}

// ---- Liveness vs readiness ----

#[test]
fn test_liveness_vs_readiness_during_startup() {
    let checker = HealthChecker::new();
    // Starting state: alive but not ready
    assert!(checker.is_alive());
    assert!(!checker.is_ready());

    let live_resp = checker.liveness_response();
    assert!(live_resp.alive);

    let ready_resp = checker.readiness_response();
    assert!(!ready_resp.ready);
}

#[test]
fn test_liveness_vs_readiness_during_shutdown() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::ShuttingDown);

    assert!(!checker.is_alive());
    assert!(!checker.is_ready());

    let live_resp = checker.liveness_response();
    assert!(!live_resp.alive);

    let ready_resp = checker.readiness_response();
    assert!(!ready_resp.ready);
}

#[test]
fn test_readiness_requires_components() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    // Storage and network still false
    assert!(!checker.is_ready());

    checker.set_storage_healthy(true);
    assert!(!checker.is_ready()); // network still down

    checker.set_network_healthy(true);
    assert!(checker.is_ready()); // now ready
}

// ---- Health history ring buffer ----

#[test]
fn test_health_history_ring_buffer_correctness() {
    let mut history = HealthHistory::new(3);

    // Record 5 entries — buffer should keep last 3
    for i in 0..5u64 {
        history.record(HealthSnapshot {
            timestamp: i,
            status: HealthStatus::Healthy,
            alive: true,
            ready: true,
        });
    }

    let snaps = history.snapshots();
    assert_eq!(snaps.len(), 3);
    // Oldest should be timestamp 2
    assert_eq!(snaps[0].timestamp, 2);
    assert_eq!(snaps[1].timestamp, 3);
    assert_eq!(snaps[2].timestamp, 4);
}

#[test]
fn test_health_history_partial_fill() {
    let mut history = HealthHistory::new(10);

    history.record(HealthSnapshot {
        timestamp: 100,
        status: HealthStatus::Healthy,
        alive: true,
        ready: true,
    });
    history.record(HealthSnapshot {
        timestamp: 200,
        status: HealthStatus::Unhealthy,
        alive: false,
        ready: false,
    });

    let snaps = history.snapshots();
    assert_eq!(snaps.len(), 2);
    assert_eq!(snaps[0].timestamp, 100);
    assert_eq!(snaps[1].timestamp, 200);
}

// ---- Uptime percentage ----

#[test]
fn test_uptime_percentage_all_alive() {
    let mut history = HealthHistory::new(5);
    for i in 0..5 {
        history.record(HealthSnapshot {
            timestamp: i,
            status: HealthStatus::Healthy,
            alive: true,
            ready: true,
        });
    }
    let pct = history.uptime_percent();
    assert!((pct - 100.0).abs() < f64::EPSILON);
}

#[test]
fn test_uptime_percentage_partial() {
    let mut history = HealthHistory::new(4);
    // 3 alive, 1 dead => 75%
    for i in 0..3 {
        history.record(HealthSnapshot {
            timestamp: i,
            status: HealthStatus::Healthy,
            alive: true,
            ready: true,
        });
    }
    history.record(HealthSnapshot {
        timestamp: 3,
        status: HealthStatus::Unhealthy,
        alive: false,
        ready: false,
    });

    let pct = history.uptime_percent();
    assert!((pct - 75.0).abs() < 0.01);
}

#[test]
fn test_uptime_percentage_empty_is_100() {
    let history = HealthHistory::new(10);
    assert!((history.uptime_percent() - 100.0).abs() < f64::EPSILON);
}

#[test]
fn test_health_checker_uptime_percent_and_history() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);

    checker.record_snapshot();
    checker.record_snapshot();

    checker.set_status(HealthStatus::Unhealthy);
    checker.record_snapshot();

    let history = checker.health_history();
    assert_eq!(history.len(), 3);

    // 2 alive, 1 not alive
    let pct = checker.uptime_percent();
    assert!((pct - 100.0 * 2.0 / 3.0).abs() < 0.01);
}

// ---- Dependency aggregation ----

#[tokio::test]
async fn test_dependency_aggregation_one_unhealthy() {
    let checker = HealthChecker::new();
    checker.register_dependency("dep_ok", Arc::new(AlwaysHealthyProbe));
    checker.register_dependency("dep_bad", Arc::new(AlwaysUnhealthyProbe));

    let worst = checker.check_dependencies().await;
    assert_eq!(worst, ProbeStatus::Unhealthy);

    // Aggregated should also be unhealthy
    assert_eq!(
        checker.aggregated_dependency_status(),
        ProbeStatus::Unhealthy
    );
}

#[tokio::test]
async fn test_dependency_aggregation_all_healthy() {
    let checker = HealthChecker::new();
    checker.register_dependency("dep_a", Arc::new(AlwaysHealthyProbe));
    checker.register_dependency("dep_b", Arc::new(AlwaysHealthyProbe));

    let worst = checker.check_dependencies().await;
    assert_eq!(worst, ProbeStatus::Healthy);
}

#[tokio::test]
async fn test_dependency_health_in_readiness_response() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);
    checker.register_dependency("cache", Arc::new(AlwaysHealthyProbe));

    let _ = checker.check_dependencies().await;

    let resp = checker.readiness_response();
    assert!(resp.ready);
    assert_eq!(resp.dependencies.len(), 1);
    assert_eq!(resp.dependencies[0].name, "cache");
    assert_eq!(resp.dependencies[0].status, ProbeStatus::Healthy);
}

// ---- Degraded state ----

#[test]
fn test_degraded_state_alive_and_ready() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Degraded);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);

    // Degraded is alive and can be ready
    assert!(checker.is_alive());
    assert!(checker.is_ready());
}

#[tokio::test]
async fn test_degraded_dependency_aggregation() {
    let checker = HealthChecker::new();
    checker.register_dependency("dep_ok", Arc::new(AlwaysHealthyProbe));
    checker.register_dependency("dep_degraded", Arc::new(AlwaysDegradedProbe));

    let worst = checker.check_dependencies().await;
    assert_eq!(worst, ProbeStatus::Degraded);
}

// ---- Concurrent health checks ----

#[tokio::test]
async fn test_concurrent_health_checks() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);
    checker.register_probe("probe_a", Arc::new(AlwaysHealthyProbe));
    checker.register_dependency("dep_a", Arc::new(AlwaysHealthyProbe));

    // Run multiple operations concurrently
    let checker_clone1 = checker.clone();
    let checker_clone2 = checker.clone();
    let checker_clone3 = checker.clone();

    let (r1, r2, r3) = tokio::join!(
        async move { checker_clone1.run_probes().await },
        async move { checker_clone2.check_dependencies().await },
        async move {
            checker_clone3.record_snapshot();
            checker_clone3.health_history()
        },
    );

    assert_eq!(r1.len(), 1);
    assert_eq!(r2, ProbeStatus::Healthy);
    assert!(!r3.is_empty());
}

// ---- ProbeStatus::worse ----

#[test]
fn test_probe_status_worse() {
    assert_eq!(
        ProbeStatus::Healthy.worse(ProbeStatus::Healthy),
        ProbeStatus::Healthy
    );
    assert_eq!(
        ProbeStatus::Healthy.worse(ProbeStatus::Degraded),
        ProbeStatus::Degraded
    );
    assert_eq!(
        ProbeStatus::Degraded.worse(ProbeStatus::Healthy),
        ProbeStatus::Degraded
    );
    assert_eq!(
        ProbeStatus::Healthy.worse(ProbeStatus::Unhealthy),
        ProbeStatus::Unhealthy
    );
    assert_eq!(
        ProbeStatus::Degraded.worse(ProbeStatus::Unhealthy),
        ProbeStatus::Unhealthy
    );
}

// ---- Deep health response ----

#[tokio::test]
async fn test_get_health_deep_includes_probes() {
    let checker = HealthChecker::new();
    checker.register_probe("deep_test", Arc::new(AlwaysHealthyProbe));

    let resp = checker.get_health_deep().await;
    assert_eq!(resp.probes.len(), 1);
    let probe_result = resp.probes.get("deep_test").expect("missing probe result");
    assert_eq!(probe_result.status, ProbeStatus::Healthy);
}

// ---- HTTP health server tests ----

async fn start_test_server(checker: HealthChecker) -> HealthHttpHandle {
    let addr: SocketAddr = "127.0.0.1:0".parse().expect("valid addr");
    HealthHttpServer::new(Arc::new(checker), addr)
        .start()
        .await
        .expect("failed to start health HTTP server")
}

async fn http_request(port: u16, method: &str, path: &str) -> (u16, String) {
    let mut stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{port}"))
        .await
        .expect("failed to connect");
    let req = format!("{method} {path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
    stream.write_all(req.as_bytes()).await.expect("write");
    let mut resp = String::new();
    stream.read_to_string(&mut resp).await.expect("read");
    let line = resp.lines().next().unwrap_or("");
    let code: u16 = line
        .split_whitespace()
        .nth(1)
        .and_then(|s| s.parse().ok())
        .unwrap_or(0);
    let body = resp.split("\r\n\r\n").nth(1).unwrap_or("").to_string();
    (code, body)
}

async fn http_get(port: u16, path: &str) -> (u16, String) {
    http_request(port, "GET", path).await
}

#[tokio::test]
async fn test_health_http_server_starts() {
    let checker = HealthChecker::new();
    let handle = start_test_server(checker).await;
    let port = handle.port();
    assert!(port > 0);

    // Verify we can connect
    let result = tokio::net::TcpStream::connect(format!("127.0.0.1:{port}")).await;
    assert!(result.is_ok());

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_health_endpoint() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/health").await;
    assert_eq!(status, 200);
    assert!(body.contains("\"status\":\"healthy\""));
    assert!(body.contains("\"version\""));
    assert!(body.contains("\"components\""));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_healthz_endpoint() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/healthz").await;
    assert_eq!(status, 200);
    assert!(body.contains("\"alive\":true"));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_healthz_unhealthy() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Unhealthy);

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/healthz").await;
    assert_eq!(status, 503);
    assert!(body.contains("\"alive\":false"));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_readyz_endpoint() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/readyz").await;
    assert_eq!(status, 200);
    assert!(body.contains("\"ready\":true"));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_readyz_not_ready() {
    let checker = HealthChecker::new();
    // Starting status — not ready (storage/network not set)

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/readyz").await;
    assert_eq!(status, 503);
    assert!(body.contains("\"ready\":false"));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_livez_endpoint() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/livez").await;
    assert_eq!(status, 200);
    assert!(body.contains("\"alive\":true"));
    assert!(body.contains("\"uptime_seconds\""));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_metrics_endpoint() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);
    checker.record_snapshot();
    checker.record_snapshot();

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/metrics").await;
    assert_eq!(status, 200);
    assert!(body.contains("\"uptime_seconds\""));
    assert!(body.contains("\"uptime_percent\""));
    assert!(body.contains("\"history_count\":2"));
    assert!(body.contains("\"history\""));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_unknown_path_404() {
    let checker = HealthChecker::new();

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_get(port, "/unknown").await;
    assert_eq!(status, 404);
    assert!(body.contains("not found"));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_non_get_method_405() {
    let checker = HealthChecker::new();

    let handle = start_test_server(checker).await;
    let port = handle.port();

    let (status, body) = http_request(port, "POST", "/health").await;
    assert_eq!(status, 405);
    assert!(body.contains("method not allowed"));

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_concurrent_http_requests() {
    let checker = HealthChecker::new();
    checker.set_status(HealthStatus::Healthy);
    checker.set_storage_healthy(true);
    checker.set_network_healthy(true);

    let handle = start_test_server(checker).await;
    let port = handle.port();

    // Fire 10 concurrent requests across different endpoints
    let mut tasks = Vec::new();
    for i in 0..10 {
        let path = match i % 4 {
            0 => "/health",
            1 => "/healthz",
            2 => "/readyz",
            _ => "/livez",
        };
        tasks.push(tokio::spawn(async move { http_get(port, path).await }));
    }

    for task in tasks {
        let (status, _body) = task.await.expect("task panicked");
        assert_eq!(status, 200);
    }

    handle.stop();
    let _ = handle.join().await;
}

#[tokio::test]
async fn test_server_shutdown() {
    let checker = HealthChecker::new();

    let handle = start_test_server(checker).await;
    let port = handle.port();

    // Verify server is listening
    let (status, _) = http_get(port, "/healthz").await;
    assert_eq!(status, 200);

    // Signal shutdown
    handle.stop();
    let result = handle.join().await;
    assert!(result.is_ok());

    // After shutdown, connection should fail (with a small delay for cleanup)
    tokio::time::sleep(Duration::from_millis(300)).await;
    let connect_result = tokio::time::timeout(
        Duration::from_millis(500),
        tokio::net::TcpStream::connect(format!("127.0.0.1:{port}")),
    )
    .await;

    // Either timeout or connection refused — both are acceptable
    match connect_result {
        Err(_) => {}     // timeout — server stopped
        Ok(Err(_)) => {} // connection refused — server stopped
        Ok(Ok(_)) => {
            // Connection succeeded — this can happen if the OS hasn't fully
            // released the port yet; we just verify the server task exited.
        }
    }
}