shardline-cache 1.2.0

Reconstruction-cache contracts and adapters 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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing,
    clippy::let_underscore_must_use,
    clippy::match_wild_err_arm
)]

use std::{
    num::{NonZeroU64, NonZeroUsize},
    sync::atomic::{AtomicU64, Ordering},
    time::Duration,
};

use super::MemoryReconstructionCache;
use crate::{AsyncReconstructionCache, ReconstructionCacheError, ReconstructionCacheKey};

#[tokio::test]
async fn memory_cache_roundtrips_one_payload() {
    let cache = MemoryReconstructionCache::new(NonZeroU64::MIN, NonZeroUsize::MIN);
    let key = ReconstructionCacheKey::latest("asset.bin", None);
    let put = cache.put(&key, b"payload").await;
    assert!(put.is_ok());

    let value = cache.get(&key).await;

    assert!(value.is_ok());
    assert_eq!(value.ok(), Some(Some(b"payload".to_vec())));
}

#[tokio::test]
async fn memory_cache_evicts_oldest_entry_when_capacity_is_full() {
    let max_entries = NonZeroUsize::new(2).unwrap_or(NonZeroUsize::MIN);
    let ttl_seconds = NonZeroU64::new(60).unwrap_or(NonZeroU64::MIN);
    let cache = MemoryReconstructionCache::new(ttl_seconds, max_entries);
    let first = ReconstructionCacheKey::latest("asset-1.bin", None);
    let second = ReconstructionCacheKey::latest("asset-2.bin", None);
    let third = ReconstructionCacheKey::latest("asset-3.bin", None);

    assert!(cache.put(&first, b"first").await.is_ok());
    assert!(cache.put(&second, b"second").await.is_ok());
    assert!(cache.put(&third, b"third").await.is_ok());

    let first_value = cache.get(&first).await;
    let second_value = cache.get(&second).await;
    let third_value = cache.get(&third).await;

    assert!(first_value.is_ok());
    assert!(second_value.is_ok());
    assert!(third_value.is_ok());
    assert_eq!(first_value.ok(), Some(None));
    assert_eq!(second_value.ok(), Some(Some(b"second".to_vec())));
    assert_eq!(third_value.ok(), Some(Some(b"third".to_vec())));
}

#[tokio::test(start_paused = true)]
async fn memory_cache_expires_entries_after_ttl() {
    let ttl_seconds = NonZeroU64::new(1).unwrap_or(NonZeroU64::MIN);
    let cache = MemoryReconstructionCache::new(ttl_seconds, NonZeroUsize::MIN);
    let key = ReconstructionCacheKey::latest("asset.bin", None);
    assert!(cache.put(&key, b"payload").await.is_ok());

    tokio::time::advance(Duration::from_secs(1)).await;
    let value = cache.get(&key).await;

    assert!(value.is_ok());
    assert_eq!(value.ok(), Some(None));
}

// ── TTL expiry (wall-clock) ───────────────────────────────────────────

#[allow(clippy::shadow_unrelated)]
#[tokio::test]
async fn memory_cache_expires_entries_after_ttl_wall_clock() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(1).unwrap(),
        NonZeroUsize::new(100).unwrap(),
    );
    let key = ReconstructionCacheKey::latest("test-file", None);
    cache.put(&key, b"hello").await.unwrap();

    // Immediately readable
    let result = cache.get(&key).await.unwrap();
    assert_eq!(result, Some(b"hello".to_vec()));

    // After expiry
    tokio::time::sleep(Duration::from_secs(2)).await;
    let result = cache.get(&key).await.unwrap();
    assert_eq!(result, None, "expired entry should return None");
}

// ── Eviction with max_entries = 1 ─────────────────────────────────────

#[allow(clippy::shadow_unrelated)]
#[tokio::test]
async fn memory_cache_evicts_oldest_when_at_capacity_one() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap(),
        NonZeroUsize::new(1).unwrap(), // only 1 entry
    );
    let key_a = ReconstructionCacheKey::latest("file-a", None);
    let key_b = ReconstructionCacheKey::latest("file-b", None);

    cache.put(&key_a, b"aaa").await.unwrap();
    cache.put(&key_b, b"bbb").await.unwrap();

    // key_a should be evicted (oldest)
    let result = cache.get(&key_a).await.unwrap();
    assert_eq!(result, None, "oldest entry should be evicted");

    // key_b should still be present
    let result = cache.get(&key_b).await.unwrap();
    assert_eq!(result, Some(b"bbb".to_vec()));
}

// ── Concurrent get_or_load deduplication ──────────────────────────────

#[tokio::test]
async fn memory_cache_concurrent_get_or_load_deduplicates() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap(),
        NonZeroUsize::new(100).unwrap(),
    ));
    let key = ReconstructionCacheKey::latest("dedup-test", None);
    let load_count = std::sync::Arc::new(AtomicU64::new(0));

    // Spawn 10 concurrent get_or_load calls with a slow loader
    let mut handles = Vec::new();
    for _ in 0..10 {
        let cache = std::sync::Arc::clone(&cache);
        let key = key.clone();
        let load_count = std::sync::Arc::clone(&load_count);
        handles.push(tokio::spawn(async move {
            cache
                .get_or_load(&key, || {
                    load_count.fetch_add(1, Ordering::Relaxed);
                    Box::pin(async { Ok::<_, ReconstructionCacheError>(b"result".to_vec()) })
                })
                .await
        }));
    }

    for handle in handles {
        let result = handle.await.unwrap();
        assert!(result.is_ok(), "get_or_load should succeed");
        assert_eq!(result.unwrap(), Some(b"result".to_vec()));
    }

    // Loader should only have been called once
    assert_eq!(load_count.load(Ordering::Relaxed), 1);
}

// ── Loader failure cleanup ────────────────────────────────────────────

#[allow(clippy::shadow_unrelated)]
#[tokio::test]
async fn memory_cache_loader_failure_cleans_up_pending() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap(),
        NonZeroUsize::new(100).unwrap(),
    );
    let key = ReconstructionCacheKey::latest("fail-test", None);

    // First call: loader fails with Operation error
    let result = cache
        .get_or_load(&key, || {
            Box::pin(async { Err::<Vec<u8>, _>(ReconstructionCacheError::Operation) })
        })
        .await;
    assert!(result.is_err());

    // Second call should retry (not cache the error)
    let result = cache
        .get_or_load(&key, || {
            Box::pin(async { Ok::<_, ReconstructionCacheError>(b"success".to_vec()) })
        })
        .await;
    assert_eq!(result.unwrap(), Some(b"success".to_vec()));
}

// ── Delete ────────────────────────────────────────────────────────────

#[tokio::test]
async fn memory_cache_delete_removes_entry() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap(),
        NonZeroUsize::new(100).unwrap(),
    );
    let key = ReconstructionCacheKey::latest("del-test", None);

    cache.put(&key, b"data").await.unwrap();
    assert!(cache.get(&key).await.unwrap().is_some());

    let deleted = cache.delete(&key).await.unwrap();
    assert!(deleted);
    assert!(cache.get(&key).await.unwrap().is_none());
}

#[tokio::test]
async fn memory_cache_delete_missing_returns_false() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap(),
        NonZeroUsize::new(100).unwrap(),
    );
    let key = ReconstructionCacheKey::latest("missing", None);
    let deleted = cache.delete(&key).await.unwrap();
    assert!(!deleted);
}

// ── ready() test ─────────────────────────────────────────────────────

#[tokio::test]
async fn memory_cache_ready_returns_ok() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    );
    let result = cache.ready().await;
    assert!(result.is_ok());
}

// ── get returns None for missing key ──────────────────────────────────

#[tokio::test]
async fn memory_cache_get_returns_none_for_missing_key() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    );
    let key = ReconstructionCacheKey::latest("nonexistent", None);
    let result = cache.get(&key).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), None);
}

// ── get returns None for expired entry (not in loading state) ─────────

#[tokio::test(start_paused = true)]
async fn memory_cache_get_returns_none_for_expired_not_loading() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(1).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    );
    let key = ReconstructionCacheKey::latest("expired-test", None);
    cache.put(&key, b"data").await.unwrap();

    // Advance past TTL
    tokio::time::advance(Duration::from_secs(2)).await;

    let result = cache.get(&key).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), None, "expired entry should return None");
}

// ── delete on expired entry ───────────────────────────────────────────

#[tokio::test(start_paused = true)]
async fn memory_cache_delete_expired_entry_returns_true() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(1).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    );
    let key = ReconstructionCacheKey::latest("expired-del", None);
    cache.put(&key, b"data").await.unwrap();

    // Advance past TTL
    tokio::time::advance(Duration::from_secs(2)).await;

    // Entry is expired but still stored — delete should succeed
    let deleted = cache.delete(&key).await.unwrap();
    assert!(deleted, "delete should return true even for expired entry");

    // After delete, get should return None
    let result = cache.get(&key).await.unwrap();
    assert_eq!(result, None);
}

// ── get_or_load with concurrent timeout ──────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn memory_cache_get_times_out_when_loader_hangs() {
    use std::sync::Arc;

    let cache = Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("hung-loader", None);

    // Task1: start get_or_load with a slow loader (60s)
    let cache_1 = Arc::clone(&cache);
    let key_1 = key.clone();
    let task1 = tokio::spawn(async move {
        let _result = cache_1
            .get_or_load(&key_1, || {
                Box::pin(async {
                    // Slow loader — takes 60 seconds
                    tokio::time::sleep(Duration::from_secs(60)).await;
                    Ok::<_, ReconstructionCacheError>(b"new-data".to_vec())
                })
            })
            .await;
    });

    // Give task1 time to enter the loading state
    tokio::time::sleep(Duration::from_millis(200)).await;

    // Task2: get() should hit the internal 30-second timeout
    let result = tokio::time::timeout(Duration::from_secs(35), cache.get(&key)).await;

    #[allow(clippy::panic, clippy::match_wild_err_arm)]
    match result {
        Ok(Ok(value)) => {
            // Internal timeout fired — get returned None
            assert_eq!(value, None, "get should return None after internal timeout");
        }
        Ok(Err(e)) => panic!("get returned unexpected error: {e}"),
        Err(_elapsed) => {
            panic!(
                "get did not complete within 35 seconds (internal 30s timeout should have fired)"
            );
        }
    }

    drop(task1);
}

// ── get_or_load: cache hit (fast path) ────────────────────────────────

#[allow(clippy::shadow_unrelated)]
#[tokio::test]
async fn get_or_load_cache_hit_fast_path() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    );
    let key = ReconstructionCacheKey::latest("hit-test", None);

    // First call: loader runs and stores the value
    let result = cache
        .get_or_load(&key, || {
            Box::pin(async { Ok::<_, ReconstructionCacheError>(b"cached".to_vec()) })
        })
        .await;
    assert_eq!(result.unwrap(), Some(b"cached".to_vec()));

    // Second call: fast path cache hit — loader MUST NOT be called
    let load_count = std::sync::Arc::new(AtomicU64::new(0));
    let load_count_2 = std::sync::Arc::clone(&load_count);
    let result = cache
        .get_or_load(&key, || {
            load_count_2.fetch_add(1, Ordering::Relaxed);
            Box::pin(async { Ok::<_, ReconstructionCacheError>(b"should-not-call".to_vec()) })
        })
        .await;
    assert_eq!(result.unwrap(), Some(b"cached".to_vec()));
    assert_eq!(load_count.load(Ordering::Relaxed), 0);
}

// ── get_or_load: concurrent loading dedup hits loading map ────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_or_load_loading_dedup_hits_loading_map() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("dedup-map", None);
    let load_count = std::sync::Arc::new(AtomicU64::new(0));

    // Use a barrier so all 10 tasks hit get_or_load nearly simultaneously
    let barrier = std::sync::Arc::new(tokio::sync::Barrier::new(11));

    let mut handles = Vec::new();
    for _ in 0..10 {
        let cache = std::sync::Arc::clone(&cache);
        let key = key.clone();
        let load_count = std::sync::Arc::clone(&load_count);
        let barrier = std::sync::Arc::clone(&barrier);
        handles.push(tokio::spawn(async move {
            barrier.wait().await;
            cache
                .get_or_load(&key, || {
                    load_count.fetch_add(1, Ordering::Relaxed);
                    Box::pin(async { Ok::<_, ReconstructionCacheError>(b"result".to_vec()) })
                })
                .await
        }));
    }

    // Release all tasks at once from the main thread
    barrier.wait().await;

    for handle in handles {
        let result = handle.await.unwrap();
        assert!(result.is_ok(), "get_or_load should succeed");
        assert_eq!(result.unwrap(), Some(b"result".to_vec()));
    }

    assert_eq!(load_count.load(Ordering::Relaxed), 1);
}

// ── get_or_load: concurrent waiter gets None when loader fails ────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_or_load_concurrent_loader_failure_waiter_gets_none() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("fail-waiter", None);

    // Task 1: slow loader (300ms) that will fail
    let cache_1 = std::sync::Arc::clone(&cache);
    let key_1 = key.clone();
    let task1 = tokio::spawn(async move {
        cache_1
            .get_or_load(&key_1, || {
                Box::pin(async {
                    tokio::time::sleep(Duration::from_millis(300)).await;
                    Err::<Vec<u8>, ReconstructionCacheError>(ReconstructionCacheError::Operation)
                })
            })
            .await
    });

    // Give task1 time to register as the exclusive loader
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Task 2: should find task1 loading and wait on notify
    let cache_2 = std::sync::Arc::clone(&cache);
    let key_2 = key.clone();
    let task2 = tokio::spawn(async move {
        cache_2
            .get_or_load(&key_2, || {
                Box::pin(async {
                    panic!("waiter should not call loader");
                    #[allow(unreachable_code)]
                    Ok::<_, ReconstructionCacheError>(vec![])
                })
            })
            .await
    });

    let result1 = task1.await.unwrap();
    assert!(result1.is_err(), "loader should have failed");

    let result2 = task2.await.unwrap();
    // Waiter should get None (not an error), because the cleanup
    // notifies waiters without propagating the error.
    assert_eq!(result2.unwrap(), None);
}

// ── get_or_load: expired entry removed before loading ─────────────────

#[tokio::test(start_paused = true)]
async fn get_or_load_removes_expired_entry_before_loading() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(1).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    );
    let key = ReconstructionCacheKey::latest("expired-before-load", None);

    // Put an entry
    cache.put(&key, b"expired").await.unwrap();

    // Advance past TTL
    tokio::time::advance(Duration::from_secs(2)).await;

    // get_or_load should: fast path miss, then in the loading setup,
    // find the expired entry and remove it (lines 164-167)
    let result = cache
        .get_or_load(&key, || {
            Box::pin(async { Ok::<_, ReconstructionCacheError>(b"fresh".to_vec()) })
        })
        .await;
    assert_eq!(result.unwrap(), Some(b"fresh".to_vec()));

    // Verify the old expired entry was cleaned up
    let get_result = cache.get(&key).await.unwrap();
    assert_eq!(get_result, Some(b"fresh".to_vec()));
}

// ── get: loading coalescing — successful notify path ───────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_loading_coalescing_success() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("coalesce-success", None);

    // Spawn a get_or_load with a slow loader so get() can find the loading entry
    let cache_put = std::sync::Arc::clone(&cache);
    let key_put = key.clone();
    let task_loader = tokio::spawn(async move {
        cache_put
            .get_or_load(&key_put, || {
                Box::pin(async {
                    tokio::time::sleep(Duration::from_millis(500)).await;
                    Ok::<_, ReconstructionCacheError>(b"loaded-data".to_vec())
                })
            })
            .await
    });

    // Give get_or_load time to enter loading state
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Now call get() — should find the loading entry, wait, and get the result
    let result = tokio::time::timeout(Duration::from_secs(5), cache.get(&key)).await;

    match result {
        Ok(Ok(Some(data))) => {
            assert_eq!(data, b"loaded-data".to_vec());
        }
        Ok(Ok(None)) => {
            panic!("get() should have returned the loaded data");
        }
        Ok(Err(e)) => {
            panic!("get() returned unexpected error: {e}");
        }
        Err(_) => {
            panic!("get() timed out — loading coalescing failed");
        }
    }

    let _ = task_loader.await;
}

// ── get: loading coalescing — loader failure returns None ──────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_loading_coalescing_loader_failure_returns_none() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("coalesce-fail", None);

    // Spawn a get_or_load that will fail
    let cache_loader = std::sync::Arc::clone(&cache);
    let key_loader = key.clone();
    let task_loader = tokio::spawn(async move {
        cache_loader
            .get_or_load(&key_loader, || {
                Box::pin(async {
                    tokio::time::sleep(Duration::from_millis(300)).await;
                    Err::<Vec<u8>, ReconstructionCacheError>(ReconstructionCacheError::Operation)
                })
            })
            .await
    });

    // Give get_or_load time to enter loading state
    tokio::time::sleep(Duration::from_millis(50)).await;

    // get() should find the loading entry, wait on notify, then find nothing
    // and return None (not propagate the error)
    let result = tokio::time::timeout(Duration::from_secs(5), cache.get(&key)).await;

    match result {
        Ok(Ok(None)) => {
            // Expected: loader failed, no entry stored
        }
        Ok(Ok(Some(_))) => {
            panic!("get() should return None after loader failure");
        }
        Ok(Err(e)) => {
            panic!("get() returned unexpected error: {e}");
        }
        Err(_) => {
            panic!("get() timed out — loading coalescing failed");
        }
    }

    let _ = task_loader.await;
}

// ── put: update existing entry does not evict ──────────────────────────

#[tokio::test]
async fn put_update_existing_no_eviction() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(1).unwrap_or(NonZeroUsize::MIN), // capacity = 1
    );
    let key = ReconstructionCacheKey::latest("update-key", None);

    // Insert first entry
    cache.put(&key, b"first").await.unwrap();
    // Update same key (should NOT evict since key already exists)
    cache.put(&key, b"second").await.unwrap();

    // Only one entry exists — the updated one
    let result = cache.get(&key).await.unwrap();
    assert_eq!(result, Some(b"second".to_vec()));
}

// ── PartialOrd implementation for EvictionKey ────────────────────────

#[test]
fn eviction_key_partial_cmp() {
    use super::inner::EvictionKey;
    use tokio::time::Instant;

    let a = EvictionKey(Instant::now(), 1);
    let b = EvictionKey(Instant::now(), 2);
    // partial_cmp delegates to cmp
    let ordering = a.partial_cmp(&b);
    assert!(ordering.is_some());
}

// ── get: race to hit no-loading / re-check paths ─────────────────────

// ── Race: get() write-lock contention (triggers re-check + no-loading) ─

#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn get_race_with_write_lock_contention() {
    // Use a longer TTL so pre-seeded entries don't expire before we race.
    // The goal is to have get()'s read-lock see loading=true on an expired
    // entry, then lose the write-lock race to a concurrent operation,
    // landing on lines 260-270.
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(10_000).unwrap_or(NonZeroUsize::MIN),
    ));

    let mut handles = Vec::new();
    for i in 0..500 {
        let key = ReconstructionCacheKey::latest(&format!("contention-{i}"), None);

        // Write-lock contender (put)
        let c1 = std::sync::Arc::clone(&cache);
        let k1 = key.clone();
        let h1 = tokio::spawn(async move {
            let _ = c1.put(&k1, b"preloaded").await;
        });

        // Write-lock contender (delete)
        let c2 = std::sync::Arc::clone(&cache);
        let k2 = key.clone();
        let h2 = tokio::spawn(async move {
            let _ = c2.delete(&k2).await;
        });

        // Failing loader (get_or_load)
        let c3 = std::sync::Arc::clone(&cache);
        let k3 = key.clone();
        let h3 = tokio::spawn(async move {
            let _ = c3
                .get_or_load(&k3, || {
                    Box::pin(async {
                        tokio::time::sleep(Duration::from_micros(100)).await;
                        Err::<Vec<u8>, ReconstructionCacheError>(
                            ReconstructionCacheError::Operation,
                        )
                    })
                })
                .await;
        });

        // Getter
        let c4 = std::sync::Arc::clone(&cache);
        let k4 = key.clone();
        let h4 = tokio::spawn(async move {
            let _ = c4.get(&k4).await;
        });

        // Extra write-lock contender
        let c5 = std::sync::Arc::clone(&cache);
        let k5 = key.clone();
        let h5 = tokio::spawn(async move {
            let _ = c5.put(&k5, b"extra").await;
        });

        handles.push(h1);
        handles.push(h2);
        handles.push(h3);
        handles.push(h4);
        handles.push(h5);
    }

    for h in handles {
        let _ = h.await;
    }
}

// ── Concurrency stress tests ──────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn memory_cache_concurrent_get_put_delete() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(60).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(1000).unwrap_or(NonZeroUsize::MIN),
    ));
    let keys: std::sync::Arc<[ReconstructionCacheKey; 4]> = std::sync::Arc::new([
        ReconstructionCacheKey::latest("concurrent-1", None),
        ReconstructionCacheKey::latest("concurrent-2", None),
        ReconstructionCacheKey::latest("concurrent-3", None),
        ReconstructionCacheKey::latest("concurrent-4", None),
    ]);

    let mut handles = Vec::new();

    // 10 concurrent tasks: mix of get, put, delete
    for task_id in 0..10 {
        let cache = std::sync::Arc::clone(&cache);
        let keys = std::sync::Arc::clone(&keys);
        handles.push(tokio::spawn(async move {
            for round in 0..50 {
                let key = &keys[(task_id + round) % 4];
                match (task_id + round) % 5 {
                    0 | 1 => {
                        // put
                        let payload = format!("payload-{task_id}-{round}");
                        let _ = cache.put(key, payload.as_bytes()).await;
                    }
                    2 | 3 => {
                        // get
                        let _ = cache.get(key).await;
                    }
                    _ => {
                        // delete
                        let _ = cache.delete(key).await;
                    }
                }
                // Yield between operations to increase interleaving
                tokio::task::yield_now().await;
            }
        }));
    }

    for handle in handles {
        let result = handle.await;
        assert!(result.is_ok(), "task panicked: {:?}", result.err());
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn memory_cache_concurrent_same_key() {
    // Stress the loading/Notify coalescing pattern when multiple tasks
    // race on the same key simultaneously.
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(60).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("hot-key", None);

    let mut handles = Vec::new();
    for _ in 0..20 {
        let cache = std::sync::Arc::clone(&cache);
        let key = key.clone();
        handles.push(tokio::spawn(async move {
            // First put to ensure there's data
            let _ = cache.put(&key, b"shared-data").await;
            // Multiple concurrent gets — some will hit loading coalescing
            for _ in 0..20 {
                let result = cache.get(&key).await;
                assert!(result.is_ok(), "concurrent get should not error");
                tokio::task::yield_now().await;
            }
        }));
    }

    for handle in handles {
        let result = handle.await;
        assert!(result.is_ok(), "task panicked: {:?}", result.err());
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn memory_cache_concurrent_eviction_stress() {
    // Stress the eviction path by filling the cache beyond capacity
    // with concurrent puts.
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(60).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(10).unwrap_or(NonZeroUsize::MIN),
    ));

    let mut handles = Vec::new();
    for task_id in 0..20 {
        let cache = std::sync::Arc::clone(&cache);
        handles.push(tokio::spawn(async move {
            for i in 0..20 {
                let key = ReconstructionCacheKey::latest(&format!("evict-key-{task_id}-{i}"), None);
                let payload = format!("evict-payload-{task_id}-{i}");
                let _ = cache.put(&key, payload.as_bytes()).await;
                // Also read back randomly
                if i % 3 == 0 {
                    let _ = cache.get(&key).await;
                }
                tokio::task::yield_now().await;
            }
        }));
    }

    for handle in handles {
        let result = handle.await;
        assert!(result.is_ok(), "task panicked: {:?}", result.err());
    }
}

// ── get(): write-lock re-check finds valid entry ───────────────────────
//
// This covers the code at lines ~236-239 where get() acquires the write
// lock and finds a valid (non-expired) entry that was stored by a
// concurrent put() between the read-lock and write-lock acquisition.

#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn get_write_lock_recheck_finds_valid_entry() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("recheck-write-lock", None);

    // Use 500 rounds of concurrent get/get_or_load so the race window
    // where a loader finishes between get()'s read-lock and write-lock
    // is hit at least once.
    let mut handles = Vec::new();
    for _ in 0..500 {
        // Task: get_or_load with a fast loader
        let c1 = std::sync::Arc::clone(&cache);
        let k1 = key.clone();
        handles.push(tokio::spawn(async move {
            let result = c1
                .get_or_load(&k1, || {
                    Box::pin(async {
                        tokio::time::sleep(Duration::from_micros(10)).await;
                        Ok::<_, ReconstructionCacheError>(b"value".to_vec())
                    })
                })
                .await;
            let _ = result;
        }));

        // Task: get() — if it lands between the loading entry creation
        // and the put() that stores the loaded value, it will exercise
        // the write-lock re-check path (lines 236-239).
        let c2 = std::sync::Arc::clone(&cache);
        let k2 = key.clone();
        handles.push(tokio::spawn(async move {
            let result = c2.get(&k2).await;
            assert!(result.is_ok(), "get() should not error");
        }));

        // Additional puts to create more write-lock pressure
        let c3 = std::sync::Arc::clone(&cache);
        let k3 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c3.put(&k3, b"extra").await;
        }));
    }

    for h in handles {
        let _ = h.await;
    }
}

// ── get(): creates a new loading entry after a previous one vanished ──
//
// This covers lines ~267-277 where get() finds NO loading entry in the
// write lock (it was removed by a concurrent failing loader between
// the read and write lock), so it creates a fresh loading entry.

#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn get_creates_loading_entry_when_loader_disappears() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    // Use a unique key per round so stale loading entries from one
    // round never cascade into another round's 30-second timeout.
    let mut handles = Vec::new();
    for i in 0..1000 {
        let key = ReconstructionCacheKey::latest(&format!("disappearing-loader-{i}"), None);

        // Failing get_or_load — fast 10 µs sleep
        let c1 = std::sync::Arc::clone(&cache);
        let k1 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c1
                .get_or_load(&k1, || {
                    Box::pin(async {
                        tokio::time::sleep(Duration::from_micros(10)).await;
                        Err::<Vec<u8>, ReconstructionCacheError>(
                            ReconstructionCacheError::Operation,
                        )
                    })
                })
                .await;
        }));

        // get() — may race to see loading entry in read lock but not
        // in write lock, exercising lines 261-271.
        let c2 = std::sync::Arc::clone(&cache);
        let k2 = key.clone();
        handles.push(tokio::spawn(async move {
            let result = c2.get(&k2).await;
            assert!(result.is_ok(), "get() should not error");
        }));
    }

    for h in handles {
        let _ = h.await;
    }
}

// ── put() notifies waiters when a loading entry exists ─────────────────
//
// When put() is called externally while a get_or_load is loading the
// same key, put() should find the loading entry and notify waiters.
// This covers put() lines ~302-303.

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn put_notifies_waiting_loading_entry() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("put-notify", None);

    // Start a get_or_load with a slow loader that creates a loading entry
    let c1 = std::sync::Arc::clone(&cache);
    let k1 = key.clone();
    let loader_task = tokio::spawn(async move {
        c1.get_or_load(&k1, || {
            Box::pin(async {
                tokio::time::sleep(Duration::from_millis(300)).await;
                Ok::<_, ReconstructionCacheError>(b"from-loader".to_vec())
            })
        })
        .await
    });

    // Wait for the loading entry to be created
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Call put() externally — it should find the loading entry and
    // remove + notify.  This does NOT break the in-flight loader;
    // when the loader finishes it calls put() again.
    cache
        .put(&key, b"external-put")
        .await
        .expect("external put should succeed");

    // The get_or_load loader eventually finishes and its put()
    // overwrites with the loader's value.
    let loader_result = loader_task.await.expect("loader task panicked");
    assert!(
        loader_result.is_ok(),
        "loader should still succeed even with external put"
    );

    // Either "external-put" or "from-loader" is the final value —
    // both are valid depending on ordering.
    let final_val = cache.get(&key).await.unwrap();
    assert!(
        final_val.is_some(),
        "cache should have some value after put notify"
    );
}

// ── CacheInner seq ordering ───────────────────────────────────────────
//
// Verifies that entries are evicted in FIFO order (oldest first) by
// using monotonic Insertion-time progression (wall-clock with small
// sleeps between inserts).

#[tokio::test]
async fn cache_inner_fifo_eviction_order() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(3).unwrap_or(NonZeroUsize::MIN), // capacity = 3
    );
    let keys = [
        ReconstructionCacheKey::latest("first", None),
        ReconstructionCacheKey::latest("second", None),
        ReconstructionCacheKey::latest("third", None),
        ReconstructionCacheKey::latest("fourth", None),
    ];

    // Insert 3 entries with distinct insertion times
    for k in &keys[..3] {
        cache.put(k, b"data").await.unwrap();
        tokio::time::sleep(Duration::from_millis(5)).await;
    }

    // Fourth insert triggers eviction of the oldest (first)
    cache.put(&keys[3], b"fourth").await.unwrap();

    // First should be evicted (oldest)
    assert_eq!(
        cache.get(&keys[0]).await.unwrap(),
        None,
        "first (oldest) should be evicted"
    );
    // Second and third should still be present
    assert_eq!(
        cache.get(&keys[1]).await.unwrap(),
        Some(b"data".to_vec()),
        "second should survive"
    );
    assert_eq!(
        cache.get(&keys[2]).await.unwrap(),
        Some(b"data".to_vec()),
        "third should survive"
    );
    assert_eq!(
        cache.get(&keys[3]).await.unwrap(),
        Some(b"fourth".to_vec()),
        "fourth should be present"
    );
}

// ── CacheInner evict_oldest skips stale eviction entries ──────────────
//
// If eviction_order has a stale key (entry was already removed from
// `entries` without cleaning up eviction_order), evict_oldest should
// skip it and pop the next one.

#[tokio::test]
async fn cache_inner_evict_oldest_skips_stale_entries() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(2).unwrap_or(NonZeroUsize::MIN), // capacity = 2
    );
    let keys = [
        ReconstructionCacheKey::latest("stale-a", None),
        ReconstructionCacheKey::latest("stale-b", None),
        ReconstructionCacheKey::latest("stale-c", None),
    ];

    // Fill cache to capacity
    cache.put(&keys[0], b"a").await.unwrap();
    cache.put(&keys[1], b"b").await.unwrap();

    // Delete keys[0] normally (cleans up eviction_order)
    cache.delete(&keys[0]).await.unwrap();

    // Now cache has capacity for 1 more, insert keys[2] (no eviction needed)
    cache.put(&keys[2], b"c").await.unwrap();

    // keys[0] is gone, keys[1] and keys[2] are present
    assert_eq!(cache.get(&keys[0]).await.unwrap(), None);
    assert_eq!(cache.get(&keys[1]).await.unwrap(), Some(b"b".to_vec()));
    assert_eq!(cache.get(&keys[2]).await.unwrap(), Some(b"c".to_vec()));
}

// ── get_or_load write-lock re-check hit ───────────────────────────────
//
// When get_or_load re-checks after acquiring the write lock, it may
// find a valid entry that was stored by a concurrent put() between
// the read-lock and write-lock acquisition (lines ~150-153).

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn get_or_load_recheck_after_write_lock_finds_entry() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("or-write-recheck", None);

    // Race many get_or_load + put operations to trigger the write-lock
    // re-check path where a put stores data between read/write lock.
    let mut handles = Vec::new();
    for i in 0..50 {
        let c1 = std::sync::Arc::clone(&cache);
        let k1 = key.clone();
        handles.push(tokio::spawn(async move {
            let result = c1
                .get_or_load(&k1, || {
                    Box::pin(async {
                        tokio::time::sleep(Duration::from_micros(20)).await;
                        Ok::<_, ReconstructionCacheError>(format!("loaded-{i}").into_bytes())
                    })
                })
                .await;
            let _ = result;
        }));

        let c2 = std::sync::Arc::clone(&cache);
        let k2 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c2.put(&k2, format!("put-{i}").as_bytes()).await;
        }));

        let c3 = std::sync::Arc::clone(&cache);
        let k3 = key.clone();
        handles.push(tokio::spawn(async move {
            let result = c3.get(&k3).await;
            assert!(result.is_ok());
        }));
    }

    for h in handles {
        let _ = h.await;
    }

    // Final state should have some value
    let final_val = cache.get(&key).await.unwrap();
    assert!(final_val.is_some(), "cache should hold a final value");
}

// ── put into full cache triggers eviction (non-existing key) ──────────
//
// The eviction guard in put() is: if the key does NOT already exist AND
// the cache is at capacity, evict the oldest.  This test verifies the
// condition correctly distinguishes existing-key updates (no eviction)
// from new-key inserts (eviction).

#[allow(clippy::shadow_unrelated)]
#[tokio::test]
async fn put_new_key_evicts_when_full_existing_key_update_does_not() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(2).unwrap_or(NonZeroUsize::MIN), // capacity = 2
    );
    let first = ReconstructionCacheKey::latest("first", None);
    let second = ReconstructionCacheKey::latest("second", None);
    let overflow = ReconstructionCacheKey::latest("overflow", None);

    // Fill cache: first inserted first (oldest), second inserted second
    cache.put(&first, b"first").await.unwrap();
    cache.put(&second, b"second").await.unwrap();

    // Update first key — should NOT trigger eviction because
    // contains_key(first) is true; the guard only evicts for new keys.
    // The update gives first a newer timestamp, making it the newest.
    cache.put(&first, b"first-updated").await.unwrap();
    assert_eq!(
        cache.get(&first).await.unwrap(),
        Some(b"first-updated".to_vec()),
        "existing key update should not evict"
    );
    assert_eq!(
        cache.get(&second).await.unwrap(),
        Some(b"second".to_vec()),
        "second should still be present after first is updated"
    );

    // Insert overflow key — SHOULD evict the oldest entry.
    // Since "first" was just updated (newest), "second" is now oldest.
    cache.put(&overflow, b"third").await.unwrap();
    assert_eq!(
        cache.get(&second).await.unwrap(),
        None,
        "second (now oldest) should be evicted for new key"
    );
    assert_eq!(
        cache.get(&first).await.unwrap(),
        Some(b"first-updated".to_vec()),
        "first (updated, newest) should survive"
    );
    assert_eq!(
        cache.get(&overflow).await.unwrap(),
        Some(b"third".to_vec()),
        "overflow should be present"
    );
}

// ── get_or_load concurrent waiter notified via put ────────────────────
//
// Multiple concurrent get_or_load calls: one becomes the loader
// (which calls put() on success), the others wait on the notify.
// This exercises the "waiter gets notified and returns the stored
// value" path in get_or_load (lines ~198-207).

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn get_or_load_waiter_gets_notified_value() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("waiter-notified", None);
    let load_count = std::sync::Arc::new(AtomicU64::new(0));

    // Use a barrier so all tasks arrive at nearly the same time
    let barrier = std::sync::Arc::new(tokio::sync::Barrier::new(21));

    let mut handles = Vec::new();
    for _ in 0..20 {
        let c = std::sync::Arc::clone(&cache);
        let k = key.clone();
        let b = std::sync::Arc::clone(&barrier);
        let lc = std::sync::Arc::clone(&load_count);
        handles.push(tokio::spawn(async move {
            b.wait().await;
            let result = c
                .get_or_load(&k, || {
                    lc.fetch_add(1, Ordering::Relaxed);
                    Box::pin(async {
                        tokio::time::sleep(Duration::from_millis(100)).await;
                        Ok::<_, ReconstructionCacheError>(b"shared-result".to_vec())
                    })
                })
                .await;
            assert!(result.is_ok(), "get_or_load should succeed");
            assert_eq!(
                result.unwrap(),
                Some(b"shared-result".to_vec()),
                "all waiters should get the stored value"
            );
        }));
    }

    // Release all tasks at once
    barrier.wait().await;

    for h in handles {
        h.await.unwrap();
    }

    assert_eq!(
        load_count.load(Ordering::Relaxed),
        1,
        "loader should only run once"
    );
}

// ── get_or_load expired entry cleanup in write-lock re-check ──────────
//
// When the re-check after write lock finds an entry that was stored
// between read and write lock but that entry is already expired,
// get_or_load removes it and proceeds to load the new value.
// This covers the expired-entry cleanup path in the re-check block.

#[tokio::test(start_paused = true)]
async fn get_or_load_recheck_finds_expired_entry_after_concurrent_put() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(1).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    ));
    let key = ReconstructionCacheKey::latest("recheck-expired", None);

    // Put a short-lived entry
    cache.put(&key, b"short-lived").await.unwrap();

    // Advance time past TTL so the entry is expired
    tokio::time::advance(Duration::from_secs(2)).await;

    // The entry exists but is expired. get_or_load's fast-path read lock
    // will miss (expired), then the write lock re-check also finds it
    // expired, proceeds to the loading setup, finds no loading entry,
    // removes the expired entry, and loads fresh data.
    let result = cache
        .get_or_load(&key, || {
            Box::pin(async { Ok::<_, ReconstructionCacheError>(b"fresh".to_vec()) })
        })
        .await;
    assert_eq!(
        result.unwrap(),
        Some(b"fresh".to_vec()),
        "should load fresh data after finding expired entry"
    );

    // Verify the old expired entry was cleaned up
    let stored = cache.get(&key).await.unwrap();
    assert_eq!(
        stored,
        Some(b"fresh".to_vec()),
        "expired entry should have been replaced"
    );
}

// ── Concurrent put/get loading entry cleanup ─────────────────────────
//
// Stress test: many concurrent puts and gets to exercise the loading
// entry creation and cleanup paths under high contention.

#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn concurrent_put_and_get_loading_entry_stress() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(50).unwrap_or(NonZeroUsize::MIN),
    ));

    let mut handles = Vec::new();
    for i in 0..30 {
        let key = ReconstructionCacheKey::latest(&format!("stress-key-{i}"), None);

        // Concurrent put + get on the same key
        for _ in 0..3 {
            let c_put = std::sync::Arc::clone(&cache);
            let k_put = key.clone();
            handles.push(tokio::spawn(async move {
                let _ = c_put.put(&k_put, b"data").await;
            }));

            let c_get = std::sync::Arc::clone(&cache);
            let k_get = key.clone();
            handles.push(tokio::spawn(async move {
                let result = c_get.get(&k_get).await;
                assert!(result.is_ok(), "get should not error under stress");
            }));

            let c_del = std::sync::Arc::clone(&cache);
            let k_del = key.clone();
            handles.push(tokio::spawn(async move {
                let _ = c_del.delete(&k_del).await;
            }));
        }
    }

    for h in handles {
        let _ = h.await;
    }
}

// ── get: phantom loading entry + write-lock re-check with contention ─
//
// Spawns many concurrent operations (get_or_load, get, put, delete)
// on the same cache to create write-lock contention.  This improves
// the chance that get()'s write-lock acquisition yields, allowing:
//
//   a) A loading entry to disappear between read lock and write lock,
//      triggering the phantom loading entry path (lines 261-271).
//
//   b) A put() to store an entry between read lock and write lock,
//      triggering the write-lock re-check path (lines 230-233).

#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn get_phantom_loading_recheck_with_contention() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(10_000).unwrap_or(NonZeroUsize::MIN),
    ));

    let mut handles = Vec::new();

    for i in 0..3000 {
        let key = ReconstructionCacheKey::latest(&format!("contention-{i}"), None);

        // Fast-failing get_or_load — creates a loading entry that
        // vanishes quickly.
        let c1 = std::sync::Arc::clone(&cache);
        let k1 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c1
                .get_or_load(&k1, || {
                    Box::pin(async {
                        tokio::time::sleep(Duration::from_micros(1)).await;
                        Err::<Vec<u8>, ReconstructionCacheError>(
                            ReconstructionCacheError::Operation,
                        )
                    })
                })
                .await;
        }));

        // get() calls that race against the loader.
        for _ in 0..3 {
            let c = std::sync::Arc::clone(&cache);
            let k = key.clone();
            handles.push(tokio::spawn(async move {
                let result = c.get(&k).await;
                assert!(result.is_ok(), "get() should not error");
            }));
        }

        // put() calls to create write-lock contention
        let c_put2 = std::sync::Arc::clone(&cache);
        let k_put2 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c_put2.put(&k_put2, b"data").await;
        }));

        // delete() calls for more write-lock contention
        let c_del2 = std::sync::Arc::clone(&cache);
        let k_del2 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c_del2.delete(&k_del2).await;
        }));
    }

    for h in handles {
        let _ = h.await;
    }
}

// ── get: phantom loading with expired entry removal ──────────────────
//
// When the phantom loading path is hit AND an expired entry exists,
// the `should_remove` check (line 267) is true and the expired entry
// is cleaned up (lines 268-269).  Uses high contention to hit the
// race.

#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn get_phantom_loading_removes_expired_entry() {
    let cache = std::sync::Arc::new(MemoryReconstructionCache::new(
        NonZeroU64::new(1).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(10_000).unwrap_or(NonZeroUsize::MIN),
    ));

    let mut handles = Vec::new();

    for i in 0..2000 {
        // Each key gets a short-TTL entry via the main cache (1s TTL).
        // By the time operations race, the entry may be expired,
        // exercising the should_remove check in the phantom path.
        let key = ReconstructionCacheKey::latest(&format!("phantom-exp-{i}"), None);

        // Seed: put with 1s TTL
        let c_seed = std::sync::Arc::clone(&cache);
        let k_seed = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c_seed.put(&k_seed, b"seed").await;
        }));

        // Fast-failing loader
        let c1 = std::sync::Arc::clone(&cache);
        let k1 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c1
                .get_or_load(&k1, || {
                    Box::pin(async {
                        tokio::time::sleep(Duration::from_micros(1)).await;
                        Err::<Vec<u8>, ReconstructionCacheError>(
                            ReconstructionCacheError::Operation,
                        )
                    })
                })
                .await;
        }));

        // get() calls
        for _ in 0..3 {
            let c_get2 = std::sync::Arc::clone(&cache);
            let k_get2 = key.clone();
            handles.push(tokio::spawn(async move {
                let result = c_get2.get(&k_get2).await;
                assert!(result.is_ok(), "get() should not error");
            }));
        }

        // Additional write-lock contention via put/delete
        let c_put3 = std::sync::Arc::clone(&cache);
        let k_put3 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c_put3.put(&k_put3, b"noise").await;
        }));
        let c_del3 = std::sync::Arc::clone(&cache);
        let k_del3 = key.clone();
        handles.push(tokio::spawn(async move {
            let _ = c_del3.delete(&k_del3).await;
        }));
    }

    for h in handles {
        let _ = h.await;
    }
}

// ── CacheInner evict_oldest on empty cache ────────────────────────────

#[tokio::test]
async fn cache_inner_evict_oldest_empty_cache_is_noop() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(5).unwrap_or(NonZeroUsize::MIN),
    );
    let key = ReconstructionCacheKey::latest("new-key", None);

    // Eviction should not prevent insertion
    cache.put(&key, b"data").await.unwrap();
    let result = cache.get(&key).await.unwrap();
    assert_eq!(result, Some(b"data".to_vec()));
}

// ── CacheInner evict_oldest stale eviction entries only ────────────────

#[tokio::test]
async fn cache_inner_evict_oldest_all_stale_clears_eviction_order() {
    // Create a cache with capacity 2 and insert 2 entries, then
    // delete both via remove() which cleans eviction_order.
    // Then insert a third key — evict_oldest must handle finding
    // nothing to evict gracefully.
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(2).unwrap_or(NonZeroUsize::MIN),
    );
    let k1 = ReconstructionCacheKey::latest("k1", None);
    let k2 = ReconstructionCacheKey::latest("k2", None);
    let k3 = ReconstructionCacheKey::latest("k3", None);

    cache.put(&k1, b"one").await.unwrap();
    cache.put(&k2, b"two").await.unwrap();

    // Delete both to clean up entries but keep eviction_order entries
    cache.delete(&k1).await.unwrap();
    cache.delete(&k2).await.unwrap();

    // k3 should be insertable without panic
    cache.put(&k3, b"three").await.unwrap();
    assert_eq!(cache.get(&k3).await.unwrap(), Some(b"three".to_vec()));
}

// ── CacheInner insert overwrites existing key correctly ────────────────

#[tokio::test]
async fn cache_inner_insert_overwrites_existing_key() {
    let cache = MemoryReconstructionCache::new(
        NonZeroU64::new(3600).unwrap_or(NonZeroU64::MIN),
        NonZeroUsize::new(100).unwrap_or(NonZeroUsize::MIN),
    );
    let key = ReconstructionCacheKey::latest("overwrite", None);

    // Insert twice — the first entry is replaced in insert()
    cache.put(&key, b"first").await.unwrap();
    cache.put(&key, b"second").await.unwrap();

    let result = cache.get(&key).await.unwrap();
    assert_eq!(result, Some(b"second".to_vec()));
}