ipfrs-network 0.2.0

Peer-to-peer networking layer with libp2p and QUIC for IPFRS
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
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
//! Stream-level priority scheduler for multiplexed network streams.
//!
//! Implements multiple scheduling disciplines:
//! - Strict Priority (SP): highest-priority streams always scheduled first
//! - Weighted Fair Queuing (WFQ): proportional share based on weight
//! - Deficit Round Robin (DRR): byte-accurate fairness with deficit counters
//! - Earliest Deadline First (EDF): deadline-aware scheduling for latency-sensitive flows
//! - Hierarchical Token Bucket (HTB): hierarchical bandwidth allocation with token buckets
//!
//! All methods are `no_std`-friendly (no external crates beyond what's already in Cargo.toml).

use std::collections::{BTreeMap, HashMap, VecDeque};

// ---------------------------------------------------------------------------
// Type aliases
// ---------------------------------------------------------------------------

/// Unique identifier for a scheduled stream.
pub type SpsStreamId = u64;

// ---------------------------------------------------------------------------
// Inline PRNG helper (xorshift64, no external crate)
// ---------------------------------------------------------------------------

#[inline]
pub fn xorshift64(state: &mut u64) -> u64 {
    let mut x = *state;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
    *state = x;
    x
}

// ---------------------------------------------------------------------------
// Scheduling policy
// ---------------------------------------------------------------------------

/// Scheduling algorithm used by [`StreamPriorityScheduler`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpsSchedulingPolicy {
    /// Always schedule the highest-priority non-blocked stream first.
    StrictPriority,
    /// Proportional share scheduling based on stream weight.
    WeightedFairQueuing,
    /// Deficit Round Robin – byte-accurate fairness using a per-stream deficit counter.
    DeficitRoundRobin,
    /// Earliest Deadline First – streams with the smallest deadline value are scheduled first.
    EarliestDeadlineFirst,
    /// Hierarchical Token Bucket – hierarchical bandwidth allocation with configurable rates.
    HierarchicalToken,
}

// ---------------------------------------------------------------------------
// Scheduler configuration
// ---------------------------------------------------------------------------

/// Configuration for [`StreamPriorityScheduler`].
#[derive(Debug, Clone)]
pub struct SpsSchedulerConfig {
    /// Maximum number of concurrent streams allowed.
    pub max_streams: usize,
    /// Number of bytes granted to each stream per DRR quantum.
    pub quantum_bytes: u64,
    /// Priority value at or above which strict priority scheduling is applied
    /// (streams with `priority >= strict_priority_threshold` bypass WFQ/DRR).
    pub strict_priority_threshold: u32,
    /// Number of DRR rounds to run in a single [`StreamPriorityScheduler::run_drr_round`] call.
    pub deficit_rounds: u32,
}

impl Default for SpsSchedulerConfig {
    fn default() -> Self {
        Self {
            max_streams: 1024,
            quantum_bytes: 1500,
            strict_priority_threshold: 240,
            deficit_rounds: 1,
        }
    }
}

// ---------------------------------------------------------------------------
// Per-stream state
// ---------------------------------------------------------------------------

/// State tracked for a single multiplexed stream.
#[derive(Debug, Clone)]
pub struct SpsStream {
    /// Unique stream identifier.
    pub id: SpsStreamId,
    /// Scheduling priority (higher = more important; 255 is highest).
    pub priority: u32,
    /// Relative weight used for proportional-share policies.
    pub weight: u32,
    /// Number of bytes waiting to be sent.
    pub pending_bytes: u64,
    /// DRR deficit counter (may be negative after a partial send).
    pub deficit_counter: i64,
    /// Total number of scheduling events for this stream.
    pub send_count: u64,
    /// Total bytes transmitted so far.
    pub bytes_sent: u64,
    /// Timestamp (monotonic, arbitrary epoch) of the last scheduling event.
    pub last_scheduled_ts: u64,
    /// If `true` the stream is temporarily suspended and will not be scheduled.
    pub is_blocked: bool,
    /// Optional deadline value (lower = more urgent; used by EDF policy).
    pub deadline: u64,
    /// Token bucket for HTB policy: current token balance in bytes.
    pub htb_tokens: i64,
    /// Token refill rate in bytes per "tick" for HTB.
    pub htb_rate: u64,
    /// Maximum burst size in bytes for HTB.
    pub htb_burst: u64,
}

impl SpsStream {
    /// Create a new stream with the given parameters.
    pub fn new(id: SpsStreamId, priority: u32, weight: u32) -> Self {
        Self {
            id,
            priority,
            weight: weight.max(1),
            pending_bytes: 0,
            deficit_counter: 0,
            send_count: 0,
            bytes_sent: 0,
            last_scheduled_ts: 0,
            is_blocked: false,
            deadline: u64::MAX,
            htb_tokens: 0,
            htb_rate: 1500,
            htb_burst: 65535,
        }
    }

    /// Returns `true` if the stream has data to send and is not blocked.
    #[inline]
    pub fn is_eligible(&self) -> bool {
        !self.is_blocked && self.pending_bytes > 0
    }
}

// ---------------------------------------------------------------------------
// Scheduler statistics
// ---------------------------------------------------------------------------

/// Aggregate scheduling statistics.
#[derive(Debug, Clone, Default)]
pub struct SpsSchedulerStats {
    /// Total number of scheduling decisions made.
    pub total_scheduled: u64,
    /// Total bytes dispatched across all streams.
    pub total_bytes: u64,
    /// Per-priority-level count of scheduling events.
    pub priority_distribution: HashMap<u32, u64>,
    /// Average wait (in ticks/rounds) across streams.
    pub avg_wait: f64,
    /// Number of streams currently registered.
    pub active_streams: usize,
    /// Number of blocked streams.
    pub blocked_streams: usize,
    /// Number of streams that have been removed since creation.
    pub streams_removed: u64,
    /// Jain's fairness index over bytes_sent (computed lazily).
    pub fairness_index: f64,
}

// ---------------------------------------------------------------------------
// Internal DRR active list entry
// ---------------------------------------------------------------------------

/// Lightweight handle used in the DRR active list.
#[derive(Debug, Clone)]
struct DrrEntry {
    stream_id: SpsStreamId,
    priority: u32,
}

// ---------------------------------------------------------------------------
// Main scheduler
// ---------------------------------------------------------------------------

/// A stream-level priority scheduler for multiplexed network streams.
///
/// # Scheduling Policies
///
/// Use [`SpsSchedulingPolicy`] to select the algorithm per call or batch.
///
/// # Example
/// ```rust
/// use ipfrs_network::stream_priority_scheduler::{
///     StreamPriorityScheduler, SpsSchedulingPolicy, SpsSchedulerConfig,
/// };
///
/// let mut scheduler = StreamPriorityScheduler::new(SpsSchedulerConfig::default());
/// scheduler.add_stream(1, 100, 10).unwrap();
/// scheduler.add_stream(2, 50,  5).unwrap();
/// scheduler.enqueue_bytes(1, 4096).unwrap();
/// scheduler.enqueue_bytes(2, 2048).unwrap();
///
/// let result = scheduler.schedule_next(&SpsSchedulingPolicy::StrictPriority);
/// assert_eq!(result.map(|(id, _)| id), Some(1));
/// ```
pub struct StreamPriorityScheduler {
    /// All registered streams keyed by `SpsStreamId`.
    streams: HashMap<SpsStreamId, SpsStream>,
    /// Priority queues: priority value → FIFO queue of stream IDs at that level.
    priority_queues: BTreeMap<u32, VecDeque<SpsStreamId>>,
    /// Scheduler configuration.
    config: SpsSchedulerConfig,
    /// Aggregate statistics.
    stats: SpsSchedulerStats,
    /// Monotonic tick counter incremented on each [`schedule_next`] call.
    tick: u64,
    /// DRR active-list maintained across rounds.
    drr_active: VecDeque<DrrEntry>,
    /// PRNG state (xorshift64) used for tie-breaking.
    rng_state: u64,
}

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors returned by [`StreamPriorityScheduler`] operations.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum SpsError {
    #[error("stream {0} not found")]
    StreamNotFound(SpsStreamId),
    #[error("maximum stream limit ({0}) reached")]
    MaxStreamsReached(usize),
    #[error("stream {0} already registered")]
    DuplicateStream(SpsStreamId),
    #[error("weight must be >= 1")]
    InvalidWeight,
    #[error("batch size must be > 0")]
    InvalidBatchSize,
}

// ---------------------------------------------------------------------------
// Implementation
// ---------------------------------------------------------------------------

impl StreamPriorityScheduler {
    /// Create a new scheduler with the supplied configuration.
    pub fn new(config: SpsSchedulerConfig) -> Self {
        Self {
            streams: HashMap::new(),
            priority_queues: BTreeMap::new(),
            config,
            stats: SpsSchedulerStats::default(),
            tick: 0,
            drr_active: VecDeque::new(),
            rng_state: 0xdeadbeef_cafebabe,
        }
    }

    // -----------------------------------------------------------------------
    // Stream lifecycle
    // -----------------------------------------------------------------------

    /// Register a new stream.
    ///
    /// # Errors
    /// Returns [`SpsError::MaxStreamsReached`] if `config.max_streams` is exceeded,
    /// or [`SpsError::DuplicateStream`] if the id is already registered.
    pub fn add_stream(
        &mut self,
        id: SpsStreamId,
        priority: u32,
        weight: u32,
    ) -> Result<(), SpsError> {
        if self.streams.len() >= self.config.max_streams {
            return Err(SpsError::MaxStreamsReached(self.config.max_streams));
        }
        if self.streams.contains_key(&id) {
            return Err(SpsError::DuplicateStream(id));
        }
        if weight == 0 {
            return Err(SpsError::InvalidWeight);
        }
        let stream = SpsStream::new(id, priority, weight);
        self.streams.insert(id, stream);
        self.stats.active_streams = self.streams.len();
        Ok(())
    }

    /// Remove a stream and all its pending data from the scheduler.
    ///
    /// # Errors
    /// Returns [`SpsError::StreamNotFound`] if the id is not registered.
    pub fn remove_stream(&mut self, id: SpsStreamId) -> Result<SpsStream, SpsError> {
        let stream = self
            .streams
            .remove(&id)
            .ok_or(SpsError::StreamNotFound(id))?;

        // Remove from every priority queue it may occupy.
        for queue in self.priority_queues.values_mut() {
            queue.retain(|&sid| sid != id);
        }
        // Remove from DRR active list.
        self.drr_active.retain(|e| e.stream_id != id);

        // Prune empty priority queues.
        self.priority_queues.retain(|_, q| !q.is_empty());

        self.stats.active_streams = self.streams.len();
        self.stats.streams_removed += 1;
        Ok(stream)
    }

    /// Block a stream — it will be skipped during scheduling.
    ///
    /// # Errors
    /// Returns [`SpsError::StreamNotFound`] if the id is not registered.
    pub fn block_stream(&mut self, id: SpsStreamId) -> Result<(), SpsError> {
        let stream = self
            .streams
            .get_mut(&id)
            .ok_or(SpsError::StreamNotFound(id))?;
        stream.is_blocked = true;
        self.stats.blocked_streams = self.streams.values().filter(|s| s.is_blocked).count();
        Ok(())
    }

    /// Unblock a previously blocked stream.
    ///
    /// # Errors
    /// Returns [`SpsError::StreamNotFound`] if the id is not registered.
    pub fn unblock_stream(&mut self, id: SpsStreamId) -> Result<(), SpsError> {
        let stream = self
            .streams
            .get_mut(&id)
            .ok_or(SpsError::StreamNotFound(id))?;
        stream.is_blocked = false;
        self.stats.blocked_streams = self.streams.values().filter(|s| s.is_blocked).count();
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Data enqueue
    // -----------------------------------------------------------------------

    /// Add `bytes` of pending data to a stream's queue.
    ///
    /// Also inserts the stream into the appropriate priority queue if it was
    /// previously empty.
    ///
    /// # Errors
    /// Returns [`SpsError::StreamNotFound`] if the id is not registered.
    pub fn enqueue_bytes(&mut self, stream_id: SpsStreamId, bytes: u64) -> Result<(), SpsError> {
        let stream = self
            .streams
            .get_mut(&stream_id)
            .ok_or(SpsError::StreamNotFound(stream_id))?;

        let was_empty = stream.pending_bytes == 0;
        stream.pending_bytes = stream.pending_bytes.saturating_add(bytes);

        if was_empty && !stream.is_blocked {
            let priority = stream.priority;
            self.priority_queues
                .entry(priority)
                .or_default()
                .push_back(stream_id);
        }
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Core scheduling
    // -----------------------------------------------------------------------

    /// Schedule the next stream according to `policy`.
    ///
    /// Returns `Some((stream_id, bytes_to_send))` where `bytes_to_send` is the
    /// number of bytes that should be dispatched this round (bounded by
    /// `config.quantum_bytes` for non-strict policies).
    ///
    /// Returns `None` if there are no eligible streams.
    pub fn schedule_next(&mut self, policy: &SpsSchedulingPolicy) -> Option<(SpsStreamId, u64)> {
        self.tick += 1;
        match policy {
            SpsSchedulingPolicy::StrictPriority => self.schedule_strict(),
            SpsSchedulingPolicy::WeightedFairQueuing => self.schedule_wfq(),
            SpsSchedulingPolicy::DeficitRoundRobin => self.schedule_drr_single(),
            SpsSchedulingPolicy::EarliestDeadlineFirst => self.schedule_edf(),
            SpsSchedulingPolicy::HierarchicalToken => self.schedule_htb(),
        }
    }

    /// Schedule a batch of up to `n` streams.
    ///
    /// Each element of the returned `Vec` is `(stream_id, bytes_to_send)`.
    ///
    /// # Errors (returned via empty vec)
    /// If `n == 0` the returned vec is empty.
    pub fn schedule_batch(
        &mut self,
        policy: &SpsSchedulingPolicy,
        n: usize,
    ) -> Vec<(SpsStreamId, u64)> {
        let mut results = Vec::with_capacity(n);
        for _ in 0..n {
            match self.schedule_next(policy) {
                Some(item) => results.push(item),
                None => break,
            }
        }
        results
    }

    // -----------------------------------------------------------------------
    // DRR round
    // -----------------------------------------------------------------------

    /// Run one or more complete Deficit Round Robin rounds.
    ///
    /// Each eligible stream receives a quantum of `config.quantum_bytes` added
    /// to its deficit counter. Streams transmit until their deficit is
    /// exhausted. The number of rounds is controlled by `config.deficit_rounds`.
    ///
    /// Returns a list of `(stream_id, bytes_to_send)` decisions from this round.
    pub fn run_drr_round(&mut self) -> Vec<(SpsStreamId, u64)> {
        let rounds = self.config.deficit_rounds as usize;
        let quantum = self.config.quantum_bytes;
        let mut results = Vec::new();

        for _ in 0..rounds {
            // Rebuild active list from eligible streams if empty.
            if self.drr_active.is_empty() {
                for stream in self.streams.values() {
                    if stream.is_eligible() {
                        self.drr_active.push_back(DrrEntry {
                            stream_id: stream.id,
                            priority: stream.priority,
                        });
                    }
                }
            }

            let mut processed = VecDeque::new();
            let len = self.drr_active.len();

            for _ in 0..len {
                let entry = match self.drr_active.pop_front() {
                    Some(e) => e,
                    None => break,
                };

                let stream = match self.streams.get_mut(&entry.stream_id) {
                    Some(s) => s,
                    None => continue,
                };

                if !stream.is_eligible() {
                    // Stream became empty or blocked; skip and do not re-add.
                    continue;
                }

                // Add quantum to deficit.
                stream.deficit_counter += quantum as i64;

                // Drain as much pending data as deficit allows.
                while stream.pending_bytes > 0 && stream.deficit_counter > 0 {
                    let send = stream.pending_bytes.min(stream.deficit_counter as u64);
                    stream.deficit_counter -= send as i64;
                    stream.pending_bytes -= send;
                    stream.bytes_sent += send;
                    stream.send_count += 1;
                    stream.last_scheduled_ts = self.tick;
                    self.tick += 1;
                    self.stats.total_scheduled += 1;
                    self.stats.total_bytes += send;
                    *self
                        .stats
                        .priority_distribution
                        .entry(stream.priority)
                        .or_insert(0) += 1;
                    results.push((entry.stream_id, send));
                }

                // If still has data, re-enqueue.
                if stream.pending_bytes > 0 {
                    processed.push_back(DrrEntry {
                        stream_id: entry.stream_id,
                        priority: entry.priority,
                    });
                } else {
                    // Reset deficit when queue empties.
                    stream.deficit_counter = 0;
                }
            }

            self.drr_active = processed;
        }

        results
    }

    // -----------------------------------------------------------------------
    // Fairness metric
    // -----------------------------------------------------------------------

    /// Compute Jain's fairness index over all streams' `bytes_sent`.
    ///
    /// Returns a value in `[0.0, 1.0]` where `1.0` is perfectly fair.
    /// Returns `1.0` if there are fewer than two streams.
    pub fn compute_fairness(&self) -> f64 {
        let streams: Vec<f64> = self.streams.values().map(|s| s.bytes_sent as f64).collect();

        let n = streams.len();
        if n < 2 {
            return 1.0;
        }

        let sum: f64 = streams.iter().sum();
        let sum_sq: f64 = streams.iter().map(|x| x * x).sum();

        if sum_sq == 0.0 {
            return 1.0;
        }

        (sum * sum) / (n as f64 * sum_sq)
    }

    // -----------------------------------------------------------------------
    // Statistics
    // -----------------------------------------------------------------------

    /// Return a snapshot of current scheduler statistics.
    pub fn scheduler_stats(&mut self) -> SpsSchedulerStats {
        self.stats.active_streams = self.streams.len();
        self.stats.blocked_streams = self.streams.values().filter(|s| s.is_blocked).count();
        self.stats.fairness_index = self.compute_fairness();
        // Compute avg_wait as mean of (tick - last_scheduled_ts) across all streams
        // that have been scheduled at least once.
        let scheduled: Vec<u64> = self
            .streams
            .values()
            .filter(|s| s.send_count > 0)
            .map(|s| self.tick.saturating_sub(s.last_scheduled_ts))
            .collect();
        if scheduled.is_empty() {
            self.stats.avg_wait = 0.0;
        } else {
            self.stats.avg_wait = scheduled.iter().sum::<u64>() as f64 / scheduled.len() as f64;
        }
        self.stats.clone()
    }

    // -----------------------------------------------------------------------
    // Accessors
    // -----------------------------------------------------------------------

    /// Return a reference to a stream, or `None`.
    pub fn get_stream(&self, id: SpsStreamId) -> Option<&SpsStream> {
        self.streams.get(&id)
    }

    /// Return a mutable reference to a stream, or `None`.
    pub fn get_stream_mut(&mut self, id: SpsStreamId) -> Option<&mut SpsStream> {
        self.streams.get_mut(&id)
    }

    /// Set the deadline for EDF scheduling on a stream.
    ///
    /// Smaller values are scheduled earlier.
    pub fn set_deadline(&mut self, id: SpsStreamId, deadline: u64) -> Result<(), SpsError> {
        let stream = self
            .streams
            .get_mut(&id)
            .ok_or(SpsError::StreamNotFound(id))?;
        stream.deadline = deadline;
        Ok(())
    }

    /// Configure HTB token-bucket parameters for a stream.
    pub fn set_htb_params(
        &mut self,
        id: SpsStreamId,
        rate: u64,
        burst: u64,
    ) -> Result<(), SpsError> {
        let stream = self
            .streams
            .get_mut(&id)
            .ok_or(SpsError::StreamNotFound(id))?;
        stream.htb_rate = rate.max(1);
        stream.htb_burst = burst.max(1);
        Ok(())
    }

    /// Refill HTB token buckets for all streams by `ticks` time units.
    pub fn htb_refill(&mut self, ticks: u64) {
        for stream in self.streams.values_mut() {
            let refill = (stream.htb_rate * ticks) as i64;
            stream.htb_tokens = (stream.htb_tokens + refill).min(stream.htb_burst as i64);
        }
    }

    /// Return the current tick counter.
    pub fn tick(&self) -> u64 {
        self.tick
    }

    /// Return the number of currently registered streams.
    pub fn stream_count(&self) -> usize {
        self.streams.len()
    }

    /// Return the number of eligible (non-blocked, non-empty) streams.
    pub fn eligible_count(&self) -> usize {
        self.streams.values().filter(|s| s.is_eligible()).count()
    }

    /// Drain all pending bytes from a stream without scheduling it.
    ///
    /// Useful for flow-control / back-pressure scenarios.
    pub fn drain_stream(&mut self, id: SpsStreamId) -> Result<u64, SpsError> {
        let stream = self
            .streams
            .get_mut(&id)
            .ok_or(SpsError::StreamNotFound(id))?;
        let drained = stream.pending_bytes;
        stream.pending_bytes = 0;
        stream.deficit_counter = 0;
        // Remove from priority queue.
        if let Some(queue) = self.priority_queues.get_mut(&stream.priority) {
            queue.retain(|&sid| sid != id);
        }
        self.drr_active.retain(|e| e.stream_id != id);
        Ok(drained)
    }

    /// Update the priority of a stream.
    ///
    /// This moves the stream to the new priority queue if it currently has data.
    pub fn update_priority(&mut self, id: SpsStreamId, new_priority: u32) -> Result<(), SpsError> {
        let stream = self
            .streams
            .get_mut(&id)
            .ok_or(SpsError::StreamNotFound(id))?;
        let old_priority = stream.priority;
        let has_data = stream.pending_bytes > 0 && !stream.is_blocked;
        stream.priority = new_priority;

        if has_data && old_priority != new_priority {
            // Remove from old queue.
            if let Some(q) = self.priority_queues.get_mut(&old_priority) {
                q.retain(|&sid| sid != id);
            }
            // Insert into new queue.
            self.priority_queues
                .entry(new_priority)
                .or_default()
                .push_back(id);
        }
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Private scheduling helpers
    // -----------------------------------------------------------------------

    /// Strict Priority: pick the first eligible stream from the highest non-empty queue.
    ///
    /// Among streams tied at the same priority and same `bytes_sent`, a xorshift64 PRNG
    /// step provides stochastic tie-breaking to avoid systematic starvation.
    fn schedule_strict(&mut self) -> Option<(SpsStreamId, u64)> {
        let quantum = self.config.quantum_bytes;

        // Advance the PRNG once per scheduling call to drive tie-breaking.
        let _jitter = xorshift64(&mut self.rng_state);

        // Iterate from highest priority (largest key) downward.
        let chosen = self.priority_queues.iter().rev().find_map(|(_, queue)| {
            queue
                .iter()
                .find(|&&sid| {
                    self.streams
                        .get(&sid)
                        .map(|s| s.is_eligible())
                        .unwrap_or(false)
                })
                .copied()
        });

        let id = chosen?;
        self.dispatch_bytes(id, quantum)
    }

    /// Weighted Fair Queuing: pick eligible stream with highest normalised virtual finish time
    /// (approximated as bytes_sent / weight — lowest wins = we invert to select smallest).
    fn schedule_wfq(&mut self) -> Option<(SpsStreamId, u64)> {
        let quantum = self.config.quantum_bytes;
        let strict_threshold = self.config.strict_priority_threshold;

        // First check for strict-priority streams above the threshold.
        let strict_id = self.priority_queues.iter().rev().find_map(|(&pri, queue)| {
            if pri < strict_threshold {
                return None;
            }
            queue
                .iter()
                .find(|&&sid| {
                    self.streams
                        .get(&sid)
                        .map(|s| s.is_eligible())
                        .unwrap_or(false)
                })
                .copied()
        });

        if let Some(id) = strict_id {
            return self.dispatch_bytes(id, quantum);
        }

        // Among the remaining eligible streams pick the one with smallest
        // virtual_time = bytes_sent / weight (smallest = most deserving).
        let best = self
            .streams
            .values()
            .filter(|s| s.is_eligible() && s.priority < strict_threshold)
            .min_by(|a, b| {
                let va = if a.weight == 0 {
                    f64::MAX
                } else {
                    a.bytes_sent as f64 / a.weight as f64
                };
                let vb = if b.weight == 0 {
                    f64::MAX
                } else {
                    b.bytes_sent as f64 / b.weight as f64
                };
                va.partial_cmp(&vb).unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|s| s.id);

        let id = best?;
        self.dispatch_bytes(id, quantum)
    }

    /// Single-shot DRR step: pick next stream from DRR active list.
    fn schedule_drr_single(&mut self) -> Option<(SpsStreamId, u64)> {
        let quantum = self.config.quantum_bytes;

        // Rebuild active list if empty.
        if self.drr_active.is_empty() {
            for stream in self.streams.values() {
                if stream.is_eligible() {
                    self.drr_active.push_back(DrrEntry {
                        stream_id: stream.id,
                        priority: stream.priority,
                    });
                }
            }
        }

        // Walk active list until we find an eligible stream with positive deficit.
        let list_len = self.drr_active.len();
        for _ in 0..list_len {
            let entry = self.drr_active.pop_front()?;
            let stream = match self.streams.get_mut(&entry.stream_id) {
                Some(s) => s,
                None => continue,
            };

            if !stream.is_eligible() {
                continue;
            }

            // Add quantum.
            stream.deficit_counter += quantum as i64;

            if stream.deficit_counter > 0 {
                let send = stream.pending_bytes.min(stream.deficit_counter as u64);
                stream.deficit_counter -= send as i64;
                stream.pending_bytes -= send;
                stream.bytes_sent += send;
                stream.send_count += 1;
                stream.last_scheduled_ts = self.tick;
                self.stats.total_scheduled += 1;
                self.stats.total_bytes += send;
                *self
                    .stats
                    .priority_distribution
                    .entry(stream.priority)
                    .or_insert(0) += 1;

                if stream.pending_bytes > 0 {
                    self.drr_active.push_back(DrrEntry {
                        stream_id: entry.stream_id,
                        priority: entry.priority,
                    });
                } else {
                    stream.deficit_counter = 0;
                }
                return Some((entry.stream_id, send));
            } else {
                // Not enough deficit yet; re-queue at back.
                self.drr_active.push_back(entry);
            }
        }
        None
    }

    /// Earliest Deadline First: pick the eligible stream with the smallest deadline.
    fn schedule_edf(&mut self) -> Option<(SpsStreamId, u64)> {
        let quantum = self.config.quantum_bytes;

        let best = self
            .streams
            .values()
            .filter(|s| s.is_eligible())
            .min_by_key(|s| s.deadline)
            .map(|s| s.id);

        let id = best?;
        self.dispatch_bytes(id, quantum)
    }

    /// Hierarchical Token Bucket: pick eligible stream with sufficient tokens.
    fn schedule_htb(&mut self) -> Option<(SpsStreamId, u64)> {
        let quantum = self.config.quantum_bytes;

        // Prefer streams that have positive token balance, then fall back to SP.
        let tokenized = self
            .streams
            .values()
            .filter(|s| s.is_eligible() && s.htb_tokens > 0)
            .max_by_key(|s| s.priority)
            .map(|s| s.id);

        let id = if let Some(id) = tokenized {
            id
        } else {
            // Fall back to strict priority when no tokens are available.
            self.streams
                .values()
                .filter(|s| s.is_eligible())
                .max_by_key(|s| s.priority)
                .map(|s| s.id)?
        };

        // Deduct tokens.
        if let Some(stream) = self.streams.get_mut(&id) {
            let send = stream.pending_bytes.min(quantum);
            stream.htb_tokens -= send as i64;
            // htb_tokens may go negative (borrow from future); caller should call htb_refill.
        }

        self.dispatch_bytes(id, quantum)
    }

    /// Shared finalisation: record stats and deduct pending bytes.
    ///
    /// Returns `Some((id, bytes_sent))` or `None` if the stream has no data.
    fn dispatch_bytes(&mut self, id: SpsStreamId, max_bytes: u64) -> Option<(SpsStreamId, u64)> {
        let stream = self.streams.get_mut(&id)?;
        if stream.pending_bytes == 0 {
            return None;
        }
        let send = stream.pending_bytes.min(max_bytes);
        stream.pending_bytes -= send;
        stream.bytes_sent += send;
        stream.send_count += 1;
        stream.last_scheduled_ts = self.tick;

        let priority = stream.priority;
        self.stats.total_scheduled += 1;
        self.stats.total_bytes += send;
        *self
            .stats
            .priority_distribution
            .entry(priority)
            .or_insert(0) += 1;

        // Remove from priority queue if empty.
        if stream.pending_bytes == 0 {
            if let Some(q) = self.priority_queues.get_mut(&priority) {
                q.retain(|&sid| sid != id);
            }
        }

        Some((id, send))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_scheduler() -> StreamPriorityScheduler {
        StreamPriorityScheduler::new(SpsSchedulerConfig::default())
    }

    // ── Construction ──────────────────────────────────────────────────────

    #[test]
    fn test_new_scheduler_is_empty() {
        let s = make_scheduler();
        assert_eq!(s.stream_count(), 0);
        assert_eq!(s.eligible_count(), 0);
    }

    #[test]
    fn test_default_config_values() {
        let cfg = SpsSchedulerConfig::default();
        assert_eq!(cfg.max_streams, 1024);
        assert!(cfg.quantum_bytes > 0);
        assert!(cfg.deficit_rounds >= 1);
    }

    // ── add_stream ─────────────────────────────────────────────────────────

    #[test]
    fn test_add_stream_basic() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        assert_eq!(s.stream_count(), 1);
    }

    #[test]
    fn test_add_stream_duplicate_errors() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        assert!(matches!(
            s.add_stream(1, 5, 1),
            Err(SpsError::DuplicateStream(1))
        ));
    }

    #[test]
    fn test_add_stream_zero_weight_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.add_stream(1, 10, 0),
            Err(SpsError::InvalidWeight)
        ));
    }

    #[test]
    fn test_add_stream_max_limit() {
        let cfg = SpsSchedulerConfig {
            max_streams: 2,
            ..Default::default()
        };
        let mut s = StreamPriorityScheduler::new(cfg);
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        assert!(matches!(
            s.add_stream(3, 10, 1),
            Err(SpsError::MaxStreamsReached(2))
        ));
    }

    // ── remove_stream ──────────────────────────────────────────────────────

    #[test]
    fn test_remove_existing_stream() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        let removed = s
            .remove_stream(1)
            .expect("test: remove_stream should succeed");
        assert_eq!(removed.id, 1);
        assert_eq!(s.stream_count(), 0);
    }

    #[test]
    fn test_remove_nonexistent_stream_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.remove_stream(99),
            Err(SpsError::StreamNotFound(99))
        ));
    }

    #[test]
    fn test_remove_stream_cleans_priority_queue() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 1000)
            .expect("test: enqueue_bytes should succeed");
        s.remove_stream(1)
            .expect("test: remove_stream should succeed");
        // No panic and priority_queues should not contain stream 1.
        let result = s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        assert!(result.is_none());
    }

    // ── block / unblock ────────────────────────────────────────────────────

    #[test]
    fn test_block_stream() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        s.block_stream(1)
            .expect("test: block_stream should succeed");
        assert!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .is_blocked
        );
        let result = s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        assert!(result.is_none());
    }

    #[test]
    fn test_unblock_stream() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        s.block_stream(1)
            .expect("test: block_stream should succeed");
        s.unblock_stream(1)
            .expect("test: unblock_stream should succeed");
        assert!(
            !s.get_stream(1)
                .expect("test: stream 1 should exist")
                .is_blocked
        );
        // After unblocking the stream must be re-enqueued for scheduling.
        s.enqueue_bytes(1, 0).unwrap_or_default(); // no-op but covers the path
    }

    #[test]
    fn test_block_nonexistent_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.block_stream(7),
            Err(SpsError::StreamNotFound(7))
        ));
    }

    #[test]
    fn test_unblock_nonexistent_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.unblock_stream(7),
            Err(SpsError::StreamNotFound(7))
        ));
    }

    // ── enqueue_bytes ──────────────────────────────────────────────────────

    #[test]
    fn test_enqueue_bytes_increases_pending() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 512)
            .expect("test: enqueue_bytes should succeed");
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .pending_bytes,
            512
        );
    }

    #[test]
    fn test_enqueue_bytes_accumulates() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: first enqueue_bytes should succeed");
        s.enqueue_bytes(1, 200)
            .expect("test: second enqueue_bytes should succeed");
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .pending_bytes,
            300
        );
    }

    #[test]
    fn test_enqueue_bytes_nonexistent_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.enqueue_bytes(42, 100),
            Err(SpsError::StreamNotFound(42))
        ));
    }

    // ── StrictPriority ─────────────────────────────────────────────────────

    #[test]
    fn test_strict_priority_basic() {
        let mut s = make_scheduler();
        s.add_stream(1, 100, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 50, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 1500)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 1500)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        let result = s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        assert_eq!(result.map(|(id, _)| id), Some(1));
    }

    #[test]
    fn test_strict_priority_selects_highest() {
        let mut s = make_scheduler();
        for pri in [10u32, 50, 200, 5] {
            s.add_stream(pri as u64, pri, 1)
                .expect("test: add_stream should succeed");
            s.enqueue_bytes(pri as u64, 3000)
                .expect("test: enqueue_bytes should succeed");
        }
        let (id, _) = s
            .schedule_next(&SpsSchedulingPolicy::StrictPriority)
            .expect("test: schedule_next should return Some");
        assert_eq!(id, 200);
    }

    #[test]
    fn test_strict_priority_no_eligible_returns_none() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        // No enqueue, so nothing eligible.
        let result = s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        assert!(result.is_none());
    }

    #[test]
    fn test_strict_priority_exhausts_stream() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        // quantum is 1500, so single call should drain the stream.
        let (_, sent) = s
            .schedule_next(&SpsSchedulingPolicy::StrictPriority)
            .expect("test: schedule_next should return Some");
        assert_eq!(sent, 100);
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .pending_bytes,
            0
        );
    }

    // ── WeightedFairQueuing ────────────────────────────────────────────────

    #[test]
    fn test_wfq_basic_scheduling() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 10)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 5000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        // Both streams start at 0 bytes_sent, so weight does not matter yet.
        let r = s.schedule_next(&SpsSchedulingPolicy::WeightedFairQueuing);
        assert!(r.is_some());
    }

    #[test]
    fn test_wfq_strict_threshold_override() {
        let cfg = SpsSchedulerConfig {
            strict_priority_threshold: 100,
            ..Default::default()
        };
        let mut s = StreamPriorityScheduler::new(cfg);
        s.add_stream(1, 200, 1)
            .expect("test: add_stream 1 should succeed"); // above threshold
        s.add_stream(2, 50, 100)
            .expect("test: add_stream 2 should succeed"); // below threshold, high weight
        s.enqueue_bytes(1, 3000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 3000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        let (id, _) = s
            .schedule_next(&SpsSchedulingPolicy::WeightedFairQueuing)
            .expect("test: schedule_next should return Some");
        assert_eq!(id, 1); // strict priority stream should win
    }

    #[test]
    fn test_wfq_proportional_allocation() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 2)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 100_000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 100_000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        // Run many rounds and check that stream 1 got roughly 2x bytes.
        let batch = s.schedule_batch(&SpsSchedulingPolicy::WeightedFairQueuing, 1000);
        assert!(!batch.is_empty());
    }

    // ── DeficitRoundRobin ──────────────────────────────────────────────────

    #[test]
    fn test_drr_single_step() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 3000)
            .expect("test: enqueue_bytes should succeed");
        let r = s.schedule_next(&SpsSchedulingPolicy::DeficitRoundRobin);
        assert!(r.is_some());
    }

    #[test]
    fn test_drr_round_basic() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 3000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 3000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        let results = s.run_drr_round();
        assert!(!results.is_empty());
    }

    #[test]
    fn test_drr_multiple_rounds() {
        let cfg = SpsSchedulerConfig {
            quantum_bytes: 500,
            deficit_rounds: 3,
            ..Default::default()
        };
        let mut s = StreamPriorityScheduler::new(cfg);
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 10000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 10000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        let results = s.run_drr_round();
        assert!(!results.is_empty());
    }

    #[test]
    fn test_drr_drain_eventually() {
        let cfg = SpsSchedulerConfig {
            quantum_bytes: 100,
            deficit_rounds: 1,
            ..Default::default()
        };
        let mut s = StreamPriorityScheduler::new(cfg);
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 300)
            .expect("test: enqueue_bytes should succeed");

        let mut total = 0u64;
        for _ in 0..20 {
            for (_, b) in s.run_drr_round() {
                total += b;
            }
        }
        assert_eq!(total, 300);
    }

    // ── EarliestDeadlineFirst ──────────────────────────────────────────────

    #[test]
    fn test_edf_selects_smallest_deadline() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.set_deadline(1, 1000)
            .expect("test: set_deadline for stream 1 should succeed");
        s.set_deadline(2, 500)
            .expect("test: set_deadline for stream 2 should succeed");
        s.enqueue_bytes(1, 1500)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 1500)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        let (id, _) = s
            .schedule_next(&SpsSchedulingPolicy::EarliestDeadlineFirst)
            .expect("test: schedule_next should return Some");
        assert_eq!(id, 2); // stream 2 has earlier deadline
    }

    #[test]
    fn test_edf_default_deadline_is_max() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        let r = s.schedule_next(&SpsSchedulingPolicy::EarliestDeadlineFirst);
        assert!(r.is_some());
    }

    #[test]
    fn test_set_deadline_nonexistent_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.set_deadline(99, 100),
            Err(SpsError::StreamNotFound(99))
        ));
    }

    // ── HierarchicalToken ──────────────────────────────────────────────────

    #[test]
    fn test_htb_requires_refill() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.set_htb_params(1, 1000, 10000)
            .expect("test: set_htb_params should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes should succeed");
        // Without refill, tokens = 0 → falls back to SP.
        let r = s.schedule_next(&SpsSchedulingPolicy::HierarchicalToken);
        // Should still return something (falls back to strict priority).
        assert!(r.is_some());
    }

    #[test]
    fn test_htb_with_refill() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.set_htb_params(1, 1000, 10000)
            .expect("test: set_htb_params should succeed");
        s.htb_refill(5); // Add 5000 tokens.
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes should succeed");
        let (id, _) = s
            .schedule_next(&SpsSchedulingPolicy::HierarchicalToken)
            .expect("test: schedule_next should return Some");
        assert_eq!(id, 1);
    }

    #[test]
    fn test_set_htb_params_nonexistent_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.set_htb_params(99, 1000, 2000),
            Err(SpsError::StreamNotFound(99))
        ));
    }

    // ── schedule_batch ─────────────────────────────────────────────────────

    #[test]
    fn test_schedule_batch_returns_up_to_n() {
        let mut s = make_scheduler();
        for i in 1u64..=5 {
            s.add_stream(i, 10, 1)
                .expect("test: add_stream should succeed");
            s.enqueue_bytes(i, 500)
                .expect("test: enqueue_bytes should succeed");
        }
        let batch = s.schedule_batch(&SpsSchedulingPolicy::StrictPriority, 3);
        assert!(batch.len() <= 3);
    }

    #[test]
    fn test_schedule_batch_stops_when_empty() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        let batch = s.schedule_batch(&SpsSchedulingPolicy::StrictPriority, 100);
        // Should stop once the single stream is drained.
        assert!(!batch.is_empty());
        let total: u64 = batch.iter().map(|(_, b)| b).sum();
        assert_eq!(total, 100);
    }

    #[test]
    fn test_schedule_batch_zero_n() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        let batch = s.schedule_batch(&SpsSchedulingPolicy::StrictPriority, 0);
        assert!(batch.is_empty());
    }

    // ── compute_fairness ───────────────────────────────────────────────────

    #[test]
    fn test_fairness_empty_scheduler() {
        let s = make_scheduler();
        assert!((s.compute_fairness() - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_fairness_single_stream() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        assert!((s.compute_fairness() - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_fairness_two_equal_streams() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 10000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 10000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        s.schedule_batch(&SpsSchedulingPolicy::WeightedFairQueuing, 200);
        let f = s.compute_fairness();
        // Two streams with equal weight should approach 1.0 fairness.
        assert!(f > 0.0 && f <= 1.0);
    }

    #[test]
    fn test_fairness_bounds() {
        let mut s = make_scheduler();
        for i in 1u64..=4 {
            s.add_stream(i, 10, 1)
                .expect("test: add_stream should succeed");
            s.enqueue_bytes(i, 5000)
                .expect("test: enqueue_bytes should succeed");
        }
        s.schedule_batch(&SpsSchedulingPolicy::WeightedFairQueuing, 200);
        let f = s.compute_fairness();
        assert!((0.0..=1.0 + 1e-9).contains(&f));
    }

    // ── scheduler_stats ────────────────────────────────────────────────────

    #[test]
    fn test_stats_initial_zeros() {
        let mut s = make_scheduler();
        let stats = s.scheduler_stats();
        assert_eq!(stats.total_scheduled, 0);
        assert_eq!(stats.total_bytes, 0);
        assert_eq!(stats.active_streams, 0);
    }

    #[test]
    fn test_stats_after_scheduling() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 1500)
            .expect("test: enqueue_bytes should succeed");
        s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        let stats = s.scheduler_stats();
        assert!(stats.total_scheduled > 0);
        assert!(stats.total_bytes > 0);
    }

    #[test]
    fn test_stats_priority_distribution() {
        let mut s = make_scheduler();
        s.add_stream(1, 42, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 1500)
            .expect("test: enqueue_bytes should succeed");
        s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        let stats = s.scheduler_stats();
        assert!(stats.priority_distribution.contains_key(&42));
    }

    #[test]
    fn test_stats_active_and_blocked() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.block_stream(1)
            .expect("test: block_stream should succeed");
        let stats = s.scheduler_stats();
        assert_eq!(stats.active_streams, 2);
        assert_eq!(stats.blocked_streams, 1);
    }

    // ── drain_stream ───────────────────────────────────────────────────────

    #[test]
    fn test_drain_stream() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes should succeed");
        let drained = s
            .drain_stream(1)
            .expect("test: drain_stream should succeed");
        assert_eq!(drained, 5000);
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .pending_bytes,
            0
        );
    }

    #[test]
    fn test_drain_nonexistent_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.drain_stream(99),
            Err(SpsError::StreamNotFound(99))
        ));
    }

    // ── update_priority ────────────────────────────────────────────────────

    #[test]
    fn test_update_priority() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 500)
            .expect("test: enqueue_bytes should succeed");
        s.update_priority(1, 200)
            .expect("test: update_priority should succeed");
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .priority,
            200
        );
    }

    #[test]
    fn test_update_priority_nonexistent_errors() {
        let mut s = make_scheduler();
        assert!(matches!(
            s.update_priority(99, 100),
            Err(SpsError::StreamNotFound(99))
        ));
    }

    #[test]
    fn test_update_priority_affects_scheduling() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 5000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        s.update_priority(2, 200)
            .expect("test: update_priority should succeed");
        // After priority change of stream 2, it should be re-enqueued via the next enqueue.
        s.enqueue_bytes(2, 0).unwrap_or_default();
        let (id, _) = s
            .schedule_next(&SpsSchedulingPolicy::StrictPriority)
            .expect("test: schedule_next should return Some");
        assert_eq!(id, 2);
    }

    // ── get_stream / get_stream_mut ────────────────────────────────────────

    #[test]
    fn test_get_stream_existing() {
        let mut s = make_scheduler();
        s.add_stream(5, 10, 2)
            .expect("test: add_stream should succeed");
        let stream = s.get_stream(5);
        assert!(stream.is_some());
        assert_eq!(stream.expect("test: stream 5 should exist").id, 5);
    }

    #[test]
    fn test_get_stream_nonexistent() {
        let s = make_scheduler();
        assert!(s.get_stream(99).is_none());
    }

    #[test]
    fn test_get_stream_mut() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        let stream = s
            .get_stream_mut(1)
            .expect("test: stream 1 should exist for mutable access");
        stream.weight = 99;
        assert_eq!(
            s.get_stream(1).expect("test: stream 1 should exist").weight,
            99
        );
    }

    // ── xorshift64 ─────────────────────────────────────────────────────────

    #[test]
    fn test_xorshift64_non_zero() {
        let mut state = 0xdeadbeef_u64;
        let v = xorshift64(&mut state);
        assert_ne!(v, 0);
    }

    #[test]
    fn test_xorshift64_different_on_repeated_calls() {
        let mut state = 12345678u64;
        let a = xorshift64(&mut state);
        let b = xorshift64(&mut state);
        assert_ne!(a, b);
    }

    // ── Miscellaneous ──────────────────────────────────────────────────────

    #[test]
    fn test_eligible_count() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        assert_eq!(s.eligible_count(), 1);
    }

    #[test]
    fn test_tick_increments() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 3000)
            .expect("test: enqueue_bytes should succeed");
        let t0 = s.tick();
        s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        assert!(s.tick() > t0);
    }

    #[test]
    fn test_bytes_sent_accumulates() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 3000)
            .expect("test: enqueue_bytes should succeed");
        s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        assert!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .bytes_sent
                > 0
        );
    }

    #[test]
    fn test_send_count_increments() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 1500)
            .expect("test: enqueue_bytes should succeed");
        s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .send_count,
            1
        );
    }

    #[test]
    fn test_multiple_policies_same_scheduler() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 20, 2)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 5000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        // Switch policies freely.
        let r1 = s.schedule_next(&SpsSchedulingPolicy::StrictPriority);
        let r2 = s.schedule_next(&SpsSchedulingPolicy::WeightedFairQueuing);
        let r3 = s.schedule_next(&SpsSchedulingPolicy::EarliestDeadlineFirst);
        assert!(r1.is_some() || r2.is_some() || r3.is_some());
    }

    #[test]
    fn test_large_enqueue_no_overflow() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, u64::MAX / 2)
            .expect("test: enqueue_bytes should succeed");
        s.enqueue_bytes(1, 1).unwrap_or_default(); // saturating add
                                                   // Should not panic.
    }

    #[test]
    fn test_streams_removed_counter() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.remove_stream(1)
            .expect("test: remove_stream should succeed");
        let stats = s.scheduler_stats();
        assert_eq!(stats.streams_removed, 1);
    }

    #[test]
    fn test_htb_refill_clamps_to_burst() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.set_htb_params(1, 1000, 5000)
            .expect("test: set_htb_params should succeed");
        s.htb_refill(1000); // would add 1_000_000 tokens without clamping
        let tok = s
            .get_stream(1)
            .expect("test: stream 1 should exist")
            .htb_tokens;
        assert_eq!(tok, 5000);
    }

    #[test]
    fn test_schedule_all_policies_no_eligible_returns_none() {
        let mut s = make_scheduler();
        let policies = [
            SpsSchedulingPolicy::StrictPriority,
            SpsSchedulingPolicy::WeightedFairQueuing,
            SpsSchedulingPolicy::DeficitRoundRobin,
            SpsSchedulingPolicy::EarliestDeadlineFirst,
            SpsSchedulingPolicy::HierarchicalToken,
        ];
        for policy in &policies {
            assert!(
                s.schedule_next(policy).is_none(),
                "policy {:?} should return None",
                policy
            );
        }
    }

    #[test]
    fn test_edf_multiple_streams_ordering() {
        let mut s = make_scheduler();
        let deadlines = [(1u64, 300u64), (2, 100), (3, 200), (4, 50)];
        for (id, dl) in &deadlines {
            s.add_stream(*id, 10, 1)
                .expect("test: add_stream should succeed");
            s.set_deadline(*id, *dl)
                .expect("test: set_deadline should succeed");
            s.enqueue_bytes(*id, 1500)
                .expect("test: enqueue_bytes should succeed");
        }
        let (first, _) = s
            .schedule_next(&SpsSchedulingPolicy::EarliestDeadlineFirst)
            .expect("test: schedule_next should return Some");
        assert_eq!(first, 4); // deadline = 50
    }

    #[test]
    fn test_wfq_no_eligible_returns_none() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed"); // no pending bytes
        let r = s.schedule_next(&SpsSchedulingPolicy::WeightedFairQueuing);
        assert!(r.is_none());
    }

    #[test]
    fn test_drr_empty_active_list_rebuilt() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 500)
            .expect("test: enqueue_bytes should succeed");
        // First call builds active list internally.
        let r1 = s.schedule_next(&SpsSchedulingPolicy::DeficitRoundRobin);
        assert!(r1.is_some());
    }

    #[test]
    fn test_fairness_index_in_stats() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 5000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        s.schedule_batch(&SpsSchedulingPolicy::WeightedFairQueuing, 50);
        let stats = s.scheduler_stats();
        assert!((0.0..=1.0 + 1e-9).contains(&stats.fairness_index));
    }

    #[test]
    fn test_update_priority_no_pending_no_queue_change() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        // No pending bytes, update priority should not error.
        s.update_priority(1, 50)
            .expect("test: update_priority should succeed");
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist")
                .priority,
            50
        );
    }

    #[test]
    fn test_scheduler_stats_avg_wait_zero_before_any_schedule() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        let stats = s.scheduler_stats();
        assert_eq!(stats.avg_wait, 0.0);
    }

    #[test]
    fn test_drr_run_round_with_no_streams() {
        let mut s = make_scheduler();
        let results = s.run_drr_round();
        assert!(results.is_empty());
    }

    #[test]
    fn test_blocked_stream_not_scheduled() {
        let mut s = make_scheduler();
        s.add_stream(1, 100, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 10, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 5000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        s.block_stream(1)
            .expect("test: block_stream should succeed");
        let (id, _) = s
            .schedule_next(&SpsSchedulingPolicy::StrictPriority)
            .expect("test: schedule_next should return Some");
        assert_eq!(id, 2); // blocked stream 1 should not be chosen
    }

    #[test]
    fn test_stream_is_eligible_after_enqueue() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        assert!(!s
            .get_stream(1)
            .expect("test: stream 1 should exist")
            .is_eligible());
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        assert!(s
            .get_stream(1)
            .expect("test: stream 1 should exist")
            .is_eligible());
    }

    #[test]
    fn test_stream_not_eligible_when_blocked_even_with_data() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.enqueue_bytes(1, 100)
            .expect("test: enqueue_bytes should succeed");
        s.block_stream(1)
            .expect("test: block_stream should succeed");
        assert!(!s
            .get_stream(1)
            .expect("test: stream 1 should exist")
            .is_eligible());
    }

    #[test]
    fn test_sps_stream_new_defaults() {
        let stream = SpsStream::new(42, 100, 5);
        assert_eq!(stream.id, 42);
        assert_eq!(stream.priority, 100);
        assert_eq!(stream.weight, 5);
        assert_eq!(stream.pending_bytes, 0);
        assert!(!stream.is_blocked);
    }

    #[test]
    fn test_sps_error_display() {
        let e = SpsError::StreamNotFound(5);
        assert!(e.to_string().contains("5"));
    }

    #[test]
    fn test_add_multiple_streams_different_priorities() {
        let mut s = make_scheduler();
        for i in 0u64..10 {
            s.add_stream(i, (i * 10) as u32, 1)
                .expect("test: add_stream should succeed");
        }
        assert_eq!(s.stream_count(), 10);
    }

    #[test]
    fn test_remove_and_readd_same_id() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream should succeed");
        s.remove_stream(1)
            .expect("test: first remove_stream should succeed");
        s.add_stream(1, 20, 2)
            .expect("test: re-add_stream should succeed");
        assert_eq!(
            s.get_stream(1)
                .expect("test: stream 1 should exist after re-add")
                .priority,
            20
        );
    }

    #[test]
    fn test_htb_fallback_with_no_tokens() {
        let mut s = make_scheduler();
        s.add_stream(1, 10, 1)
            .expect("test: add_stream 1 should succeed");
        s.add_stream(2, 20, 1)
            .expect("test: add_stream 2 should succeed");
        s.enqueue_bytes(1, 5000)
            .expect("test: enqueue_bytes for stream 1 should succeed");
        s.enqueue_bytes(2, 5000)
            .expect("test: enqueue_bytes for stream 2 should succeed");
        // No refill → falls back to strict priority.
        let (id, _) = s
            .schedule_next(&SpsSchedulingPolicy::HierarchicalToken)
            .expect("test: schedule_next should return Some");
        assert_eq!(id, 2); // higher priority wins in fallback
    }
}