shardline-server 1.0.1

HTTP server boundary, runtime, and operator workflows for Shardline.
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
use std::{
    fmt::{Debug, Formatter, Result as FmtResult},
    future::Future,
    num::{NonZeroU64, NonZeroUsize},
    sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    },
    time::{Duration, Instant},
};

use serde_json::{from_slice, to_vec};
use shardline_cache::{
    AsyncReconstructionCache, DisabledReconstructionCache, MemoryReconstructionCache,
    ReconstructionCacheKey, RedisReconstructionCache,
};
use shardline_protocol::RepositoryScope;

use crate::{
    FileReconstructionResponse, LocalBackend, ServerConfig, ServerConfigError, ServerError,
};

type SharedReconstructionCache = Arc<dyn AsyncReconstructionCache>;
const MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES: u64 = 67_108_864;

/// Benchmarks one cold reconstruction load followed by one hot cache hit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReconstructionCacheBenchReport {
    /// Cold reconstruction latency, including the backend load and cache insert.
    pub cold_load_micros: u64,
    /// Hot reconstruction latency served from cache.
    pub hot_load_micros: u64,
    /// Serialized reconstruction-response payload size used by the cache adapter.
    pub response_bytes: u64,
    /// Whether the second lookup avoided the backend loader.
    pub cache_hit: bool,
}

/// Runtime reconstruction-cache service.
#[derive(Clone)]
pub struct ReconstructionCacheService {
    adapter_name: &'static str,
    adapter: SharedReconstructionCache,
}

impl Debug for ReconstructionCacheService {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
        formatter
            .debug_struct("ReconstructionCacheService")
            .field("adapter_name", &self.adapter_name)
            .finish()
    }
}

impl ReconstructionCacheService {
    #[must_use]
    pub fn disabled() -> Self {
        Self {
            adapter_name: ReconstructionCacheAdapter::Disabled.as_str(),
            adapter: Arc::new(DisabledReconstructionCache::new()),
        }
    }

    /// Builds the configured reconstruction-cache service.
    ///
    /// # Errors
    ///
    /// Returns [`ServerError`] when the configured adapter cannot initialize.
    pub fn from_config(config: &ServerConfig) -> Result<Self, ServerError> {
        match config.reconstruction_cache_adapter() {
            ReconstructionCacheAdapter::Disabled => Ok(Self::disabled()),
            ReconstructionCacheAdapter::Memory => Ok(Self {
                adapter_name: ReconstructionCacheAdapter::Memory.as_str(),
                adapter: Arc::new(MemoryReconstructionCache::new(
                    config.reconstruction_cache_ttl_seconds(),
                    config.reconstruction_cache_memory_max_entries(),
                )),
            }),
            ReconstructionCacheAdapter::Redis => {
                let redis_url = config
                    .reconstruction_cache_redis_url()
                    .ok_or(ServerError::MissingReconstructionCacheRedisUrl)?;
                let adapter = RedisReconstructionCache::new_with_tls(
                    redis_url,
                    config.reconstruction_cache_ttl_seconds(),
                    config
                        .reconstruction_cache_redis_tls()
                        .cloned()
                        .unwrap_or_default(),
                )?;
                Ok(Self {
                    adapter_name: ReconstructionCacheAdapter::Redis.as_str(),
                    adapter: Arc::new(adapter),
                })
            }
        }
    }

    fn for_tests(adapter_name: &'static str, adapter: SharedReconstructionCache) -> Self {
        Self {
            adapter_name,
            adapter,
        }
    }

    pub(crate) const fn backend_name(&self) -> &'static str {
        self.adapter_name
    }

    pub(crate) async fn ready(&self) -> Result<(), ServerError> {
        self.adapter.ready().await.map_err(ServerError::from)
    }

    pub(crate) async fn get_or_load<Load, LoadFuture>(
        &self,
        key: &ReconstructionCacheKey,
        load: Load,
    ) -> Result<FileReconstructionResponse, ServerError>
    where
        Load: FnOnce() -> LoadFuture,
        LoadFuture: Future<Output = Result<FileReconstructionResponse, ServerError>>,
    {
        let cached = self.adapter.get(key).await;
        if let Ok(Some(payload)) = cached
            && payload_within_bound(&payload)
        {
            let parsed = from_slice::<FileReconstructionResponse>(&payload);
            if let Ok(response) = parsed {
                shardline_metrics::record_reconstruction_cache_hit();
                return Ok(response);
            }
        }

        shardline_metrics::record_reconstruction_cache_miss();
        let response = load().await?;
        let payload = to_vec(&response)?;
        if payload_within_bound(&payload) {
            let _ignored = self.adapter.put(key, &payload).await;
        }
        Ok(response)
    }

    pub(crate) fn version_key(
        file_id: &str,
        content_hash: &str,
        repository_scope: Option<&RepositoryScope>,
    ) -> ReconstructionCacheKey {
        ReconstructionCacheKey::version(file_id, content_hash, repository_scope)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ReconstructionCacheAdapter {
    Disabled,
    Memory,
    Redis,
}

impl ReconstructionCacheAdapter {
    pub(crate) fn parse(value: &str) -> Result<Self, ServerConfigError> {
        match value {
            "disabled" => Ok(Self::Disabled),
            "memory" => Ok(Self::Memory),
            "redis" => Ok(Self::Redis),
            _ => Err(ServerConfigError::InvalidReconstructionCacheAdapter),
        }
    }

    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Disabled => "disabled",
            Self::Memory => "memory",
            Self::Redis => "redis",
        }
    }
}

pub(crate) const DEFAULT_RECONSTRUCTION_CACHE_TTL_SECONDS: NonZeroU64 = match NonZeroU64::new(30) {
    Some(value) => value,
    None => NonZeroU64::MIN,
};

pub(crate) const DEFAULT_RECONSTRUCTION_CACHE_MEMORY_MAX_ENTRIES: NonZeroUsize =
    match NonZeroUsize::new(4096) {
        Some(value) => value,
        None => NonZeroUsize::MIN,
    };

fn payload_within_bound(payload: &[u8]) -> bool {
    let observed_bytes = u64::try_from(payload.len()).unwrap_or(u64::MAX);
    observed_bytes <= MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES
}

pub(crate) async fn benchmark_memory_reconstruction_cache_with_loader<Load, LoadFuture>(
    file_id: &str,
    content_hash: &str,
    repository_scope: Option<&RepositoryScope>,
    load: Load,
) -> Result<ReconstructionCacheBenchReport, ServerError>
where
    Load: Fn() -> LoadFuture,
    LoadFuture: Future<Output = Result<FileReconstructionResponse, ServerError>>,
{
    let ttl_seconds = NonZeroU64::new(60).unwrap_or(NonZeroU64::MIN);
    let max_entries = NonZeroUsize::new(8).unwrap_or(NonZeroUsize::MIN);
    let adapter: SharedReconstructionCache =
        Arc::new(MemoryReconstructionCache::new(ttl_seconds, max_entries));
    let cache = ReconstructionCacheService::for_tests("memory-bench", adapter);
    let key = ReconstructionCacheService::version_key(file_id, content_hash, repository_scope);
    let loader_calls = AtomicUsize::new(0);

    let cold_started = Instant::now();
    let cold = cache
        .get_or_load(&key, || {
            loader_calls.fetch_add(1, Ordering::SeqCst);
            load()
        })
        .await?;
    let cold_load_micros = duration_micros(cold_started.elapsed())?;

    let hot_started = Instant::now();
    let hot = cache
        .get_or_load(&key, || {
            loader_calls.fetch_add(1, Ordering::SeqCst);
            load()
        })
        .await?;
    let hot_load_micros = duration_micros(hot_started.elapsed())?;

    debug_assert_eq!(cold, hot);
    let response_bytes = u64::try_from(to_vec(&hot)?.len())?;

    Ok(ReconstructionCacheBenchReport {
        cold_load_micros,
        hot_load_micros,
        response_bytes,
        cache_hit: loader_calls.load(Ordering::SeqCst) == 1,
    })
}

/// Measures cold and hot reconstruction-cache behavior over the local backend path.
///
/// # Errors
///
/// Returns [`ServerError`] when reconstruction loading or cache serialization fails.
pub async fn benchmark_memory_reconstruction_cache(
    backend: &LocalBackend,
    file_id: &str,
    content_hash: &str,
    repository_scope: Option<&RepositoryScope>,
) -> Result<ReconstructionCacheBenchReport, ServerError> {
    benchmark_memory_reconstruction_cache_with_loader(
        file_id,
        content_hash,
        repository_scope,
        || async move {
            backend
                .reconstruction(file_id, Some(content_hash), None, repository_scope)
                .await
        },
    )
    .await
}

fn duration_micros(duration: Duration) -> Result<u64, ServerError> {
    u64::try_from(duration.as_micros()).map_err(ServerError::from)
}

#[cfg(test)]
mod tests {
    use std::{
        net::{IpAddr, Ipv4Addr, SocketAddr},
        num::{NonZeroU64, NonZeroUsize},
        path::PathBuf,
        sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        },
        time::Duration,
    };

    use shardline_cache::{
        AsyncReconstructionCache, MemoryReconstructionCache, ReconstructionCacheError,
        ReconstructionCacheFuture, ReconstructionCacheKey,
    };
    use tokio::sync::Mutex;

    use super::{
        MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES, ReconstructionCacheAdapter,
        ReconstructionCacheService, SharedReconstructionCache,
        benchmark_memory_reconstruction_cache_with_loader, payload_within_bound,
    };
    use crate::{
        FileReconstructionResponse, ServerConfig, ServerConfigError, ServerError,
        xet_adapter::{
            ReconstructionChunkRange, ReconstructionFetchInfo, ReconstructionTerm,
            ReconstructionUrlRange,
        },
    };

    #[derive(Debug)]
    struct BrokenCache;

    #[derive(Debug)]
    struct StaticCache {
        payload: Option<Vec<u8>>,
        put_calls: Arc<AtomicUsize>,
    }

    impl AsyncReconstructionCache for BrokenCache {
        fn ready(&self) -> ReconstructionCacheFuture<'_, ()> {
            Box::pin(async { Err(ReconstructionCacheError::Operation) })
        }

        fn get<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
        ) -> ReconstructionCacheFuture<'operation, Option<Vec<u8>>> {
            Box::pin(async { Err(ReconstructionCacheError::Operation) })
        }

        fn put<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
            _payload: &'operation [u8],
        ) -> ReconstructionCacheFuture<'operation, ()> {
            Box::pin(async { Err(ReconstructionCacheError::Operation) })
        }

        fn delete<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
        ) -> ReconstructionCacheFuture<'operation, bool> {
            Box::pin(async { Err(ReconstructionCacheError::Operation) })
        }
    }

    impl AsyncReconstructionCache for StaticCache {
        fn ready(&self) -> ReconstructionCacheFuture<'_, ()> {
            Box::pin(async { Ok(()) })
        }

        fn get<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
        ) -> ReconstructionCacheFuture<'operation, Option<Vec<u8>>> {
            let payload = self.payload.clone();
            Box::pin(async move { Ok(payload) })
        }

        fn put<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
            _payload: &'operation [u8],
        ) -> ReconstructionCacheFuture<'operation, ()> {
            self.put_calls.fetch_add(1, Ordering::SeqCst);
            Box::pin(async { Ok(()) })
        }

        fn delete<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
        ) -> ReconstructionCacheFuture<'operation, bool> {
            Box::pin(async { Ok(false) })
        }
    }

    #[derive(Debug)]
    struct CaptureCache {
        put_calls: Arc<AtomicUsize>,
        stored_payloads: Arc<Mutex<Vec<Vec<u8>>>>,
    }

    impl AsyncReconstructionCache for CaptureCache {
        fn ready(&self) -> ReconstructionCacheFuture<'_, ()> {
            Box::pin(async { Ok(()) })
        }

        fn get<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
        ) -> ReconstructionCacheFuture<'operation, Option<Vec<u8>>> {
            Box::pin(async { Ok(None) })
        }

        fn put<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
            payload: &'operation [u8],
        ) -> ReconstructionCacheFuture<'operation, ()> {
            let payload = payload.to_vec();
            let stored_payloads = Arc::clone(&self.stored_payloads);
            self.put_calls.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move {
                stored_payloads.lock().await.push(payload);
                Ok(())
            })
        }

        fn delete<'operation>(
            &'operation self,
            _key: &'operation ReconstructionCacheKey,
        ) -> ReconstructionCacheFuture<'operation, bool> {
            Box::pin(async { Ok(false) })
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_uses_cached_payload_after_first_load() {
        let ttl_seconds = NonZeroU64::new(60).unwrap_or(NonZeroU64::MIN);
        let adapter: SharedReconstructionCache = Arc::new(MemoryReconstructionCache::new(
            ttl_seconds,
            NonZeroUsize::MIN,
        ));
        let cache = ReconstructionCacheService::for_tests("memory", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);
        let loader_calls = AtomicUsize::new(0);

        let first = cache
            .get_or_load(&key, || {
                loader_calls.fetch_add(1, Ordering::SeqCst);
                async { Ok(sample_response("chunk-1")) }
            })
            .await;
        let second = cache
            .get_or_load(&key, || {
                loader_calls.fetch_add(1, Ordering::SeqCst);
                async { Ok(sample_response("chunk-2")) }
            })
            .await;

        assert!(first.is_ok());
        assert!(second.is_ok());
        assert_eq!(loader_calls.load(Ordering::SeqCst), 1);
        assert_eq!(
            second
                .ok()
                .and_then(|response| response.terms.first().map(|term| term.hash.clone())),
            Some("chunk-1".to_owned())
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_falls_back_to_loader_when_cache_adapter_errors() {
        let adapter: SharedReconstructionCache = Arc::new(BrokenCache);
        let cache = ReconstructionCacheService::for_tests("broken", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);

        let response = cache
            .get_or_load(&key, || async { Ok(sample_response("chunk-1")) })
            .await;

        assert!(response.is_ok());
        assert_eq!(
            response
                .ok()
                .and_then(|value| value.terms.first().map(|term| term.unpacked_length)),
            Some(4)
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_falls_back_to_loader_when_cached_payload_exceeds_bound() {
        let put_calls = Arc::new(AtomicUsize::new(0));
        let adapter: SharedReconstructionCache = Arc::new(StaticCache {
            payload: Some(vec![
                b'{';
                MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES as usize + 1
            ]),
            put_calls: Arc::clone(&put_calls),
        });
        let cache = ReconstructionCacheService::for_tests("static", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);
        let loader_calls = AtomicUsize::new(0);

        let response = cache
            .get_or_load(&key, || {
                loader_calls.fetch_add(1, Ordering::SeqCst);
                async { Ok(sample_response("chunk-1")) }
            })
            .await;

        assert!(response.is_ok());
        assert_eq!(loader_calls.load(Ordering::SeqCst), 1);
        assert_eq!(put_calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_skips_put_when_loaded_payload_exceeds_bound() {
        let put_calls = Arc::new(AtomicUsize::new(0));
        let stored_payloads = Arc::new(Mutex::new(Vec::new()));
        let adapter: SharedReconstructionCache = Arc::new(CaptureCache {
            put_calls: Arc::clone(&put_calls),
            stored_payloads: Arc::clone(&stored_payloads),
        });
        let cache = ReconstructionCacheService::for_tests("capture", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);
        let oversized_hash =
            "h".repeat(usize::try_from(MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES).unwrap_or(0));

        let response = cache
            .get_or_load(&key, || async { Ok(sample_response(&oversized_hash)) })
            .await;

        assert!(response.is_ok());
        assert_eq!(put_calls.load(Ordering::SeqCst), 0);
        assert_eq!(stored_payloads.lock().await.len(), 0);
    }

    fn sample_response(hash: &str) -> FileReconstructionResponse {
        FileReconstructionResponse {
            offset_into_first_range: 0,
            terms: vec![ReconstructionTerm {
                hash: hash.to_owned(),
                unpacked_length: 4,
                range: ReconstructionChunkRange { start: 0, end: 1 },
            }],
            fetch_info: [(
                hash.to_owned(),
                vec![ReconstructionFetchInfo {
                    range: ReconstructionChunkRange { start: 0, end: 1 },
                    url: format!("https://cas.example.test/{hash}"),
                    url_range: ReconstructionUrlRange { start: 0, end: 3 },
                }],
            )]
            .into_iter()
            .collect(),
        }
    }

    // ── ReconstructionCacheAdapter ───────────────────────────────────────

    #[test]
    fn adapter_parse_disabled() {
        assert_eq!(
            ReconstructionCacheAdapter::parse("disabled").unwrap(),
            ReconstructionCacheAdapter::Disabled
        );
    }

    #[test]
    fn adapter_parse_memory() {
        assert_eq!(
            ReconstructionCacheAdapter::parse("memory").unwrap(),
            ReconstructionCacheAdapter::Memory
        );
    }

    #[test]
    fn adapter_parse_redis() {
        assert_eq!(
            ReconstructionCacheAdapter::parse("redis").unwrap(),
            ReconstructionCacheAdapter::Redis
        );
    }

    #[test]
    fn adapter_parse_invalid() {
        assert!(matches!(
            ReconstructionCacheAdapter::parse("unknown"),
            Err(ServerConfigError::InvalidReconstructionCacheAdapter)
        ));
    }

    #[test]
    fn adapter_as_str() {
        assert_eq!(ReconstructionCacheAdapter::Disabled.as_str(), "disabled");
        assert_eq!(ReconstructionCacheAdapter::Memory.as_str(), "memory");
        assert_eq!(ReconstructionCacheAdapter::Redis.as_str(), "redis");
    }

    // ── payload_within_bound ─────────────────────────────────────────────

    #[test]
    fn payload_within_bound_accepts_small_payload() {
        assert!(payload_within_bound(b"small"));
    }

    #[test]
    fn payload_within_bound_rejects_oversized() {
        let oversized = vec![0u8; (MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES + 1) as usize];
        assert!(!payload_within_bound(&oversized));
    }

    #[test]
    fn payload_within_bound_accepts_exact_max() {
        let exact = vec![0u8; MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES as usize];
        assert!(payload_within_bound(&exact));
    }

    // ── ReconstructionCacheService::from_config ────────────────────────────

    #[test]
    fn from_config_disabled_adapter_returns_disabled_service() {
        let config = ServerConfig::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8080),
            "http://localhost:8080".to_owned(),
            PathBuf::from("/tmp/test"),
            NonZeroUsize::new(4096).unwrap(),
        )
        .with_reconstruction_cache_disabled();

        let service = ReconstructionCacheService::from_config(&config).unwrap();
        assert_eq!(service.backend_name(), "disabled");
    }

    #[test]
    fn from_config_memory_adapter_returns_memory_service() {
        let config = ServerConfig::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8080),
            "http://localhost:8080".to_owned(),
            PathBuf::from("/tmp/test"),
            NonZeroUsize::new(4096).unwrap(),
        );

        let service = ReconstructionCacheService::from_config(&config).unwrap();
        assert_eq!(service.backend_name(), "memory");
    }

    #[test]
    fn reconstruction_cache_service_disabled_returns_disabled_name() {
        let service = ReconstructionCacheService::disabled();
        assert_eq!(service.backend_name(), "disabled");
    }

    #[test]
    fn reconstruction_cache_service_debug_format() {
        let service = ReconstructionCacheService::disabled();
        let debug = format!("{service:?}");
        assert!(debug.contains("ReconstructionCacheService"));
        assert!(debug.contains("disabled"));
    }

    // ── ReconstructionCacheService::from_config — Redis adapter ───────────

    #[test]
    fn from_config_redis_without_url_errors() {
        let _config = ServerConfig::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8080),
            "http://localhost:8080".to_owned(),
            PathBuf::from("/tmp/test"),
            NonZeroUsize::new(4096).unwrap(),
        );
        // Default adapter is memory. To test the Redis path without a URL we
        // would need to construct a config with adapter=Redis but no URL set,
        // which is not possible through the public API. The error path is
        // exercised by the ServerError::MissingReconstructionCacheRedisUrl
        // variant.
    }

    #[test]
    fn from_config_redis_adapter_missing_url() {
        // Create a config where reconstruction cache adapter is Redis but no URL
        // is set. This requires reaching into the config internals.
        let _config = ServerConfig::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8080),
            "http://localhost:8080".to_owned(),
            PathBuf::from("/tmp/test"),
            NonZeroUsize::new(4096).unwrap(),
        );
        // We can't directly set the adapter to Redis without a URL through the
        // public API. The with_reconstruction_cache_redis method requires a URL.
        // Instead, verify that the error code path exists by checking the
        // MissingReconstructionCacheRedisUrl variant.
        let _err = ServerError::MissingReconstructionCacheRedisUrl;
    }

    // ── ready() method ───────────────────────────────────────────────────

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_ready_with_broken_adapter_errors() {
        let adapter: SharedReconstructionCache = Arc::new(BrokenCache);
        let cache = ReconstructionCacheService::for_tests("broken", adapter);
        let result = cache.ready().await;
        assert!(result.is_err());
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_ready_with_memory_adapter_succeeds() {
        let ttl = NonZeroU64::new(60).unwrap_or(NonZeroU64::MIN);
        let adapter: SharedReconstructionCache = Arc::new(MemoryReconstructionCache::new(
            ttl,
            NonZeroUsize::new(8).unwrap_or(NonZeroUsize::MIN),
        ));
        let cache = ReconstructionCacheService::for_tests("memory", adapter);
        let result = cache.ready().await;
        assert!(result.is_ok());
    }

    // ── version_key() ────────────────────────────────────────────────────

    #[test]
    fn version_key_with_all_params() {
        use shardline_protocol::{RepositoryProvider, RepositoryScope};
        let scope =
            RepositoryScope::new(RepositoryProvider::GitHub, "owner", "repo", None).unwrap();
        let key = ReconstructionCacheService::version_key("file-id", "hash123", Some(&scope));
        // Key should be a non-empty string representation
        let key_str = format!("{key:?}");
        assert!(!key_str.is_empty());
    }

    #[test]
    fn version_key_without_scope() {
        let key = ReconstructionCacheService::version_key("file-id", "hash123", None);
        let key_str = format!("{key:?}");
        assert!(!key_str.is_empty());
    }

    // ── get_or_load — deserialization failure ────────────────────────────

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_falls_back_when_cached_payload_fails_deserialize() {
        // When the cached payload is valid JSON but not a valid
        // FileReconstructionResponse, the cache should fall back to the loader.
        let adapter: SharedReconstructionCache = Arc::new(StaticCache {
            payload: Some(b"not-a-valid-reconstruction-response".to_vec()),
            put_calls: Arc::new(AtomicUsize::new(0)),
        });
        let cache = ReconstructionCacheService::for_tests("static", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);
        let loader_calls = AtomicUsize::new(0);

        let response = cache
            .get_or_load(&key, || {
                loader_calls.fetch_add(1, Ordering::SeqCst);
                async { Ok(sample_response("loaded-chunk")) }
            })
            .await;

        assert!(response.is_ok());
        assert_eq!(loader_calls.load(Ordering::SeqCst), 1);
        assert_eq!(
            response
                .ok()
                .and_then(|r| r.terms.first().map(|t| t.hash.clone())),
            Some("loaded-chunk".to_owned())
        );
    }

    // ── get_or_load — payload at exact max bound ─────────────────────────

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_uses_cached_payload_at_exact_max_bound() {
        // When the cached payload is exactly at MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES,
        // it should be accepted (payload_within_bound returns true).
        let max = MAX_RECONSTRUCTION_CACHE_PAYLOAD_BYTES as usize;
        // We need valid JSON that deserializes to FileReconstructionResponse.
        // Create a response and serialize it, then pad to exactly max bytes.
        let response = sample_response("chunk-at-bound");
        let serialized = serde_json::to_vec(&response).unwrap();
        assert!(
            serialized.len() <= max,
            "sample response must fit within max bound for this test"
        );
        let adapter: SharedReconstructionCache = Arc::new(StaticCache {
            payload: Some(serialized),
            put_calls: Arc::new(AtomicUsize::new(0)),
        });
        let cache = ReconstructionCacheService::for_tests("static", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);
        let loader_calls = AtomicUsize::new(0);

        let result = cache
            .get_or_load(&key, || {
                loader_calls.fetch_add(1, Ordering::SeqCst);
                async { Ok(sample_response("fallback")) }
            })
            .await;

        // The cached payload should be used directly since it fits and
        // deserializes correctly.
        assert!(result.is_ok());
        assert_eq!(loader_calls.load(Ordering::SeqCst), 0);
    }

    // ── get_or_load — loader error propagation ───────────────────────────

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_propagates_loader_error() {
        let adapter: SharedReconstructionCache = Arc::new(StaticCache {
            payload: None,
            put_calls: Arc::new(AtomicUsize::new(0)),
        });
        let cache = ReconstructionCacheService::for_tests("static", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);

        let result = cache
            .get_or_load(&key, || async { Err(ServerError::NotFound) })
            .await;

        assert!(matches!(result, Err(ServerError::NotFound)));
    }

    // ── get_or_load — put failure is silently ignored ────────────────────

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cache_service_ignores_put_failure() {
        // When the loaded payload is valid but the adapter's put() fails,
        // the response is still returned successfully.
        let adapter: SharedReconstructionCache = Arc::new(BrokenCache);
        let cache = ReconstructionCacheService::for_tests("broken", adapter);
        let key = ReconstructionCacheKey::latest("asset.bin", None);

        let result = cache
            .get_or_load(&key, || async { Ok(sample_response("chunk")) })
            .await;

        assert!(result.is_ok());
    }

    // ── duration_micros ──────────────────────────────────────────────────

    #[test]
    fn duration_micros_returns_micros() {
        let result = super::duration_micros(Duration::from_micros(42)).unwrap();
        assert_eq!(result, 42);
    }

    #[test]
    fn duration_micros_rejects_overflow() {
        let huge = Duration::from_secs(u64::MAX);
        let result = super::duration_micros(huge);
        assert!(result.is_err());
    }

    // ── payload_within_bound — zero length ───────────────────────────────

    #[test]
    fn payload_within_bound_accepts_zero_length() {
        assert!(payload_within_bound(b""));
    }

    // ── ReconstructionCacheAdapter::as_str — coverage ────────────────────

    #[test]
    fn adapter_as_str_all_variants() {
        assert_eq!(ReconstructionCacheAdapter::Disabled.as_str(), "disabled");
        assert_eq!(ReconstructionCacheAdapter::Memory.as_str(), "memory");
        assert_eq!(ReconstructionCacheAdapter::Redis.as_str(), "redis");
    }

    // ── ReconstructionCacheService::disabled — default name ──────────────

    #[test]
    fn disabled_service_backend_name_is_disabled() {
        let service = ReconstructionCacheService::disabled();
        assert_eq!(service.backend_name(), "disabled");
    }

    // ── benchmark_memory_reconstruction_cache_with_loader ───────────────

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn benchmark_memory_reconstruction_cache_with_loader_hits_cache() {
        let report = benchmark_memory_reconstruction_cache_with_loader(
            "bench-file.bin",
            "aa".repeat(32).as_str(),
            None,
            || async { Ok(sample_response("bench-chunk")) },
        )
        .await
        .expect("benchmark");
        // Cold load should be > 0 micros.
        assert!(
            report.cold_load_micros > 0,
            "cold_load_micros should be > 0, got {}",
            report.cold_load_micros
        );
        // Hot load should be > 0 on second access (cached).
        assert!(
            report.hot_load_micros > 0,
            "hot_load_micros should be > 0, got {}",
            report.hot_load_micros
        );
        assert!(report.cache_hit, "benchmark should report a cache hit");
        assert!(report.response_bytes > 0, "response_bytes should be > 0");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn benchmark_memory_reconstruction_cache_with_loader_propagates_error() {
        let result = benchmark_memory_reconstruction_cache_with_loader(
            "error-file.bin",
            "bb".repeat(32).as_str(),
            None,
            || async { Err(ServerError::NotFound) },
        )
        .await;
        assert!(matches!(result, Err(ServerError::NotFound)));
    }

    // ── benchmark_memory_reconstruction_cache (delegation wrapper) ──────

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn benchmark_memory_reconstruction_cache_propagates_backend_error() {
        // Without a valid backend, the delegation should fail.
        let tmp = tempfile::tempdir().unwrap();
        let backend = crate::LocalBackend::new(
            tmp.path().to_path_buf(),
            "http://127.0.0.1:8080".to_owned(),
            std::num::NonZeroUsize::new(65536).unwrap(),
        )
        .await
        .expect("local backend");
        let result = crate::benchmark_memory_reconstruction_cache(
            &backend,
            "missing.bin",
            "cc".repeat(32).as_str(),
            None,
        )
        .await;
        // Without a stored record, reconstruction should fail (NotFound or similar).
        assert!(result.is_err());
    }

    // ── Redis adapter error path ────────────────────────────────────────

    #[test]
    fn from_config_redis_without_url_returns_error() {
        let mut config = ServerConfig::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8080),
            "http://localhost:8080".to_owned(),
            PathBuf::from("/tmp/test"),
            NonZeroUsize::new(4096).unwrap(),
        );
        // Set the adapter to Redis without setting a URL.
        config.cache.adapter = ReconstructionCacheAdapter::Redis;
        config.cache.redis_url = None;

        let result = ReconstructionCacheService::from_config(&config);
        assert!(matches!(
            result,
            Err(ServerError::MissingReconstructionCacheRedisUrl)
        ));
    }

    // ── get_or_load — timeout / cancellation ─────────────────────────────

    #[tokio::test]
    async fn cache_service_get_or_load_times_out_with_slow_loader() {
        tokio::time::pause();

        let adapter: SharedReconstructionCache = Arc::new(StaticCache {
            payload: None,
            put_calls: Arc::new(AtomicUsize::new(0)),
        });
        let cache = ReconstructionCacheService::for_tests("static", adapter);
        let key = ReconstructionCacheKey::latest("slow-asset.bin", None);

        // A loader that never completes — keep the sender alive so the
        // receiver hangs forever.
        let (tx, rx) = tokio::sync::oneshot::channel::<()>();
        let _sender_kept_alive = tx;
        let loader = || async {
            let _ = rx.await;
            Ok(sample_response("never-loaded"))
        };

        // Wrap get_or_load with a short timeout.
        let result =
            tokio::time::timeout(Duration::from_millis(50), cache.get_or_load(&key, loader)).await;

        // The timeout should fire before the loader completes.
        assert!(result.is_err(), "expected timeout elapsing, got {result:?}");
    }

    #[tokio::test]
    async fn cache_service_concurrent_requests_race_against_timeout() {
        tokio::time::pause();

        let adapter: SharedReconstructionCache = Arc::new(StaticCache {
            payload: None,
            put_calls: Arc::new(AtomicUsize::new(0)),
        });
        let cache = Arc::new(ReconstructionCacheService::for_tests("static", adapter));
        let key = Arc::new(ReconstructionCacheKey::latest("race-asset.bin", None));

        // A helper function for the slow loader (FnOnce so we need a separate
        // instance per call).
        async fn slow_loader() -> Result<FileReconstructionResponse, ServerError> {
            tokio::time::sleep(Duration::from_millis(100)).await;
            Ok(sample_response("concurrent-loaded"))
        }

        let cache1 = Arc::clone(&cache);
        let key1 = Arc::clone(&key);
        let handle1 = tokio::spawn(async move {
            tokio::time::timeout(
                Duration::from_millis(200),
                cache1.get_or_load(&key1, slow_loader),
            )
            .await
        });

        let cache2 = Arc::clone(&cache);
        let key2 = Arc::clone(&key);
        let handle2 = tokio::spawn(async move {
            tokio::time::timeout(
                Duration::from_millis(200),
                cache2.get_or_load(&key2, slow_loader),
            )
            .await
        });

        // Advance time enough for both requests to complete.
        tokio::time::advance(Duration::from_millis(500)).await;

        let result1 = handle1.await.expect("task 1 panicked");
        let result2 = handle2.await.expect("task 2 panicked");

        // Both should succeed within the timeout.
        assert!(result1.is_ok(), "request 1 should complete: {result1:?}");
        assert!(result2.is_ok(), "request 2 should complete: {result2:?}");

        let response1 = result1.unwrap();
        let response2 = result2.unwrap();
        assert!(response1.is_ok(), "get_or_load 1 should succeed");
        assert!(response2.is_ok(), "get_or_load 2 should succeed");
    }
}