foundation_core 0.0.4

Central crate for all foundation crates for the ewe-platform project
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
use std::{
    sync::Arc,
    time::{self, Duration, Instant},
};

use concurrent_queue::{ConcurrentQueue, ForcePushError, PopError, PushError, TryIter};
use derive_more::derive::From;

pub struct Receiver<T> {
    chan: Arc<ConcurrentQueue<T>>,
}

#[derive(Debug, Clone, PartialEq, Eq, From)]
pub enum ReceiverError {
    Timeout,
    Empty,
    Closed(PopError),
}

impl ReceiverError {
    #[must_use]
    pub fn is_timeout(&self) -> bool {
        matches!(self, ReceiverError::Timeout)
    }
}

impl core::error::Error for ReceiverError {}

impl core::fmt::Display for ReceiverError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ReceiverError::Closed(err) => write!(f, "ReceiverError::Closed({err})"),
            ReceiverError::Timeout => write!(f, "ReceiverError::Timeout"),
            ReceiverError::Empty => write!(f, "ReceiverError::Empty"),
        }
    }
}

impl<T> Clone for Receiver<T> {
    fn clone(&self) -> Self {
        Self {
            chan: self.chan.clone(),
        }
    }
}

#[allow(unused)]
impl<T> Receiver<T> {
    pub fn new(chan: Arc<ConcurrentQueue<T>>) -> Self {
        Receiver { chan }
    }

    /// Get a clone of the underlying channel Arc
    #[must_use]
    pub fn chan(&self) -> Arc<ConcurrentQueue<T>> {
        self.chan.clone()
    }

    #[must_use]
    pub fn capacity(&self) -> Option<usize> {
        self.chan.capacity()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.chan.len()
    }

    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.chan.is_closed()
    }

    #[must_use]
    pub fn close(&self) -> bool {
        self.chan.close()
    }

    #[must_use]
    pub fn is_full(&self) -> bool {
        self.chan.is_full()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.chan.is_empty()
    }

    pub fn try_iter(&self) -> TryIter<'_, T> {
        self.chan.try_iter()
    }

    pub fn into_recv_iter_timeout(&self, duration: Duration) -> RecvIterator<T> {
        RecvIterator::from_chan(self.chan.clone(), duration)
    }

    pub fn into_recv_iter(&self) -> RecvIterator<T> {
        RecvIterator::from_hundred_millis(self.chan.clone())
    }

    pub fn recv(&self) -> Result<T, ReceiverError> {
        match self.chan.pop() {
            Ok(value) => Ok(value),
            Err(err) => match err {
                PopError::Empty => Err(ReceiverError::Empty),
                PopError::Closed => Err(ReceiverError::Closed(err)),
            },
        }
    }

    /// `recv_timeout` attempts to read value from the channel within the specified duration.
    /// It internally uses [`std::thread::park_timeout`] to block the current thread
    /// for a given duration until a value is received or the timeout is reached.
    pub fn recv_timeout(&self, dur: std::time::Duration) -> Result<T, ReceiverError> {
        let started = Instant::now();

        let mut remaining_timeout = dur;
        loop {
            // if not parking then
            match self.chan.pop() {
                Ok(value) => return Ok(value),
                Err(err) => match err {
                    PopError::Empty => {
                        // check the state and see if we've crossed that threshold.
                        let elapsed = started.elapsed();
                        if elapsed >= remaining_timeout {
                            return Err(ReceiverError::Timeout);
                        }
                        remaining_timeout -= elapsed;
                    }
                    PopError::Closed => return Err(ReceiverError::Closed(err)),
                },
            }

            std::thread::park_timeout(remaining_timeout);
        }
    }
}

pub struct RecvIter<T> {
    chan: Arc<ConcurrentQueue<T>>,
}

impl<T> Clone for RecvIter<T> {
    fn clone(&self) -> Self {
        Self {
            chan: self.chan.clone(),
        }
    }
}

#[allow(unused)]
impl<T> RecvIter<T> {
    pub fn new(chan: Arc<ConcurrentQueue<T>>) -> Self {
        Self { chan }
    }

    /// [`Self::as_iter`] returns a new iterator that will block for 50 nanoseconds
    /// if the channel is empty and will yield to the OS thread scheduler if no
    /// content is received.
    #[must_use]
    pub fn as_iter(self) -> RecvIterator<T> {
        RecvIterator::ten_nano(self)
    }

    /// [`Self::block_iter`] returns a new iterator that will block for provided duration
    /// if the channel is empty and will yield to the OS thread scheduler if no
    /// content is received.
    #[must_use]
    pub fn block_iter(self, dur: time::Duration) -> RecvIterator<T> {
        RecvIterator::new(self, dur)
    }

    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.chan.is_closed()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.chan.is_empty()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.chan.len()
    }

    /// [`Self::recv`] returns a Result of the value from the underlying channel if
    /// there is a value or if empty or closed.
    pub fn recv(&self) -> Result<T, ReceiverError> {
        match self.chan.pop() {
            Ok(value) => Ok(value),
            Err(err) => match err {
                PopError::Empty => Err(ReceiverError::Empty),
                PopError::Closed => Err(ReceiverError::Closed(err)),
            },
        }
    }

    /// [`Self::block_recv`] blocks the thread with a spinning loop waiting for an item
    /// to be received on the channel.
    pub fn block_recv(&self, block_ts: time::Duration) -> Result<T, ReceiverError> {
        let mut yield_now = false;
        loop {
            // if not parking then
            let val = self.chan.pop();
            match val {
                Ok(value) => return Ok(value),
                Err(err) => match err {
                    PopError::Empty => {
                        if yield_now {
                            yield_now = false;
                            std::thread::yield_now();
                            // std::hint::spin_loop();
                            continue;
                        }

                        // noticed this was way slower than std::hint::spin_loop
                        // or std::thread::sleep.
                        std::thread::park_timeout(block_ts);
                        yield_now = true;
                    }
                    PopError::Closed => {
                        return Err(ReceiverError::Closed(err));
                    }
                },
            }
        }
    }
}

/// `RecvIterator` implements an iterator for the [`RecvIter`] type.
///
/// The [`time::Duration`] received indicates how long it will block to get
/// the next value upon which it will yield, if no value is received before
/// that period the `RecvIterator` returns `None` if there was any error or if the internal
/// receiver's channel was closed.
///
/// Which provides a sensible polling functionality yielding the threading when the timeout hits
/// and no data is received.
pub struct RecvIterator<T>(RecvIter<T>, time::Duration);

/// [`DEFAULT_BLOCK_DURATION`] is the default wait time used by the [`RecvIter`]
/// when we use a normal iterator.
const DEFAULT_BLOCK_DURATION: time::Duration = time::Duration::from_nanos(50);

impl<T> RecvIterator<T> {
    pub fn from_chan(item: Arc<ConcurrentQueue<T>>, dur: time::Duration) -> Self {
        Self::new(RecvIter::new(item), dur)
    }

    pub fn from_hundred_millis(item: Arc<ConcurrentQueue<T>>) -> Self {
        Self::new(RecvIter::new(item), Duration::from_millis(100))
    }

    pub fn from_ten_nano(item: Arc<ConcurrentQueue<T>>) -> Self {
        Self::new(RecvIter::new(item), DEFAULT_BLOCK_DURATION)
    }

    #[must_use]
    pub fn ten_nano(item: RecvIter<T>) -> Self {
        Self::new(item, DEFAULT_BLOCK_DURATION)
    }

    #[must_use]
    pub fn new(item: RecvIter<T>, dur: time::Duration) -> Self {
        Self(item, dur)
    }

    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.0.is_closed()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl<T> Iterator for RecvIterator<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.block_recv(self.1).ok()
    }
}

pub struct Sender<T> {
    chan: Arc<ConcurrentQueue<T>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SenderError<T> {
    /// Queue is full - retry with backpressure
    Full(T),
    /// Queue is closed (receiver dropped) - stop sending
    Closed(T),
}

impl<T: core::fmt::Debug> core::error::Error for SenderError<T> {}

impl<T: core::fmt::Debug> core::fmt::Display for SenderError<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            SenderError::Full(_) => write!(f, "SenderError::Full"),
            SenderError::Closed(_) => write!(f, "SenderError::Closed"),
        }
    }
}

impl<T> Clone for Sender<T> {
    fn clone(&self) -> Self {
        Sender {
            chan: self.chan.clone(),
        }
    }
}

impl<T> Sender<T> {
    pub fn new(chan: Arc<ConcurrentQueue<T>>) -> Self {
        Sender { chan }
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.chan.is_empty()
    }

    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.chan.is_closed()
    }

    #[must_use]
    pub fn close(&self) -> bool {
        self.chan.close()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.chan.len()
    }

    #[must_use]
    pub fn capacity(&self) -> Option<usize> {
        self.chan.capacity()
    }

    #[must_use]
    pub fn is_full(&self) -> bool {
        self.chan.is_full()
    }

    pub fn send(&self, value: T) -> Result<(), SenderError<T>> {
        match self.chan.push(value) {
            Ok(()) => Ok(()),
            Err(PushError::Full(v)) => Err(SenderError::Full(v)),
            Err(PushError::Closed(v)) => Err(SenderError::Closed(v)),
        }
    }

    pub fn force_send(&self, value: T) -> Result<Option<T>, SenderError<T>> {
        match self.chan.force_push(value) {
            Ok(v) => Ok(v),
            Err(ForcePushError(v)) => Err(SenderError::Closed(v)),
        }
    }
}

/// bounded creates a new bounded channel with the specified capacity.
#[must_use]
pub fn bounded<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
    let chan = Arc::new(ConcurrentQueue::bounded(capacity));
    let sender = Sender::new(chan.clone());
    let receiver = Receiver::new(chan);
    (sender, receiver)
}

/// unbounded creates a new unbounded channel.
#[must_use]
pub fn unbounded<T>() -> (Sender<T>, Receiver<T>) {
    let chan = Arc::new(ConcurrentQueue::unbounded());
    let sender = Sender::new(chan.clone());
    let receiver = Receiver::new(chan);
    (sender, receiver)
}

/// Multi-subscriber broadcaster built on top of mpp channels.
///
/// Each subscriber gets an independent `Receiver<T>`.
/// When `broadcast()` is called, the event is pushed to every subscriber's queue.
/// Dead subscriber channels (Receiver dropped) are cleaned up automatically.
pub struct Broadcaster<T: Clone + Send + 'static> {
    subscribers: Vec<Sender<T>>,
    capacity: usize,
}

impl<T: Clone + Send + 'static> Broadcaster<T> {
    /// Create a new broadcaster with the given per-subscriber channel capacity.
    pub fn new(capacity: usize) -> Self {
        Self {
            subscribers: Vec::new(),
            capacity,
        }
    }

    /// Subscribe — returns a `Receiver<T>`.
    pub fn subscribe(&mut self) -> Receiver<T> {
        let (tx, rx) = bounded(self.capacity);
        self.subscribers.push(tx);
        rx
    }

    /// Send an event to all subscribers.
    ///
    /// Dead subscriber channels (Receiver dropped) are cleaned up automatically.
    /// Uses `force_send` to drop oldest events if a subscriber's queue is full.
    pub fn broadcast(&mut self, event: T) {
        self.subscribers.retain(|tx| {
            match tx.send(event.clone()) {
                Ok(()) => true,
                Err(_) => {
                    match tx.force_send(event.clone()) {
                        Ok(_dropped) => true,
                        Err(_) => false,
                    }
                }
            }
        });
    }

    /// Clean up dead subscribers (Receiver dropped).
    pub fn cleanup(&mut self) {
        self.subscribers.retain(|tx| !tx.is_closed());
    }

    /// Number of active subscribers.
    pub fn subscriber_count(&self) -> usize {
        self.subscribers.len()
    }
}

impl<T: Clone + Send + 'static> Default for Broadcaster<T> {
    fn default() -> Self {
        Self::new(64)
    }
}

// ---------------------------------------------------------------------------
// TrackedBroadcaster — &self-safe fan-out with delivery tracking & eviction
// ---------------------------------------------------------------------------

struct TrackedSlot<T> {
    sender: Sender<T>,
    delivered_up_to: u64,
    consecutive_failures: u32,
}

struct TrackedInner<T: Clone + Send + 'static> {
    slots: Vec<TrackedSlot<T>>,
    capacity: usize,
    max_retries: u32,
    event_index: u64,
}

/// `&self`-safe multi-subscriber broadcaster with bounded per-subscriber
/// queues, delivery tracking, and eviction on max-retry failure.
///
/// Unlike [`Broadcaster`] (which requires `&mut self`), all methods here take
/// `&self` — an internal `Mutex` handles synchronization. Each subscriber gets
/// an independent bounded `Receiver<T>`. `broadcast()` fans out via try-push
/// (never blocks). Subscribers that fail to keep up are evicted after
/// `max_retries` consecutive push failures; their queue is closed so the
/// consumer sees `PopError::Closed`.
pub struct TrackedBroadcaster<T: Clone + Send + 'static> {
    inner: std::sync::Mutex<TrackedInner<T>>,
}

impl<T: Clone + Send + 'static> TrackedBroadcaster<T> {
    pub fn new(capacity: usize) -> Self {
        Self::with_max_retries(capacity, 16)
    }

    pub fn with_max_retries(capacity: usize, max_retries: u32) -> Self {
        Self {
            inner: std::sync::Mutex::new(TrackedInner {
                slots: Vec::new(),
                capacity,
                max_retries,
                event_index: 0,
            }),
        }
    }

    pub fn subscribe(&self) -> Receiver<T> {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        let cap = inner.capacity;
        let idx = inner.event_index;
        let (tx, rx) = bounded(cap);
        inner.slots.push(TrackedSlot {
            sender: tx,
            delivered_up_to: idx,
            consecutive_failures: 0,
        });
        rx
    }

    /// Fan out an event to all subscribers. Never blocks.
    ///
    /// Try-push to each subscriber's queue. On success, reset the failure
    /// counter. On failure (queue full or closed), increment
    /// `consecutive_failures`. After `max_retries` consecutive failures,
    /// evict: close the queue and remove the slot.
    pub fn broadcast(&self, event: T) {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner.event_index += 1;
        let idx = inner.event_index;
        let max = inner.max_retries;

        inner.slots.retain_mut(|slot| {
            if slot.sender.is_closed() {
                return false;
            }
            match slot.sender.send(event.clone()) {
                Ok(()) => {
                    slot.delivered_up_to = idx;
                    slot.consecutive_failures = 0;
                    true
                }
                Err(_) => {
                    slot.consecutive_failures += 1;
                    if slot.consecutive_failures >= max {
                        let _ = slot.sender.close();
                        false
                    } else {
                        true
                    }
                }
            }
        });
    }

    pub fn subscriber_count(&self) -> usize {
        let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner.slots.len()
    }

    /// Close all subscriber queues (session-end lifecycle signal).
    pub fn close(&self) {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        for slot in &inner.slots {
            let _ = slot.sender.close();
        }
        inner.slots.clear();
    }
}

impl<T: Clone + Send + 'static> Default for TrackedBroadcaster<T> {
    fn default() -> Self {
        Self::new(64)
    }
}

#[cfg(test)]
mod test_channels {
    use std::{sync::Arc, thread, time::Duration};

    use concurrent_queue::ConcurrentQueue;

    use crate::synca::mpp::RecvIterator;

    use super::{bounded, unbounded, ReceiverError};

    #[test]
    fn unbounded_channel() {
        let (sender, receiver) = unbounded();
        sender.send(42).unwrap();
        assert_eq!(receiver.recv().unwrap(), 42);
    }

    #[test]
    fn bounded_channel() {
        let (sender, receiver) = bounded(1);
        sender.send(42).unwrap();
        assert_eq!(receiver.recv().unwrap(), 42);
    }

    #[test]
    fn fail_to_receive_item_after_timeout() {
        let (sender, receiver) = bounded(1);

        let sender_clone = sender.clone();
        let join_handler = thread::spawn(move || {
            thread::sleep(Duration::from_millis(100));
            sender_clone.send(42).unwrap();
        });

        assert!(
            matches!(
                receiver.recv_timeout(Duration::from_millis(50)),
                Err(ReceiverError::Timeout)
            ),
            "should fail to receive item after timeout"
        );

        join_handler.join().expect("should finish");
    }

    #[test]
    fn can_receive_item_after_timeout() {
        let (sender, receiver) = bounded(1);

        let sender_clone = sender.clone();
        let join_handler = thread::spawn(move || {
            thread::sleep(Duration::from_millis(100));
            sender_clone.send(42).unwrap();
        });

        assert_eq!(
            receiver.recv_timeout(Duration::from_millis(102)).unwrap(),
            42
        );
        join_handler.join().expect("should finish");
    }

    #[test]
    fn can_receive_from_recv_iterator() {
        let chan: Arc<ConcurrentQueue<usize>> = Arc::new(ConcurrentQueue::bounded(10));

        chan.push(42).expect("Must receive value");
        chan.close();

        let chan_iter = RecvIterator::from_ten_nano(chan);

        let items: Vec<usize> = chan_iter.collect();
        dbg!("Received values: {:?}", &items);

        assert_eq!(items, vec![42]);
    }
}

#[cfg(test)]
mod test_tracked_broadcaster {
    use super::TrackedBroadcaster;

    #[test]
    fn fans_out_to_multiple_subscribers() {
        let b = TrackedBroadcaster::new(16);
        let r1 = b.subscribe();
        let r2 = b.subscribe();
        let r3 = b.subscribe();

        b.broadcast(42);
        b.broadcast(99);

        assert_eq!(r1.recv().unwrap(), 42);
        assert_eq!(r1.recv().unwrap(), 99);
        assert_eq!(r2.recv().unwrap(), 42);
        assert_eq!(r2.recv().unwrap(), 99);
        assert_eq!(r3.recv().unwrap(), 42);
        assert_eq!(r3.recv().unwrap(), 99);
    }

    #[test]
    fn subscriber_added_after_broadcast_misses_earlier_events() {
        let b = TrackedBroadcaster::new(16);
        let r1 = b.subscribe();
        b.broadcast(1);

        let r2 = b.subscribe();
        b.broadcast(2);

        assert_eq!(r1.recv().unwrap(), 1);
        assert_eq!(r1.recv().unwrap(), 2);
        assert_eq!(r2.recv().unwrap(), 2);
        assert!(r2.recv().is_err());
    }

    #[test]
    fn slow_subscriber_evicted_after_max_retries() {
        let b = TrackedBroadcaster::with_max_retries(2, 3);
        let slow = b.subscribe();
        let fast = b.subscribe();

        b.broadcast(1);
        b.broadcast(2);
        assert_eq!(b.subscriber_count(), 2);

        // Drain fast so it stays healthy; slow stays full
        assert_eq!(fast.recv().unwrap(), 1);
        assert_eq!(fast.recv().unwrap(), 2);

        // 3 more pushes: slow fails each time → evicted at 3
        b.broadcast(3);
        b.broadcast(4);
        b.broadcast(5);

        assert_eq!(b.subscriber_count(), 1);
        assert!(slow.is_closed());

        assert_eq!(fast.recv().unwrap(), 3);
    }

    #[test]
    fn success_resets_failure_counter() {
        let b = TrackedBroadcaster::with_max_retries(2, 3);
        let r = b.subscribe();

        b.broadcast(1);
        b.broadcast(2);
        // 2 failures (queue full, capacity 2)
        b.broadcast(3);
        b.broadcast(4);
        assert_eq!(b.subscriber_count(), 1);

        // Drain to reset
        r.recv().unwrap();
        r.recv().unwrap();

        // Next broadcast succeeds → resets counter
        b.broadcast(5);
        assert_eq!(b.subscriber_count(), 1);
        assert_eq!(r.recv().unwrap(), 5);
    }

    #[test]
    fn closed_receiver_removed_on_next_broadcast() {
        let b = TrackedBroadcaster::new(16);
        let r1 = b.subscribe();
        let r2 = b.subscribe();
        assert_eq!(b.subscriber_count(), 2);

        assert!(r2.close(), "closing an open receiver reports that it closed it");
        b.broadcast(1);

        assert_eq!(b.subscriber_count(), 1);
        assert_eq!(r1.recv().unwrap(), 1);
    }

    #[test]
    fn close_closes_all_subscribers() {
        let b = TrackedBroadcaster::<i32>::new(16);
        let r1 = b.subscribe();
        let r2 = b.subscribe();

        b.close();

        assert!(r1.is_closed());
        assert!(r2.is_closed());
        assert_eq!(b.subscriber_count(), 0);
    }

    #[test]
    fn broadcast_with_no_subscribers_is_noop() {
        let b = TrackedBroadcaster::<i32>::new(16);
        b.broadcast(42);
        assert_eq!(b.subscriber_count(), 0);
    }

    #[test]
    fn subscribe_and_broadcast_from_multiple_threads() {
        use std::sync::Arc;

        let b = Arc::new(TrackedBroadcaster::new(64));
        let r = b.subscribe();

        let handles: Vec<_> = (0..4)
            .map(|i| {
                let b = b.clone();
                std::thread::spawn(move || {
                    b.broadcast(i);
                })
            })
            .collect();

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

        let mut received = Vec::new();
        while let Ok(v) = r.recv() {
            received.push(v);
        }
        received.sort();
        assert_eq!(received, vec![0, 1, 2, 3]);
    }

    #[test]
    fn default_has_capacity_64() {
        let b = TrackedBroadcaster::<i32>::default();
        let r = b.subscribe();
        for i in 0..64 {
            b.broadcast(i);
        }
        for i in 0..64 {
            assert_eq!(r.recv().unwrap(), i);
        }
    }
}