sequenced-broadcast 0.3.0

Async structure to allow sequenced broadcasts with history
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
use std::{
    collections::VecDeque,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

use arc_metrics::{IntCounter, IntGauge};
use tokio::sync::{Notify, RwLock, broadcast};

pub struct SequencedBroadcast<T> {
    state: Arc<State<T>>,
}

/// Sends sequence-numbered messages into a [`SequencedBroadcast`].
pub struct SequencedSender<T> {
    next_seq: u64,
    state: Arc<State<T>>,
}

/// Receives sequence-numbered messages from replay history and live broadcast.
///
/// A receiver may observe the same sequence in both its catch-up replay and the
/// live broadcast channel. This type treats those repeated sequences as
/// duplicates and skips them before returning the next expected message.
pub struct SequencedReceiver<T> {
    state: Arc<State<T>>,
    next_seq: u64,
    replay: VecDeque<SequencedItem<T>>,
    live_rx: broadcast::Receiver<SequencedItem<T>>,
    terminal: Option<SequencedRecvError>,
    active: bool,
}

#[derive(Default, Debug)]
pub struct SequencedBroadcastMetrics {
    pub oldest_sequence: IntGauge,
    pub next_sequence: IntGauge,
    pub new_client_drop_count: IntCounter,
    pub new_client_accept_count: IntCounter,
    pub active_subs_gauge: IntGauge,
    pub disconnect_count: IntCounter,
    pub duplicate_skip_count: IntCounter,
    pub lagged_receiver_count: IntCounter,
}

#[derive(Debug, Clone)]
pub struct SequencedBroadcastSettings {
    pub history_capacity: usize,
    pub broadcast_capacity: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SettingsError {
    ZeroHistoryCapacity,
    ZeroBroadcastCapacity,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SubscribeError {
    SequenceTooFarAhead { seq: u64, max: u64 },
    SequenceTooFarBehind { seq: u64, min: u64 },
    Closed,
}

#[derive(Debug, PartialEq, Eq)]
pub enum SequencedSenderError<T> {
    InvalidSequence { expected: u64, got: u64, item: T },
    Closed(T),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SequencedRecvError {
    Closed,
    Lagged {
        expected: u64,
        got: u64,
        skipped: u64,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SequencedTryRecvError {
    Empty,
    Closed,
    Lagged {
        expected: u64,
        got: u64,
        skipped: u64,
    },
}

struct State<T> {
    live_tx: broadcast::Sender<SequencedItem<T>>,
    history: RwLock<History<T>>,
    closed: AtomicBool,
    close_notify: Notify,
    metrics: Arc<SequencedBroadcastMetrics>,
}

#[derive(Debug, Clone)]
struct SequencedItem<T> {
    seq: u64,
    item: T,
}

struct History<T> {
    oldest_seq: u64,
    next_seq: u64,
    entries: VecDeque<SequencedItem<T>>,
    capacity: usize,
}

impl Default for SequencedBroadcastSettings {
    fn default() -> Self {
        SequencedBroadcastSettings {
            history_capacity: 16 * 1024,
            broadcast_capacity: 16 * 1024,
        }
    }
}

impl<T> SequencedBroadcast<T>
where
    T: Send + Clone + 'static,
{
    pub fn new(
        next_seq: u64,
        settings: SequencedBroadcastSettings,
    ) -> Result<(Self, SequencedSender<T>), SettingsError> {
        if settings.history_capacity == 0 {
            return Err(SettingsError::ZeroHistoryCapacity);
        }

        if settings.broadcast_capacity == 0 {
            return Err(SettingsError::ZeroBroadcastCapacity);
        }

        let (live_tx, _) = broadcast::channel(settings.broadcast_capacity);
        let metrics = Arc::new(SequencedBroadcastMetrics {
            oldest_sequence: {
                let i = IntGauge::default();
                i.set(next_seq);
                i
            },
            next_sequence: {
                let i = IntGauge::default();
                i.set(next_seq);
                i
            },
            ..Default::default()
        });

        let state = Arc::new(State {
            live_tx,
            history: RwLock::new(History {
                oldest_seq: next_seq,
                next_seq,
                entries: VecDeque::with_capacity(settings.history_capacity),
                capacity: settings.history_capacity,
            }),
            closed: AtomicBool::new(false),
            close_notify: Notify::new(),
            metrics,
        });

        Ok((
            Self {
                state: state.clone(),
            },
            SequencedSender { next_seq, state },
        ))
    }

    pub async fn subscribe_from(
        &self,
        next_sequence: u64,
    ) -> Result<SequencedReceiver<T>, SubscribeError> {
        // Subscribe to live messages before copying history. That ordering can
        // duplicate messages across replay and live delivery, but it prevents a
        // missed sequence between the history snapshot and live subscription.
        let live_rx = self.state.live_tx.subscribe();
        let history = self.state.history.read().await;

        if next_sequence < history.oldest_seq {
            self.state.metrics.new_client_drop_count.inc();
            return Err(SubscribeError::SequenceTooFarBehind {
                seq: next_sequence,
                min: history.oldest_seq,
            });
        }

        if history.next_seq < next_sequence {
            self.state.metrics.new_client_drop_count.inc();
            return Err(SubscribeError::SequenceTooFarAhead {
                seq: next_sequence,
                max: history.next_seq,
            });
        }

        let replay = history
            .entries
            .iter()
            .filter(|entry| next_sequence <= entry.seq)
            .cloned()
            .collect();

        drop(history);

        self.state.metrics.new_client_accept_count.inc();
        self.state.metrics.active_subs_gauge.inc();

        Ok(SequencedReceiver {
            state: self.state.clone(),
            next_seq: next_sequence,
            replay,
            live_rx,
            terminal: None,
            active: true,
        })
    }

    pub fn metrics_ref(&self) -> &SequencedBroadcastMetrics {
        &self.state.metrics
    }

    pub fn metrics(&self) -> Arc<SequencedBroadcastMetrics> {
        self.state.metrics.clone()
    }

    pub fn is_closed(&self) -> bool {
        self.state.closed.load(Ordering::Acquire)
    }

    pub async fn closed(&self) {
        loop {
            let notified = self.state.close_notify.notified();
            if self.is_closed() {
                return;
            }

            notified.await;
        }
    }
}

impl<T> SequencedSender<T> {
    pub fn seq(&self) -> u64 {
        self.next_seq
    }

    pub fn is_closed(&self) -> bool {
        self.state.closed.load(Ordering::Acquire)
    }

    pub async fn closed(&self) {
        loop {
            let notified = self.state.close_notify.notified();
            if self.is_closed() {
                return;
            }

            notified.await;
        }
    }

    pub fn close(&mut self) {
        if !self.state.closed.swap(true, Ordering::AcqRel) {
            self.state.close_notify.notify_waiters();
        }
    }
}

impl<T> SequencedSender<T>
where
    T: Send + Clone + 'static,
{
    pub async fn send(&mut self, item: T) -> Result<u64, SequencedSenderError<T>> {
        self.send_at(self.next_seq, item).await
    }

    pub async fn send_at(&mut self, seq: u64, item: T) -> Result<u64, SequencedSenderError<T>> {
        if self.is_closed() {
            return Err(SequencedSenderError::Closed(item));
        }

        if seq != self.next_seq {
            return Err(SequencedSenderError::InvalidSequence {
                expected: self.next_seq,
                got: seq,
                item,
            });
        }

        let mut history = self.state.history.write().await;
        if self.is_closed() {
            return Err(SequencedSenderError::Closed(item));
        }

        /* note, do capacity check before push to ensure we don't allocate more memory */
        if history.capacity <= history.entries.len() {
            history.entries.pop_front();
            history.oldest_seq += 1;
        }

        let message = SequencedItem { seq, item };
        history.entries.push_back(message.clone());
        history.next_seq += 1;

        self.state.metrics.oldest_sequence.set(history.oldest_seq);
        self.state.metrics.next_sequence.set(history.next_seq);

        drop(history);

        let _ = self.state.live_tx.send(message);
        self.next_seq += 1;

        Ok(seq)
    }
}

impl<T> Drop for SequencedSender<T> {
    fn drop(&mut self) {
        self.close();
    }
}

impl<T> SequencedReceiver<T>
where
    T: Send + Clone + 'static,
{
    pub async fn recv(&mut self) -> Result<(u64, T), SequencedRecvError> {
        if let Some(error) = &self.terminal {
            return Err(error.clone());
        }

        loop {
            if let Some(item) = self.pop_replay()? {
                return Ok(item);
            }

            if self.state.closed.load(Ordering::Acquire) {
                match self.live_rx.try_recv() {
                    Ok(message) => match self.handle_message(message) {
                        Ok(Some(item)) => return Ok(item),
                        Ok(None) => continue,
                        Err(error) => return Err(error),
                    },
                    Err(broadcast::error::TryRecvError::Empty)
                    | Err(broadcast::error::TryRecvError::Closed) => {
                        return Err(self.terminate(SequencedRecvError::Closed));
                    }
                    Err(broadcast::error::TryRecvError::Lagged(skipped)) => {
                        return Err(self.terminate_lagged(skipped));
                    }
                }
            }

            tokio::select! {
                result = self.live_rx.recv() => {
                    match result {
                        Ok(message) => match self.handle_message(message) {
                            Ok(Some(item)) => return Ok(item),
                            Ok(None) => continue,
                            Err(error) => return Err(error),
                        },
                        Err(broadcast::error::RecvError::Closed) => {
                            return Err(self.terminate(SequencedRecvError::Closed));
                        }
                        Err(broadcast::error::RecvError::Lagged(skipped)) => {
                            return Err(self.terminate_lagged(skipped));
                        }
                    }
                }
                _ = self.state.close_notify.notified() => {
                    continue;
                }
            }
        }
    }

    pub fn try_recv(&mut self) -> Result<(u64, T), SequencedTryRecvError> {
        if let Some(error) = &self.terminal {
            return Err(error.clone().into());
        }

        loop {
            if let Some(item) = self.pop_replay()? {
                return Ok(item);
            }

            match self.live_rx.try_recv() {
                Ok(message) => match self
                    .handle_message(message)
                    .map_err(SequencedTryRecvError::from)?
                {
                    Some(item) => return Ok(item),
                    None => continue,
                },
                Err(broadcast::error::TryRecvError::Empty) => {
                    if self.state.closed.load(Ordering::Acquire) {
                        return Err(SequencedTryRecvError::from(
                            self.terminate(SequencedRecvError::Closed),
                        ));
                    }

                    return Err(SequencedTryRecvError::Empty);
                }
                Err(broadcast::error::TryRecvError::Closed) => {
                    return Err(SequencedTryRecvError::from(
                        self.terminate(SequencedRecvError::Closed),
                    ));
                }
                Err(broadcast::error::TryRecvError::Lagged(skipped)) => {
                    return Err(SequencedTryRecvError::from(self.terminate_lagged(skipped)));
                }
            }
        }
    }

    pub fn next_seq(&self) -> u64 {
        self.next_seq
    }

    pub fn is_closed(&self) -> bool {
        self.terminal.is_some() || self.state.closed.load(Ordering::Acquire)
    }

    fn pop_replay(&mut self) -> Result<Option<(u64, T)>, SequencedRecvError> {
        while let Some(message) = self.replay.pop_front() {
            match self.handle_message(message)? {
                Some(item) => return Ok(Some(item)),
                None => continue,
            }
        }

        Ok(None)
    }

    fn handle_message(
        &mut self,
        message: SequencedItem<T>,
    ) -> Result<Option<(u64, T)>, SequencedRecvError> {
        if message.seq < self.next_seq {
            self.state.metrics.duplicate_skip_count.inc();
            return Ok(None);
        }

        if self.next_seq < message.seq {
            let expected = self.next_seq;
            let skipped = message.seq - expected;
            return Err(self.terminate(SequencedRecvError::Lagged {
                expected,
                got: message.seq,
                skipped,
            }));
        }

        self.next_seq = message.seq + 1;
        Ok(Some((message.seq, message.item)))
    }

    fn terminate_lagged(&mut self, skipped: u64) -> SequencedRecvError {
        let expected = self.next_seq;
        self.terminate(SequencedRecvError::Lagged {
            expected,
            got: expected.saturating_add(skipped),
            skipped,
        })
    }

    fn terminate(&mut self, error: SequencedRecvError) -> SequencedRecvError {
        if self.terminal.is_none() {
            if self.active {
                self.active = false;
                self.state.metrics.active_subs_gauge.dec();
                self.state.metrics.disconnect_count.inc();
            }

            if matches!(error, SequencedRecvError::Lagged { .. }) {
                self.state.metrics.lagged_receiver_count.inc();
            }

            self.terminal = Some(error.clone());
        }

        error
    }
}

impl<T> Drop for SequencedReceiver<T> {
    fn drop(&mut self) {
        if self.active {
            self.active = false;
            self.state.metrics.active_subs_gauge.dec();
        }
    }
}

impl From<SequencedRecvError> for SequencedTryRecvError {
    fn from(value: SequencedRecvError) -> Self {
        match value {
            SequencedRecvError::Closed => SequencedTryRecvError::Closed,
            SequencedRecvError::Lagged {
                expected,
                got,
                skipped,
            } => SequencedTryRecvError::Lagged {
                expected,
                got,
                skipped,
            },
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use tokio::{
        task::JoinHandle,
        time::{Duration, Instant, sleep, timeout},
    };

    fn settings(history_capacity: usize, broadcast_capacity: usize) -> SequencedBroadcastSettings {
        SequencedBroadcastSettings {
            history_capacity,
            broadcast_capacity,
        }
    }

    #[tokio::test]
    async fn basic_live_delivery() {
        let (subs, mut tx) = SequencedBroadcast::new(10, SequencedBroadcastSettings::default())
            .expect("valid settings");
        let mut rx = subs.subscribe_from(10).await.unwrap();

        assert_eq!(tx.send("a").await.unwrap(), 10);
        assert_eq!(tx.send("b").await.unwrap(), 11);
        assert_eq!(tx.send("c").await.unwrap(), 12);

        assert_eq!(rx.recv().await.unwrap(), (10, "a"));
        assert_eq!(rx.recv().await.unwrap(), (11, "b"));
        assert_eq!(rx.recv().await.unwrap(), (12, "c"));
    }

    #[tokio::test]
    async fn history_catchup_delivery() {
        let (subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");

        tx.send("a").await.unwrap();
        tx.send("b").await.unwrap();

        let mut rx = subs.subscribe_from(0).await.unwrap();
        assert_eq!(rx.recv().await.unwrap(), (0, "a"));
        assert_eq!(rx.recv().await.unwrap(), (1, "b"));

        tx.send("c").await.unwrap();
        assert_eq!(rx.recv().await.unwrap(), (2, "c"));
    }

    #[tokio::test]
    async fn subscribe_from_middle_of_history() {
        let (subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");

        tx.send("a").await.unwrap();
        tx.send("b").await.unwrap();
        tx.send("c").await.unwrap();

        let mut rx = subs.subscribe_from(1).await.unwrap();
        assert_eq!(rx.recv().await.unwrap(), (1, "b"));
        assert_eq!(rx.recv().await.unwrap(), (2, "c"));
        assert_eq!(rx.try_recv(), Err(SequencedTryRecvError::Empty));
    }

    #[tokio::test]
    async fn reject_too_far_behind() {
        let (subs, mut tx) = SequencedBroadcast::new(0, settings(2, 16)).expect("valid settings");

        tx.send("a").await.unwrap();
        tx.send("b").await.unwrap();
        tx.send("c").await.unwrap();

        let error = match subs.subscribe_from(0).await {
            Ok(_) => panic!("expected subscribe error"),
            Err(error) => error,
        };
        assert_eq!(
            error,
            SubscribeError::SequenceTooFarBehind { seq: 0, min: 1 }
        );
        assert_eq!(subs.metrics_ref().new_client_drop_count.load(), 1);
    }

    #[tokio::test]
    async fn reject_too_far_ahead() {
        let (subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");

        tx.send("a").await.unwrap();
        tx.send("b").await.unwrap();
        tx.send("c").await.unwrap();

        let error = match subs.subscribe_from(4).await {
            Ok(_) => panic!("expected subscribe error"),
            Err(error) => error,
        };
        assert_eq!(
            error,
            SubscribeError::SequenceTooFarAhead { seq: 4, max: 3 }
        );
        assert_eq!(subs.metrics_ref().new_client_drop_count.load(), 1);
    }

    #[tokio::test]
    async fn duplicate_live_messages_are_ignored() {
        let (subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");
        let mut rx = subs.subscribe_from(0).await.unwrap();

        rx.replay.push_back(SequencedItem { seq: 0, item: "a" });

        tx.send("a").await.unwrap();
        tx.send("b").await.unwrap();

        assert_eq!(rx.recv().await.unwrap(), (0, "a"));
        assert_eq!(rx.recv().await.unwrap(), (1, "b"));
        assert_eq!(subs.metrics_ref().duplicate_skip_count.load(), 1);
    }

    #[tokio::test]
    async fn broadcast_lag_returns_error() {
        let (subs, mut tx) = SequencedBroadcast::new(0, settings(16, 2)).expect("valid settings");
        let mut rx = subs.subscribe_from(0).await.unwrap();

        for i in 0..8 {
            tx.send(i).await.unwrap();
        }

        assert!(matches!(
            rx.recv().await,
            Err(SequencedRecvError::Lagged { .. })
        ));
        assert_eq!(subs.metrics_ref().active_subs_gauge.load(), 0);
        assert_eq!(subs.metrics_ref().disconnect_count.load(), 1);
        assert_eq!(subs.metrics_ref().lagged_receiver_count.load(), 1);
    }

    #[tokio::test]
    async fn gap_returns_lagged_error() {
        let (subs, _tx) = SequencedBroadcast::new(5, SequencedBroadcastSettings::default())
            .expect("valid settings");
        let mut rx = subs.subscribe_from(5).await.unwrap();

        let _ = subs.state.live_tx.send(SequencedItem {
            seq: 7,
            item: "gap",
        });

        assert_eq!(
            rx.recv().await.unwrap_err(),
            SequencedRecvError::Lagged {
                expected: 5,
                got: 7,
                skipped: 2,
            }
        );
    }

    #[tokio::test]
    async fn send_succeeds_without_receivers() {
        let (subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");

        assert_eq!(tx.send("a").await.unwrap(), 0);
        assert_eq!(tx.send("b").await.unwrap(), 1);

        let mut rx = subs.subscribe_from(0).await.unwrap();
        assert_eq!(rx.recv().await.unwrap(), (0, "a"));
        assert_eq!(rx.recv().await.unwrap(), (1, "b"));
    }

    #[tokio::test]
    async fn sender_close_closes_receivers_after_replay() {
        let (subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");

        tx.send("a").await.unwrap();
        let mut rx = subs.subscribe_from(0).await.unwrap();
        tx.close();

        assert_eq!(rx.recv().await.unwrap(), (0, "a"));
        assert_eq!(rx.recv().await.unwrap_err(), SequencedRecvError::Closed);
    }

    #[tokio::test]
    async fn subscribe_after_close_can_replay_history() {
        let (subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");

        tx.send("a").await.unwrap();
        tx.send("b").await.unwrap();
        tx.close();

        let mut rx = subs.subscribe_from(0).await.unwrap();
        assert_eq!(rx.recv().await.unwrap(), (0, "a"));
        assert_eq!(rx.recv().await.unwrap(), (1, "b"));
        assert_eq!(rx.recv().await.unwrap_err(), SequencedRecvError::Closed);
    }

    #[tokio::test]
    async fn send_after_close_returns_item() {
        let (_subs, mut tx) = SequencedBroadcast::new(0, SequencedBroadcastSettings::default())
            .expect("valid settings");

        tx.close();

        assert_eq!(
            tx.send("a").await.unwrap_err(),
            SequencedSenderError::Closed("a")
        );
    }

    #[tokio::test]
    async fn send_at_validates_sequence() {
        let (_subs, mut tx) = SequencedBroadcast::new(10, SequencedBroadcastSettings::default())
            .expect("valid settings");

        assert_eq!(
            tx.send_at(11, "a").await.unwrap_err(),
            SequencedSenderError::InvalidSequence {
                expected: 10,
                got: 11,
                item: "a",
            }
        );
        assert_eq!(tx.seq(), 10);
    }

    #[tokio::test]
    async fn try_recv_empty() {
        let (subs, _tx) =
            SequencedBroadcast::<&'static str>::new(0, SequencedBroadcastSettings::default())
                .expect("valid settings");
        let mut rx = subs.subscribe_from(0).await.unwrap();

        assert_eq!(rx.try_recv(), Err(SequencedTryRecvError::Empty));
    }

    #[tokio::test]
    async fn active_metrics_decrement_on_drop() {
        let (subs, _tx) =
            SequencedBroadcast::<&'static str>::new(0, SequencedBroadcastSettings::default())
                .expect("valid settings");
        let rx_1 = subs.subscribe_from(0).await.unwrap();
        let _rx_2 = subs.subscribe_from(0).await.unwrap();

        assert_eq!(subs.metrics_ref().active_subs_gauge.load(), 2);
        drop(rx_1);
        assert_eq!(subs.metrics_ref().active_subs_gauge.load(), 1);
    }

    #[tokio::test]
    async fn closed_waits_for_sender_close() {
        let (subs, mut tx) =
            SequencedBroadcast::<&'static str>::new(0, SequencedBroadcastSettings::default())
                .expect("valid settings");

        assert!(
            timeout(Duration::from_millis(10), subs.closed())
                .await
                .is_err()
        );
        tx.close();
        timeout(Duration::from_millis(10), subs.closed())
            .await
            .expect("closed should resolve");
    }

    #[tokio::test]
    async fn settings_validation() {
        let err = match SequencedBroadcast::<()>::new(
            0,
            SequencedBroadcastSettings {
                history_capacity: 0,
                broadcast_capacity: 1,
            },
        ) {
            Ok(_) => panic!("expected settings error"),
            Err(error) => error,
        };
        assert_eq!(err, SettingsError::ZeroHistoryCapacity);

        let err = match SequencedBroadcast::<()>::new(
            0,
            SequencedBroadcastSettings {
                history_capacity: 1,
                broadcast_capacity: 0,
            },
        ) {
            Ok(_) => panic!("expected settings error"),
            Err(error) => error,
        };
        assert_eq!(err, SettingsError::ZeroBroadcastCapacity);
    }

    #[derive(Clone)]
    struct FuzzRng {
        state: u64,
    }

    impl FuzzRng {
        fn new(seed: u64) -> Self {
            Self { state: seed }
        }

        fn next(&mut self) -> u64 {
            self.state = self
                .state
                .wrapping_mul(6_364_136_223_846_793_005)
                .wrapping_add(1_442_695_040_888_963_407);
            self.state
        }

        fn range(&mut self, upper: usize) -> usize {
            if upper == 0 {
                return 0;
            }

            (self.next() as usize) % upper
        }

        fn one_in(&mut self, denominator: usize) -> bool {
            self.range(denominator) == 0
        }
    }

    async fn run_fuzz_receiver(mut rx: SequencedReceiver<u64>, mut rng: FuzzRng) -> u64 {
        let mut received = 0;

        loop {
            if rng.one_in(3) {
                let expected = rx.next_seq();
                match rx.try_recv() {
                    Ok((seq, item)) => {
                        assert_eq!(seq, expected);
                        assert_eq!(item, seq);
                        received += 1;
                    }
                    Err(SequencedTryRecvError::Empty) => {
                        sleep(Duration::from_micros((rng.range(500) + 1) as u64)).await;
                    }
                    Err(SequencedTryRecvError::Closed)
                    | Err(SequencedTryRecvError::Lagged { .. }) => break,
                }
            } else {
                let expected = rx.next_seq();
                match timeout(Duration::from_millis(50), rx.recv()).await {
                    Ok(Ok((seq, item))) => {
                        assert_eq!(seq, expected);
                        assert_eq!(item, seq);
                        received += 1;
                    }
                    Ok(Err(SequencedRecvError::Closed))
                    | Ok(Err(SequencedRecvError::Lagged { .. })) => break,
                    Err(_) => {}
                }
            }

            if received != 0 && rng.one_in(128) {
                break;
            }

            if rng.one_in(8) {
                sleep(Duration::from_micros((rng.range(1_000) + 1) as u64)).await;
            }
        }

        received
    }

    async fn join_finished(tasks: &mut Vec<JoinHandle<u64>>) -> u64 {
        let mut received = 0;

        while let Some(pos) = tasks.iter().position(JoinHandle::is_finished) {
            received += tasks
                .swap_remove(pos)
                .await
                .expect("fuzz receiver task panicked");
        }

        received
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    #[ignore = "30 second fuzzy stability test; run with `cargo test fuzzy_stability_30_seconds -- --ignored`"]
    async fn fuzzy_stability_30_seconds() {
        let deadline = Instant::now() + Duration::from_secs(30);
        let (subs, mut tx) = SequencedBroadcast::new(0, settings(512, 64)).expect("valid settings");
        let sender_deadline = deadline;

        let sender = tokio::spawn(async move {
            let mut rng = FuzzRng::new(0xa511_ce5d_f00d);
            let mut sent = 0;

            while Instant::now() < sender_deadline {
                let seq = tx.seq();
                let sent_seq = tx
                    .send(seq)
                    .await
                    .expect("send should succeed before close");
                assert_eq!(sent_seq, seq);
                sent += 1;

                if rng.one_in(4) {
                    tokio::task::yield_now().await;
                }

                if rng.one_in(16) {
                    sleep(Duration::from_micros((rng.range(250) + 1) as u64)).await;
                }
            }

            tx.close();
            sent
        });

        let mut rng = FuzzRng::new(0x5eed_5eed_cafe);
        let mut tasks: Vec<JoinHandle<u64>> = Vec::new();
        let mut total_received = 0;

        while Instant::now() < deadline {
            total_received += join_finished(&mut tasks).await;

            if tasks.len() < 128 {
                let history = subs.state.history.read().await;
                let oldest = history.oldest_seq;
                let next = history.next_seq;
                drop(history);

                let valid_span = next.saturating_sub(oldest) + 1;
                let seq = oldest + rng.range(valid_span as usize) as u64;

                match subs.subscribe_from(seq).await {
                    Ok(rx) => {
                        let seed = rng.next();
                        tasks.push(tokio::spawn(run_fuzz_receiver(rx, FuzzRng::new(seed))));
                    }
                    Err(SubscribeError::SequenceTooFarBehind { .. })
                    | Err(SubscribeError::SequenceTooFarAhead { .. }) => {}
                    Err(SubscribeError::Closed) => break,
                }
            }

            if rng.one_in(4) {
                let history = subs.state.history.read().await;
                let too_old = history.oldest_seq.saturating_sub(1);
                let too_new = history.next_seq.saturating_add(1_000);
                let oldest = history.oldest_seq;
                drop(history);

                if oldest != 0 {
                    assert!(matches!(
                        subs.subscribe_from(too_old).await,
                        Err(SubscribeError::SequenceTooFarBehind { .. })
                    ));
                }

                assert!(matches!(
                    subs.subscribe_from(too_new).await,
                    Err(SubscribeError::SequenceTooFarAhead { .. })
                ));
            }

            sleep(Duration::from_millis((rng.range(5) + 1) as u64)).await;
        }

        let sent = sender.await.expect("fuzz sender task panicked");

        for task in tasks {
            total_received += task.await.expect("fuzz receiver task panicked");
        }

        assert!(sent > 1_000);
        assert!(total_received > 0);
        assert_eq!(subs.metrics_ref().next_sequence.load(), sent);
        assert_eq!(subs.metrics_ref().active_subs_gauge.load(), 0);
    }
}