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
//! Queue storage implementation.
use super::{Error, cursor::Cursor, metrics::Metrics};
use crate::{
Context,
journal::contiguous::{Contiguous as _, variable},
};
use commonware_codec::CodecShared;
use commonware_macros::boxed;
use commonware_runtime::{
buffer::paged::CacheRef,
telemetry::metrics::{Gauge, GaugeExt as _},
};
use commonware_utils::channel::watch;
use std::{
borrow::Borrow,
num::{NonZeroU64, NonZeroUsize},
ops::Range,
sync::{
Arc,
atomic::{AtomicU64, Ordering},
},
};
use tracing::debug;
/// Configuration for [Queue].
#[derive(Clone)]
pub struct Config<C> {
/// The storage partition name for the queue's journal.
pub partition: String,
/// The number of items to store in each journal section.
///
/// Larger values reduce file overhead but increase minimum pruning granularity.
/// Once set, this value cannot be changed across restarts.
pub items_per_section: NonZeroU64,
/// Optional zstd compression level for stored items.
///
/// If set, items will be compressed before storage. Higher values provide
/// better compression but use more CPU.
///
/// Keep the choice between `None` and `Some(_)` fixed while stored items are retained.
/// Only the compression level may change between initializations when compression is enabled.
pub compression: Option<u8>,
/// Codec configuration for encoding/decoding items.
pub codec_config: C,
/// Page cache for buffering reads from the underlying journal.
pub page_cache: CacheRef,
/// Write buffer size for each section.
pub write_buffer: NonZeroUsize,
/// Buffer size for sequential reads during recovery.
pub replay_buffer: NonZeroUsize,
}
/// A published view of the queue's items.
type Snapshot<E, V> = Arc<variable::Reader<'static, E, V>>;
/// A durable, at-least-once delivery queue with per-item acknowledgment.
///
/// Items are durably stored in a journal and survive crashes. [Queue::init] returns the queue,
/// which writes items, and its [Reader], which delivers them. The reader must acknowledge each
/// item individually after processing. Items can be acknowledged out of order, enabling parallel
/// processing.
///
/// # Operations
///
/// - [append](Self::append) / [commit](Self::commit): Write items to the journal, then persist
/// them and publish them to the reader. Appended items are lost on restart if not committed.
/// - [enqueue](Self::enqueue): Append + commit in one step. The item is durable before return.
/// - [sync](Self::sync): Commit, then prune completed sections below the reader's ack floor.
/// - [Reader::recv] / [Reader::try_recv]: Return the next unacked committed item in FIFO order.
/// - [Reader::ack] / [Reader::ack_up_to]: Mark items as processed (in-memory only).
///
/// # Acknowledgment
///
/// Acks are tracked in-memory with an `ack_floor` (all positions below are acked)
/// plus an [RMap](crate::rmap::RMap) of acked positions above the floor. When items are acked
/// contiguously from the floor, the floor advances automatically.
///
/// Acks are **not** persisted. The durable equivalent is the journal's pruning
/// boundary, advanced by [sync](Self::sync). On restart, all non-pruned
/// items are re-delivered regardless of prior ack state.
///
/// # Crash Recovery
///
/// On restart, `ack_floor` is set to the journal's pruning boundary.
/// Items that were pruned are gone. Everything else is re-delivered.
/// Applications must handle duplicates (idempotent processing).
///
/// Storage-mutating functions consume the queue and return it only on success: an error (or a
/// dropped future) destroys the handle. The reader then delivers every published item and returns
/// `None`.
pub struct Queue<E: Context, V: CodecShared> {
/// The underlying journal storing queue items.
journal: variable::Journal<E, V>,
/// Total enqueued items.
tip: Gauge,
/// Bounds of the last published snapshot.
published: Range<u64>,
/// Publishes snapshots to the reader.
snapshots: watch::Sender<Snapshot<E, V>>,
/// The reader's ack floor.
floor: Arc<AtomicU64>,
}
impl<E: Context, V: CodecShared> Queue<E, V> {
/// Initialize a queue from storage, returning it with its [Reader].
///
/// On first initialization, creates an empty queue. On restart, the reader begins from the
/// journal's pruning boundary (providing at-least-once delivery for all non-pruned items).
/// Both handles must be dropped before the queue is reopened.
///
/// # Errors
///
/// Returns an error if the underlying journal cannot be initialized.
#[boxed]
pub async fn init(context: E, cfg: Config<V::Cfg>) -> Result<(Self, Reader<E, V>), Error> {
// Initialize metrics before creating sub-contexts
let metrics = Metrics::init(&context);
// Recover the journal that backs the queue's retained items.
let journal = variable::Journal::init(
context.child("journal"),
variable::Config {
partition: cfg.partition,
items_per_section: cfg.items_per_section,
compression: cfg.compression,
codec_config: cfg.codec_config,
page_cache: cfg.page_cache,
write_buffer: cfg.write_buffer,
replay_buffer: cfg.replay_buffer,
},
)
.await?;
// On restart, the ack floor is the pruning boundary (items below are deleted).
// In-memory acknowledgements are lost on restart.
let bounds = journal.bounds();
debug!(floor = bounds.start, size = bounds.end, "queue initialized");
let _ = metrics.tip.try_set(bounds.end);
let cursor = Cursor::new(bounds.start, metrics.next, metrics.floor);
// Share the recovered view and the acknowledgement floor between the queue and its
// reader.
let (journal, snapshot) = journal.snapshot().await?;
let snapshot = Arc::new(snapshot);
let (sender, receiver) = watch::channel(snapshot.clone());
let floor = Arc::new(AtomicU64::new(bounds.start));
let queue = Self {
journal,
tip: metrics.tip,
published: bounds,
snapshots: sender,
floor: floor.clone(),
};
let reader = Reader {
snapshot,
snapshots: receiver,
cursor,
floor,
};
Ok((queue, reader))
}
/// Append and commit an item, returning its position. The reader can receive the item once
/// this returns.
///
/// # Errors
///
/// Returns an error if the underlying storage operation fails.
pub async fn enqueue(self, item: &V) -> Result<(Self, u64), Error> {
let (queue, pos) = self.append(item).await?;
let queue = queue.commit().await?;
debug!(position = pos, "enqueued item");
Ok((queue, pos))
}
/// Append a batch of items with a single commit, returning positions `[start, end)`. The
/// reader can receive the items once this returns.
///
/// # Errors
///
/// Returns an error if any append or the final commit fails.
pub async fn enqueue_bulk(
mut self,
items: impl IntoIterator<Item = impl Borrow<V>>,
) -> Result<(Self, Range<u64>), Error> {
// Append the batch before publishing it to the reader.
let start = self.journal.size();
for item in items {
(self, _) = self.append(item.borrow()).await?;
}
// Only a nonempty batch needs a commit.
let end = self.journal.size();
if end > start {
self = self.commit().await?;
}
debug!(start, end, "enqueued bulk");
Ok((self, start..end))
}
/// Append an item without committing, returning its position. The reader can receive the
/// item after the next [Self::commit] or [Self::sync].
///
/// # Errors
///
/// Returns an error if the underlying storage operation fails.
pub async fn append(mut self, item: &V) -> Result<(Self, u64), Error> {
let pos;
(self.journal, pos) = self.journal.append(item).await?;
let _ = self.tip.try_set(pos + 1);
debug!(position = pos, "appended item");
Ok((self, pos))
}
/// Durably persist appended items and publish them to the reader.
///
/// This does not persist acknowledgements. For a stronger guarantee that eliminates potential
/// recovery and prunes acknowledged items, use [Self::sync] instead.
pub async fn commit(mut self) -> Result<Self, Error> {
self.journal = self.journal.commit().await?;
self.publish().await
}
/// Durably persist the queue, guaranteeing the current state will survive a crash, and that
/// no recovery will be needed on startup.
///
/// This also prunes items below the reader's ack floor and publishes the result to the reader.
pub async fn sync(mut self) -> Result<Self, Error> {
// Make appended items durable before pruning their predecessors.
self.journal = self.journal.sync().await?;
// Sample the reader's monotonic floor. A stale value only delays pruning.
let floor = self.floor.load(Ordering::Relaxed);
(self.journal, _) = self.journal.prune(floor).await?;
// Publish a view with the retained bounds.
self.publish().await
}
/// Returns the total number of items that have been appended.
pub fn size(&self) -> u64 {
self.journal.size()
}
/// Publish a snapshot of the journal if its bounds changed since the last one.
async fn publish(mut self) -> Result<Self, Error> {
let bounds = self.journal.bounds();
if bounds == self.published {
return Ok(self);
}
// Capture the new view before publishing its bounds and waking the reader.
let snapshot;
(self.journal, snapshot) = self.journal.snapshot().await?;
self.published = bounds;
self.snapshots.send_replace(Arc::new(snapshot));
Ok(self)
}
/// Destroy the queue, removing all data from disk.
#[boxed]
pub async fn destroy(self) -> Result<(), Error> {
self.journal.destroy().await?;
Ok(())
}
}
impl<E: Context, V: CodecShared> std::fmt::Debug for Queue<E, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Queue")
.field("size", &self.size())
.finish_non_exhaustive()
}
}
/// Delivers a [Queue]'s committed items and tracks their acknowledgement.
///
/// Acknowledgements are in-memory, and [Queue::sync] prunes items below the ack floor.
pub struct Reader<E: Context, V: CodecShared> {
/// The snapshot items are delivered from.
snapshot: Snapshot<E, V>,
/// Receives snapshots from the queue.
snapshots: watch::Receiver<Snapshot<E, V>>,
/// Delivery and acknowledgement state.
cursor: Cursor,
/// The ack floor shared with the queue.
floor: Arc<AtomicU64>,
}
impl<E: Context, V: CodecShared> Reader<E, V> {
/// Receive the next unacknowledged item, waiting if necessary.
///
/// This method is designed for use with `select!`. It will:
/// 1. Return immediately if an unacked item has been published
/// 2. Wait for the queue to publish new items otherwise
/// 3. Return `None` once the queue is dropped and every published item has been delivered
///
/// # Errors
///
/// Returns an error if the underlying storage operation fails.
pub async fn recv(&mut self) -> Result<Option<(u64, V)>, Error> {
// Drain published items before waiting for the next snapshot or the queue's departure.
loop {
if let Some(item) = self.try_recv().await? {
return Ok(Some(item));
}
// `try_recv` has seen the newest snapshot, so wait for the next one. `changed` marks it
// seen, so move to it here.
if self.snapshots.changed().await.is_err() {
return Ok(None);
}
self.snapshot = self.snapshots.borrow_and_update().clone();
}
}
/// Try to dequeue the next unacknowledged item without waiting.
///
/// Returns `None` immediately if no unacked item has been published.
///
/// # Errors
///
/// Returns an error if the underlying storage operation fails.
pub async fn try_recv(&mut self) -> Result<Option<(u64, V)>, Error> {
// Move to the newest snapshot before reading, releasing blobs the queue has pruned. A
// closed channel still holds the queue's final snapshot.
if self.snapshots.has_changed().unwrap_or(true) {
self.snapshot = self.snapshots.borrow_and_update().clone();
}
self.cursor.dequeue(&*self.snapshot).await
}
/// Returns whether a specific position has been acknowledged.
pub fn is_acked(&self, position: u64) -> bool {
self.cursor.is_acked(position)
}
/// Mark the item at `position` as processed (in-memory only). The item is skipped on
/// subsequent receives. If this creates a contiguous run from the ack floor, the floor
/// advances.
///
/// # Errors
///
/// Returns [Error::PositionOutOfRange] if `position` has not been published.
pub fn ack(&mut self, position: u64) -> Result<(), Error> {
self.cursor.ack(position, self.published())?;
self.floor.store(self.cursor.ack_floor(), Ordering::Relaxed);
Ok(())
}
/// Acknowledge all items in `[ack_floor, up_to)` by advancing the floor directly. More
/// efficient than calling [Self::ack] in a loop.
///
/// # Errors
///
/// Returns [Error::PositionOutOfRange] if `up_to` exceeds the published items.
pub fn ack_up_to(&mut self, up_to: u64) -> Result<(), Error> {
self.cursor.ack_up_to(up_to, self.published())?;
self.floor.store(self.cursor.ack_floor(), Ordering::Relaxed);
Ok(())
}
/// Returns the current ack floor.
///
/// All items at positions less than this value are considered acknowledged.
pub const fn ack_floor(&self) -> u64 {
self.cursor.ack_floor()
}
/// Returns the current read position.
///
/// This is the position of the next item [Self::try_recv] will check.
pub const fn read_position(&self) -> u64 {
self.cursor.read_position()
}
/// Returns whether every published item has been acknowledged.
pub fn is_empty(&self) -> bool {
self.cursor.is_empty(self.published())
}
/// Reset the read position to the ack floor so the reader re-delivers every unacknowledged
/// item.
pub fn reset(&mut self) {
self.cursor.reset();
}
/// Returns the number of published items.
fn published(&self) -> u64 {
self.snapshots.borrow().bounds().end
}
/// Returns the number of published items not yet read (test-only).
#[cfg(test)]
fn pending(&self) -> u64 {
self.published().saturating_sub(self.cursor.read_position())
}
}
#[cfg(test)]
mod tests {
use super::*;
use commonware_codec::RangeCfg;
use commonware_macros::{select, test_traced};
use commonware_runtime::{
Metrics as _, Runner, Spawner, Supervisor as _,
buffer::paged::CacheRef,
deterministic,
mocks::{DelayedSyncContext, PendingSyncs},
};
use commonware_utils::{NZU16, NZU64, NZUsize};
use std::num::NonZeroU16;
const PAGE_SIZE: NonZeroU16 = NZU16!(1024);
const PAGE_CACHE_SIZE: NonZeroUsize = NZUsize!(10);
fn test_config(partition: &str) -> Config<(RangeCfg<usize>, ())> {
Config {
partition: partition.into(),
items_per_section: NZU64!(10),
compression: None,
codec_config: ((0..).into(), ()),
page_cache: CacheRef::new(PAGE_SIZE, PAGE_CACHE_SIZE),
write_buffer: NZUsize!(4096),
replay_buffer: NZUsize!(4096),
}
}
async fn init<E: Context>(
context: E,
cfg: Config<(RangeCfg<usize>, ())>,
) -> Result<(Queue<E, Vec<u8>>, Reader<E, Vec<u8>>), Error> {
Queue::init(context, cfg).await
}
fn acked_above_count<E: Context, V: CodecShared>(reader: &Reader<E, V>) -> usize {
reader.cursor.acked_above_count()
}
#[test_traced]
fn test_basic_enqueue_dequeue() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_basic");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Queue should be empty initially
assert!(reader.is_empty());
assert_eq!(reader.pending(), 0);
assert_eq!(queue.size(), 0);
// Enqueue items
let pos0;
(queue, pos0) = queue.enqueue(&b"item0".to_vec()).await.unwrap();
let pos1;
(queue, pos1) = queue.enqueue(&b"item1".to_vec()).await.unwrap();
let pos2;
(queue, pos2) = queue.enqueue(&b"item2".to_vec()).await.unwrap();
assert_eq!(pos0, 0);
assert_eq!(pos1, 1);
assert_eq!(pos2, 2);
assert_eq!(queue.size(), 3);
assert_eq!(reader.pending(), 3);
assert!(!reader.is_empty());
// Dequeue items
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 0);
assert_eq!(item, b"item0");
assert_eq!(reader.pending(), 2);
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 1);
assert_eq!(item, b"item1");
assert_eq!(reader.pending(), 1);
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 2);
assert_eq!(item, b"item2");
assert_eq!(reader.pending(), 0);
// Queue still has unacked items
assert!(!reader.is_empty());
assert!(reader.try_recv().await.unwrap().is_none());
});
}
#[test_traced]
fn test_append_commit_batch() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_batch");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Append multiple items, then commit once
for i in 0..5u8 {
(queue, _) = queue.append(&vec![i]).await.unwrap();
}
let mut queue = queue.commit().await.unwrap();
assert_eq!(queue.size(), 5);
// Dequeue and verify order
for i in 0..5 {
let (pos, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, i);
assert_eq!(item, vec![i as u8]);
}
// Mix batch and single enqueue
for i in 5..8u8 {
(queue, _) = queue.append(&vec![i]).await.unwrap();
}
let queue = queue.commit().await.unwrap();
let (queue, _) = queue.enqueue(&vec![8]).await.unwrap();
assert_eq!(queue.size(), 9);
reader.ack_up_to(9).unwrap();
assert!(reader.is_empty());
});
}
#[test_traced]
fn test_append_commit_persistence() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_batch_persist");
{
let (mut queue, _reader) =
Queue::<_, Vec<u8>>::init(context.child("first"), cfg.clone())
.await
.unwrap();
for i in 0..4u8 {
(queue, _) = queue.append(&vec![i]).await.unwrap();
}
let queue = queue.commit().await.unwrap();
queue.sync().await.unwrap();
}
{
let (queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("second"), cfg)
.await
.unwrap();
assert_eq!(queue.size(), 4);
for i in 0..4 {
let (pos, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, i);
assert_eq!(item, vec![i as u8]);
}
}
});
}
#[test_traced]
fn test_commit_after_sync_recovers_without_second_sync() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_commit_after_sync_recovery");
{
let (mut queue, _reader) =
Queue::<_, Vec<u8>>::init(context.child("first"), cfg.clone())
.await
.unwrap();
// Establish a synced baseline so the recovery watermark is behind the next commit.
(queue, _) = queue.append(&b"synced".to_vec()).await.unwrap();
queue = queue.commit().await.unwrap();
queue = queue.sync().await.unwrap();
// Commit later data without syncing; reopen must replay it from the old watermark.
(queue, _) = queue.append(&b"committed-a".to_vec()).await.unwrap();
(queue, _) = queue.append(&b"committed-b".to_vec()).await.unwrap();
queue.commit().await.unwrap();
}
let (queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("second"), cfg)
.await
.unwrap();
assert_eq!(queue.size(), 3);
for (expected_pos, expected_item) in [
(0, b"synced".to_vec()),
(1, b"committed-a".to_vec()),
(2, b"committed-b".to_vec()),
] {
let (pos, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, expected_pos);
assert_eq!(item, expected_item);
}
queue.destroy().await.unwrap();
});
}
#[test_traced]
fn test_sequential_ack() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_seq_ack");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..5u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Dequeue and ack sequentially
for i in 0..5 {
let (pos, _) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, i);
reader.ack(pos).unwrap();
assert_eq!(reader.ack_floor(), i + 1);
}
// All items acked
assert!(reader.is_empty());
assert_eq!(reader.ack_floor(), 5);
});
}
#[test_traced]
fn test_out_of_order_ack() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_ooo_ack");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..5u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Dequeue all
for _ in 0..5 {
reader.try_recv().await.unwrap();
}
// Ack out of order: 2, 4, 1, 3, 0
reader.ack(2).unwrap();
assert_eq!(reader.ack_floor(), 0); // Floor doesn't move
assert!(reader.is_acked(2));
reader.ack(4).unwrap();
assert_eq!(reader.ack_floor(), 0);
assert!(reader.is_acked(4));
reader.ack(1).unwrap();
assert_eq!(reader.ack_floor(), 0);
reader.ack(3).unwrap();
assert_eq!(reader.ack_floor(), 0);
// Ack 0 - floor should advance to 5 (consuming 1,2,3,4)
reader.ack(0).unwrap();
assert_eq!(reader.ack_floor(), 5);
assert!(reader.is_empty());
});
}
#[test_traced]
fn test_ack_up_to() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_ack_up_to");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Batch ack items 0-4
reader.ack_up_to(5).unwrap();
assert_eq!(reader.ack_floor(), 5);
// Items 0-4 should be acked
for i in 0..5 {
assert!(reader.is_acked(i));
}
// Items 5-9 should not be acked
for i in 5..10 {
assert!(!reader.is_acked(i));
}
// Dequeue should start at 5
let (p, _) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 5);
});
}
#[test_traced]
fn test_ack_up_to_with_existing_acks() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_ack_up_to_existing");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Ack some items out of order first
reader.ack(7).unwrap();
reader.ack(8).unwrap();
assert_eq!(acked_above_count(&reader), 2);
// Batch ack up to 5
reader.ack_up_to(5).unwrap();
assert_eq!(reader.ack_floor(), 5);
assert_eq!(acked_above_count(&reader), 2);
// Now batch ack up to 9 - should consume the acked_above entries
reader.ack_up_to(9).unwrap();
assert_eq!(reader.ack_floor(), 9);
assert_eq!(acked_above_count(&reader), 0);
});
}
#[test_traced]
fn test_ack_up_to_coalesces_with_acked_above() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_ack_up_to_coalesce");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Ack items 5, 6, 7 first
reader.ack(5).unwrap();
reader.ack(6).unwrap();
reader.ack(7).unwrap();
assert_eq!(reader.ack_floor(), 0);
// Batch ack up to 5 - should coalesce with 5, 6, 7
reader.ack_up_to(5).unwrap();
assert_eq!(reader.ack_floor(), 8); // Consumed 5, 6, 7
});
}
#[test_traced]
fn test_ack_up_to_errors() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_ack_up_to_errors");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
(queue, _) = queue.enqueue(&b"item0".to_vec()).await.unwrap();
let (_queue, _) = queue.enqueue(&b"item1".to_vec()).await.unwrap();
// Can't ack_up_to beyond queue size
let err = reader.ack_up_to(5).unwrap_err();
assert!(matches!(err, Error::PositionOutOfRange(5, 2)));
// Can ack_up_to at queue size
reader.ack_up_to(2).unwrap();
assert_eq!(reader.ack_floor(), 2);
// Acking up_to at or below floor is a no-op
reader.ack_up_to(1).unwrap();
assert_eq!(reader.ack_floor(), 2);
});
}
#[test_traced]
fn test_dequeue_skips_acked() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_skip_acked");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items 0-4
for i in 0..5u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Ack items 1 and 3 before reading
reader.ack(1).unwrap();
reader.ack(3).unwrap();
// Dequeue should skip 1 and 3
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 0);
assert_eq!(item, vec![0]);
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 2); // Skipped 1
assert_eq!(item, vec![2]);
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 4); // Skipped 3
assert_eq!(item, vec![4]);
assert!(reader.try_recv().await.unwrap().is_none());
});
}
#[test_traced]
fn test_ack_errors() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_ack_errors");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
(queue, _) = queue.enqueue(&b"item0".to_vec()).await.unwrap();
let (_queue, _) = queue.enqueue(&b"item1".to_vec()).await.unwrap();
// Can't ack position beyond queue size
let err = reader.ack(5).unwrap_err();
assert!(matches!(err, Error::PositionOutOfRange(5, 2)));
// Can ack unread items
reader.ack(1).unwrap();
assert!(reader.is_acked(1));
// Double ack is a no-op
reader.ack(1).unwrap();
});
}
#[test_traced]
fn test_prune() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_prune");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items (more than items_per_section to test pruning)
for i in 0..25u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
let _queue = queue.sync().await.unwrap();
// Read and ack some items
for i in 0..15 {
reader.try_recv().await.unwrap();
reader.ack(i).unwrap();
}
assert_eq!(reader.ack_floor(), 15);
// Items 15+ should still be readable
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 15);
assert_eq!(item, vec![15]);
});
}
#[test_traced]
fn test_ack_across_sections() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_multi_prune");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue many items across multiple sections (items_per_section = 10)
for i in 0..50u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
let _queue = queue.sync().await.unwrap();
// First batch: ack items 0-14
for i in 0..15 {
reader.try_recv().await.unwrap();
reader.ack(i).unwrap();
}
assert_eq!(reader.ack_floor(), 15);
// Verify items 15+ still readable
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 15);
assert_eq!(item, vec![15]);
// Second batch: ack items 15-29
reader.ack(15).unwrap();
for i in 16..30 {
reader.try_recv().await.unwrap();
reader.ack(i).unwrap();
}
assert_eq!(reader.ack_floor(), 30);
// Verify items 30+ still readable
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 30);
assert_eq!(item, vec![30]);
// Third batch: ack remaining items
reader.ack(30).unwrap();
for i in 31..50 {
reader.try_recv().await.unwrap();
reader.ack(i).unwrap();
}
assert_eq!(reader.ack_floor(), 50);
// Queue should be empty now
assert!(reader.is_empty());
assert!(reader.try_recv().await.unwrap().is_none());
});
}
#[test_traced]
fn test_crash_recovery_replays_from_pruning_boundary() {
// On restart, ack_floor = pruning_boundary. Items not pruned are re-delivered.
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_recovery_replay");
// First session: enqueue items, ack some (but not enough to prune)
{
let (mut queue, mut reader) =
Queue::<_, Vec<u8>>::init(context.child("first"), cfg.clone())
.await
.unwrap();
for i in 0..5u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Ack items 0, 1, 2 - but items_per_section=10, so no pruning
reader.ack(0).unwrap();
reader.ack(1).unwrap();
reader.ack(2).unwrap();
assert_eq!(reader.ack_floor(), 3);
queue.sync().await.unwrap();
}
// Second session: all items are re-delivered (no pruning occurred)
{
let (_queue, mut reader) =
Queue::<_, Vec<u8>>::init(context.child("second"), cfg.clone())
.await
.unwrap();
// ack_floor = pruning_boundary = 0 (nothing was pruned)
assert_eq!(reader.ack_floor(), 0);
// All items re-delivered
for i in 0..5 {
let (p, _) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, i);
}
}
});
}
#[test_traced]
fn test_crash_recovery_with_pruning() {
// Items pruned before crash are not re-delivered.
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_recovery_pruned");
// First session: enqueue many items, ack enough to trigger pruning
let expected_pruning_boundary = {
let (mut queue, mut reader) =
Queue::<_, Vec<u8>>::init(context.child("first"), cfg.clone())
.await
.unwrap();
// Enqueue items across multiple sections (items_per_section = 10)
for i in 0..25u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Ack items 0-14 to advance floor past section 0
for i in 0..15 {
reader.ack(i).unwrap();
}
assert_eq!(reader.ack_floor(), 15);
// Sync triggers pruning
queue = queue.sync().await.unwrap();
// Verify pruning occurred
let pruning_boundary = queue.journal.bounds().start;
assert!(pruning_boundary > 0, "expected some pruning to occur");
pruning_boundary
};
// Second session: only non-pruned items are available
{
let (queue, mut reader) =
Queue::<_, Vec<u8>>::init(context.child("second"), cfg.clone())
.await
.unwrap();
// ack_floor = pruning_boundary (items 0-9 were pruned)
let pruning_boundary = queue.journal.bounds().start;
assert_eq!(reader.ack_floor(), pruning_boundary);
assert_eq!(pruning_boundary, expected_pruning_boundary);
// Items from pruning_boundary to 24 are re-delivered
for i in pruning_boundary..25 {
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, i);
assert_eq!(item, vec![i as u8]);
}
assert!(reader.try_recv().await.unwrap().is_none());
}
});
}
#[test_traced]
fn test_reset() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_reset");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..5u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Read some
reader.try_recv().await.unwrap();
reader.try_recv().await.unwrap();
reader.try_recv().await.unwrap();
assert_eq!(reader.read_position(), 3);
// Reset without ack - should go back to 0
reader.reset();
assert_eq!(reader.read_position(), 0);
// Verify we can re-read
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 0);
assert_eq!(item, vec![0]);
});
}
#[test_traced]
fn test_reset_with_ack() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_reset_ack");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Read and ack some
for i in 0..5 {
reader.try_recv().await.unwrap();
reader.ack(i).unwrap();
}
assert_eq!(reader.ack_floor(), 5);
assert_eq!(reader.read_position(), 5);
// Read a few more
reader.try_recv().await.unwrap();
reader.try_recv().await.unwrap();
assert_eq!(reader.read_position(), 7);
// Reset - should go back to ack floor
reader.reset();
assert_eq!(reader.read_position(), 5);
// Next dequeue should return item 5
let (p, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(p, 5);
assert_eq!(item, vec![5]);
});
}
#[test_traced]
fn test_empty_queue_operations() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_empty");
let (queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Operations on empty queue
assert!(reader.is_empty());
assert!(reader.try_recv().await.unwrap().is_none());
let _queue = queue.sync().await.unwrap();
reader.reset();
});
}
#[test_traced]
fn test_persistence() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_persist");
// First session
{
let (mut queue, _reader) =
Queue::<_, Vec<u8>>::init(context.child("first"), cfg.clone())
.await
.unwrap();
(queue, _) = queue.enqueue(&b"item0".to_vec()).await.unwrap();
(queue, _) = queue.enqueue(&b"item1".to_vec()).await.unwrap();
queue.sync().await.unwrap();
}
// Second session - data should persist
{
let (queue, mut reader) =
Queue::<_, Vec<u8>>::init(context.child("second"), cfg.clone())
.await
.unwrap();
assert_eq!(queue.size(), 2);
let (_, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(item, b"item0");
let (_, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(item, b"item1");
}
});
}
#[test_traced]
fn test_large_queue_with_sparse_acks() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_sparse");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue many items
for i in 0..100u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Ack every 3rd item (sparse acking)
for i in (0..100).step_by(3) {
reader.ack(i).unwrap();
}
// Dequeue should skip acked items
let mut received = Vec::new();
while let Some((pos, _)) = reader.try_recv().await.unwrap() {
received.push(pos);
}
// Should have received all items not divisible by 3
let expected: Vec<u64> = (0..100).filter(|x| x % 3 != 0).collect();
assert_eq!(received, expected);
});
}
#[test_traced]
fn test_acked_above_coalescing() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_coalesce");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
// Enqueue items
for i in 0..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Ack items 1-8 (not 0)
for i in 1..9 {
reader.ack(i).unwrap();
}
// Acked_above should have items 1-8
assert_eq!(reader.ack_floor(), 0);
assert!(acked_above_count(&reader) > 0);
// Now ack 0 - floor should advance to 9, consuming all acked_above
reader.ack(0).unwrap();
assert_eq!(reader.ack_floor(), 9);
assert_eq!(acked_above_count(&reader), 0);
});
}
#[test_traced]
fn test_ack_up_to_past_read_pos() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_ack_up_to_past_read_pos");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(context.child("storage"), cfg)
.await
.unwrap();
for i in 0..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
// Read only 3 items
for _ in 0..3 {
reader.try_recv().await.unwrap();
}
assert_eq!(reader.read_position(), 3);
// Batch ack past read position
reader.ack_up_to(7).unwrap();
assert_eq!(reader.ack_floor(), 7);
// Dequeue should skip 3-6 and return 7
let (pos, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, 7);
assert_eq!(item, vec![7]);
});
}
#[test_traced]
fn test_metrics() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test-metrics");
let ctx = context.child("test_metrics");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(ctx, cfg).await.unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_tip 0"),
"expected tip 0: {encoded}"
);
assert!(
encoded.contains("test_metrics_floor 0"),
"expected floor 0: {encoded}"
);
assert!(
encoded.contains("test_metrics_next 0"),
"expected next 0: {encoded}"
);
// Append updates tip without enqueue
(queue, _) = queue.append(&vec![0]).await.unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_tip 1"),
"expected tip 1: {encoded}"
);
let mut queue = queue.commit().await.unwrap();
// Enqueue updates tip further
for i in 1..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_tip 10"),
"expected tip 10: {encoded}"
);
// Multiple dequeues advance next
reader.try_recv().await.unwrap();
reader.try_recv().await.unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_next 2"),
"expected next 2: {encoded}"
);
// Sequential ack advances floor
reader.ack(0).unwrap();
reader.ack(1).unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_floor 2"),
"expected floor 2: {encoded}"
);
// Out-of-order ack: floor stays until gap fills
reader.ack(4).unwrap();
reader.ack(6).unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_floor 2"),
"expected floor still 2: {encoded}"
);
// Fill gap coalesces floor forward
reader.ack(2).unwrap();
reader.ack(3).unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_floor 5"),
"expected floor 5: {encoded}"
);
// ack_up_to advances floor past sparse ack at 6
reader.ack_up_to(8).unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_floor 8"),
"expected floor 8: {encoded}"
);
// Ack remaining
reader.ack(8).unwrap();
reader.ack(9).unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_floor 10"),
"expected floor 10: {encoded}"
);
// Reset brings next back to floor
reader.reset();
let encoded = context.encode();
assert!(
encoded.contains("test_metrics_next 10"),
"expected next 10: {encoded}"
);
});
}
#[test_traced]
fn test_metrics_next_updates_on_fast_forward() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test-ff");
let ctx = context.child("test_ff");
let (mut queue, mut reader) = Queue::<_, Vec<u8>>::init(ctx, cfg).await.unwrap();
// Enqueue 3 items, dequeue and ack only the first
for i in 0..3u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
let (pos, _) = reader.try_recv().await.unwrap().unwrap();
reader.ack(pos).unwrap();
let encoded = context.encode();
assert!(
encoded.contains("test_ff_next 1"),
"expected next 1: {encoded}"
);
// Ack remaining items out-of-order to advance floor to 3
reader.ack(2).unwrap();
reader.ack(1).unwrap();
assert_eq!(reader.ack_floor(), 3);
// next metric is still 1 (no dequeue yet)
let encoded = context.encode();
assert!(
encoded.contains("test_ff_next 1"),
"expected next still 1: {encoded}"
);
// Dequeue returns None but fast-forwards read_pos to ack_floor
assert!(reader.try_recv().await.unwrap().is_none());
let encoded = context.encode();
assert!(
encoded.contains("test_ff_next 3"),
"expected next 3 after fast-forward: {encoded}"
);
});
}
/// An enqueued item reaches the reader and can be acknowledged.
#[test_traced]
fn test_reader_basic() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Create independent handles for publishing, receiving, and acknowledging one item.
let cfg = test_config("test_reader_basic");
let (queue, mut reader) = init(context, cfg).await.unwrap();
// Enqueue from queue
let (_queue, pos) = queue.enqueue(&b"hello".to_vec()).await.unwrap();
assert_eq!(pos, 0);
// Receive from reader
let (recv_pos, item) = reader.recv().await.unwrap().unwrap();
assert_eq!(recv_pos, 0);
assert_eq!(item, b"hello".to_vec());
// Ack the item
reader.ack(recv_pos).unwrap();
assert!(reader.is_empty());
});
}
/// Appended items reach the reader only once committed.
#[test_traced]
fn test_reader_append_commit() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Initialize separate handles to observe the publication boundary.
let cfg = test_config("test_reader_append_commit");
let (mut queue, mut reader) = init(context, cfg).await.unwrap();
// Append several items without committing
for i in 0..5u8 {
let pos;
(queue, pos) = queue.append(&vec![i]).await.unwrap();
assert_eq!(pos, i as u64);
}
// The reader cannot see or acknowledge them before commit
assert!(reader.try_recv().await.unwrap().is_none());
assert!(matches!(
reader.ack(0),
Err(Error::PositionOutOfRange(0, 0))
));
assert!(reader.is_empty());
// Commit to publish
let _queue = queue.commit().await.unwrap();
// Every item is readable
for i in 0..5 {
let (pos, item) = reader.recv().await.unwrap().unwrap();
assert_eq!(pos, i);
assert_eq!(item, vec![i as u8]);
reader.ack(pos).unwrap();
}
assert!(reader.is_empty());
});
}
/// A bulk enqueue publishes its items, in order, with one commit.
#[test_traced]
fn test_reader_enqueue_bulk() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Initialize a reader that will observe the entire batch after one commit.
let cfg = test_config("test_reader_bulk");
let (queue, mut reader) = init(context, cfg).await.unwrap();
// Publish the batch with a single commit.
let items: Vec<_> = (0..5u8).map(|i| vec![i]).collect();
let (_queue, range) = queue.enqueue_bulk(&items).await.unwrap();
assert_eq!(range, 0..5);
// Verify FIFO delivery and acknowledge every item in the published batch.
for i in 0..5 {
let (pos, item) = reader.recv().await.unwrap().unwrap();
assert_eq!(pos, i);
assert_eq!(item, vec![i as u8]);
reader.ack(pos).unwrap();
}
assert!(reader.is_empty());
});
}
/// Items enqueued by a queue task reach a concurrent reader in order.
#[test_traced]
fn test_reader_concurrent() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Give concurrent producer and consumer tasks handles over the same journal.
let cfg = test_config("test_reader_concurrent");
let (queue, mut reader) = init(context.child("storage"), cfg).await.unwrap();
// Spawn queue task
let queue_handle = context.child("queue").spawn(|_ctx| async move {
let mut queue = queue;
for i in 0..10u8 {
(queue, _) = queue.enqueue(&vec![i]).await.unwrap();
}
queue
});
// Reader receives items as they come
let mut received = Vec::new();
for _ in 0..10 {
let (pos, item) = reader.recv().await.unwrap().unwrap();
received.push((pos, item.clone()));
reader.ack(pos).unwrap();
}
// Verify all items received in order
for (i, (pos, item)) in received.iter().enumerate() {
assert_eq!(*pos, i as u64);
assert_eq!(*item, vec![i as u8]);
}
let _ = queue_handle.await.unwrap();
});
}
/// `recv` completes inside `select!`.
#[test_traced]
fn test_reader_select() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Prepare queue handles for receiving through select.
let cfg = test_config("test_reader_select");
let (queue, mut reader) = init(context.child("storage"), cfg).await.unwrap();
// Enqueue an item
let (_queue, _) = queue.enqueue(&b"test".to_vec()).await.unwrap();
// Receive through select alongside a branch that never completes.
let result = select! {
item = reader.recv() => item,
_ = futures::future::pending::<()>() => unreachable!(),
};
let (pos, item) = result.unwrap().unwrap();
assert_eq!(pos, 0);
assert_eq!(item, b"test".to_vec());
reader.ack(pos).unwrap();
});
}
/// After the queue is dropped, the reader delivers the published items and then returns
/// `None`.
#[test_traced]
fn test_reader_queue_dropped() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Initialize both handles before testing delivery after the queue is dropped.
let cfg = test_config("test_reader_queue_dropped");
let (queue, mut reader) = init(context.child("storage"), cfg).await.unwrap();
// Enqueue items then drop queue
let (queue, _) = queue.enqueue(&b"item1".to_vec()).await.unwrap();
let (queue, _) = queue.enqueue(&b"item2".to_vec()).await.unwrap();
drop(queue);
// Reader should still get existing items
let (pos1, _) = reader.recv().await.unwrap().unwrap();
reader.ack(pos1).unwrap();
let (pos2, _) = reader.recv().await.unwrap().unwrap();
reader.ack(pos2).unwrap();
// Next recv should return None (queue dropped, queue empty)
let result = reader.recv().await.unwrap();
assert!(result.is_none());
});
}
/// `try_recv` returns `None` until an item is published, then returns the item.
#[test_traced]
fn test_reader_try_recv() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Start with an empty published view for nonblocking receive checks.
let cfg = test_config("test_reader_try_recv");
let (queue, mut reader) = init(context, cfg).await.unwrap();
// try_recv on empty queue returns None
let result = reader.try_recv().await.unwrap();
assert!(result.is_none());
// Enqueue and try_recv
let (_queue, _) = queue.enqueue(&b"item".to_vec()).await.unwrap();
let (pos, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, 0);
assert_eq!(item, b"item".to_vec());
reader.ack(pos).unwrap();
});
}
/// `try_recv` alone drains every published item after the queue is dropped.
#[test_traced]
fn test_reader_try_recv_after_queue_dropped() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Publish items and drop the queue before any read.
let cfg = test_config("test_reader_try_recv_dropped");
let (queue, mut reader) = init(context, cfg).await.unwrap();
let (queue, _) = queue.enqueue_bulk((0..3u8).map(|i| vec![i])).await.unwrap();
drop(queue);
// The closed channel still holds the final snapshot.
for i in 0..3 {
let (pos, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, i);
assert_eq!(item, vec![i as u8]);
}
assert!(reader.try_recv().await.unwrap().is_none());
});
}
/// A queue mutation dropped mid-flight destroys the queue. The reader delivers every
/// published item, then reports the end of the queue.
#[test_traced]
fn test_reader_ends_after_interrupted_mutation() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Wrap storage so a queue operation can be parked during its durability barrier.
let pending = PendingSyncs::default();
let context = DelayedSyncContext {
inner: context,
pending: pending.clone(),
};
let cfg = test_config("test_reader_interrupted");
let (queue, mut reader) = init(context.child("storage"), cfg).await.unwrap();
// Let started syncs complete so the first enqueue publishes.
pending.unblock();
let (queue, _) = queue.enqueue(&b"first".to_vec()).await.unwrap();
// Park the next enqueue's durable commit, then drop it mid-flight.
pending.arm();
let second = b"second".to_vec();
let mut enqueue = Box::pin(queue.enqueue(&second));
assert!(futures::poll!(enqueue.as_mut()).is_pending());
drop(enqueue);
// The reader delivers the published item, then returns None.
let (pos, item) = reader.recv().await.unwrap().unwrap();
assert_eq!(pos, 0);
assert_eq!(item, b"first".to_vec());
assert!(reader.recv().await.unwrap().is_none());
});
}
/// `Queue::sync` publishes appended items, like a commit.
#[test_traced]
fn test_reader_sync_publishes() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
let cfg = test_config("test_reader_sync_publishes");
let (queue, mut reader) = init(context, cfg).await.unwrap();
// An appended item stays unpublished until the sync.
let (queue, _) = queue.append(&vec![7]).await.unwrap();
assert!(reader.try_recv().await.unwrap().is_none());
let _queue = queue.sync().await.unwrap();
// The reader receives the synced item.
let (pos, item) = reader.try_recv().await.unwrap().unwrap();
assert_eq!(pos, 0);
assert_eq!(item, vec![7]);
});
}
/// `Reader::ack_up_to` shares its floor with the queue, so a sync prunes below it.
#[test_traced]
fn test_reader_ack_up_to_prunes() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Publish three sections, then acknowledge the first 15 items without reading them.
let cfg = test_config("test_reader_ack_up_to");
let (queue, mut reader) = init(context.child("first"), cfg.clone()).await.unwrap();
let (queue, _) = queue
.enqueue_bulk((0..25u8).map(|i| vec![i]))
.await
.unwrap();
reader.ack_up_to(15).unwrap();
assert_eq!(reader.ack_floor(), 15);
// Syncing prunes whole sections below the floor.
let queue = queue.sync().await.unwrap();
drop(queue);
drop(reader);
// A restart resumes delivery at the pruning boundary.
let (_queue, reader) = init(context.child("second"), cfg).await.unwrap();
assert_eq!(reader.ack_floor(), 10);
});
}
/// The queue prunes below the reader's ack floor on sync, the reader keeps receiving
/// across the prune, and a restart re-delivers from the pruning boundary.
#[test_traced]
fn test_reader_sync_prunes_below_ack_floor() {
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Populate multiple sections so acknowledgements can advance the pruning boundary.
let cfg = test_config("test_reader_sync_prunes");
let (queue, mut reader) = init(context.child("first"), cfg.clone()).await.unwrap();
let (queue, _) = queue
.enqueue_bulk((0..25u8).map(|i| vec![i]))
.await
.unwrap();
// Acknowledge the first 15 items, then sync. Sections hold 10 items, so the
// journal prunes to position 10.
for i in 0..15 {
let (pos, _) = reader.recv().await.unwrap().unwrap();
assert_eq!(pos, i);
reader.ack(pos).unwrap();
}
assert_eq!(reader.ack_floor(), 15);
let queue = queue.sync().await.unwrap();
assert_eq!(queue.size(), 25);
// The reader moves to the pruned snapshot before its next read and receives the rest.
let (pos, _) = reader.recv().await.unwrap().unwrap();
assert_eq!(pos, 15);
assert_eq!(reader.snapshot.bounds().start, 10);
for i in 16..25 {
let (pos, item) = reader.recv().await.unwrap().unwrap();
assert_eq!(pos, i);
assert_eq!(item, vec![i as u8]);
}
assert!(reader.try_recv().await.unwrap().is_none());
drop(queue);
drop(reader);
// A restart resumes delivery at the pruning boundary.
let (_queue, mut reader) = init(context.child("second"), cfg).await.unwrap();
assert_eq!(reader.ack_floor(), 10);
let (pos, item) = reader.recv().await.unwrap().unwrap();
assert_eq!(pos, 10);
assert_eq!(item, vec![10]);
});
}
}