#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
use crate::{
rc_map::{InsertError, RcMap},
subscription::Subscription,
};
use async_broadcast::{Sender, broadcast};
use std::sync::Arc;
pub const DEFAULT_TOPIC_CAPACITY: usize = 1000;
mod rc_map;
mod subscription;
#[derive(thiserror::Error, Debug)]
pub enum PublishError {
#[error("Failed to publish message to topic, it was unexpectedly closed: '{0}'")]
ChannelClosed(Arc<str>),
#[error(
"Failed to publish message to topic, the topic is full and can't handle more messages: '{0}'"
)]
CapacityOverflow(Arc<str>),
}
#[derive(Clone)]
pub struct EventBus {
inner: RcMap<Arc<str>, Sender<Arc<[u8]>>>,
topic_capacity: usize,
}
impl Default for EventBus {
fn default() -> Self {
EventBus {
inner: RcMap::new(),
topic_capacity: DEFAULT_TOPIC_CAPACITY,
}
}
}
impl EventBus {
pub fn new() -> EventBus {
Self::default()
}
pub fn new_with_topic_capacity(topic_capacity: usize) -> EventBus {
let mut bus = EventBus::new();
bus.topic_capacity = topic_capacity;
bus
}
pub fn subscribe(&self, topic: &str) -> Subscription {
let (tx, rx) = broadcast(self.topic_capacity);
match self.inner.insert(topic.into(), tx) {
Ok(object_ref) => {
Subscription::new_with_rx(object_ref, rx)
}
Err(InsertError::AlreadyExists(_key, object_ref)) => Subscription::from(object_ref),
}
}
pub fn publish(&self, topic: &str, data: &[u8]) -> Result<(), PublishError> {
let Some(object_ref) = self.inner.get(topic.into()) else {
return Ok(());
};
let tx = object_ref.value();
let result = tx.try_broadcast(Arc::from(data));
match result {
Ok(_) => Ok(()),
Err(async_broadcast::TrySendError::Inactive(_)) => Ok(()),
Err(async_broadcast::TrySendError::Closed(_)) => {
Err(PublishError::ChannelClosed(topic.into()))
}
Err(async_broadcast::TrySendError::Full(_)) => {
Err(PublishError::CapacityOverflow(topic.into()))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures::StreamExt;
use rand::{RngCore, SeedableRng, rngs::StdRng};
#[tokio::test(flavor = "multi_thread")]
async fn test_multithreaded_pub_sub() {
let event_bus = EventBus::new();
let topic = "test_simple";
let expected_message = b"Hello EventBus";
let mut subscription = event_bus.subscribe(topic);
let task_handle = tokio::spawn(async move { subscription.next().await.unwrap() });
event_bus.publish(topic, expected_message).unwrap();
let received = task_handle
.await
.expect("Failed to receive result from task");
assert_eq!(&*received, expected_message);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_publish_to_nonexistent_topic() {
let bus = EventBus::new();
let result = bus.publish("missing_topic", b"ignored");
assert!(result.is_ok());
}
#[tokio::test(flavor = "multi_thread")]
async fn test_multiple_subscribers_receive() {
let topic = "multi_subs";
let bus = EventBus::new();
let mut s1 = bus.subscribe(topic);
let mut s2 = bus.subscribe(topic);
bus.publish(topic, b"msg").unwrap();
let r1 = s1.next().await.unwrap();
let r2 = s2.next().await.unwrap();
assert_eq!(&*r1, b"msg");
assert_eq!(&*r2, b"msg");
}
#[tokio::test(flavor = "multi_thread")]
async fn test_topic_removed_when_no_subscribers() {
let bus = EventBus::new();
let topic = "temp_topic";
{
let mut sub = bus.subscribe(topic);
bus.publish(topic, b"hello").unwrap();
let r1 = sub.next().await.unwrap();
assert_eq!(&*r1, b"hello");
}
let result = bus.publish(topic, b"nobody_listens");
assert!(result.is_ok());
}
#[tokio::test(flavor = "multi_thread")]
async fn test_capacity_overflow() {
let topic = "overflow_test";
let bus = EventBus::new_with_topic_capacity(1);
let mut sub = bus.subscribe(topic);
bus.publish(topic, b"A").unwrap();
let err = bus.publish(topic, b"B").unwrap_err();
matches!(err, PublishError::CapacityOverflow(_));
let _ = sub.next().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn test_multiple_topics_isolation() {
let bus = EventBus::new();
let mut sub_a = bus.subscribe("A");
let mut sub_b = bus.subscribe("B");
bus.publish("A", b"msgA").unwrap();
bus.publish("A", b"msgC").unwrap();
bus.publish("B", b"msgB").unwrap();
bus.publish("B", b"msgD").unwrap();
let recv_a = sub_a.next().await.unwrap();
let recv_c = sub_a.next().await.unwrap();
let recv_b = sub_b.next().await.unwrap();
let recv_d = sub_b.next().await.unwrap();
assert_eq!(&*recv_a, b"msgA");
assert_eq!(&*recv_c, b"msgC");
assert_eq!(&*recv_b, b"msgB");
assert_eq!(&*recv_d, b"msgD");
}
#[tokio::test(flavor = "multi_thread")]
async fn stress_test_concurrent_publishers() {
const TOPIC: &str = "stress_topic";
const PUBLISHERS: usize = 20;
const MSGS_PER_PUBLISHER: usize = 200;
const TOTAL_MSGS: usize = PUBLISHERS * MSGS_PER_PUBLISHER;
let bus = EventBus::new();
let mut sub = bus.subscribe(TOPIC);
let mut rng = StdRng::seed_from_u64(12345);
let messages: Vec<Arc<[u8]>> = (0..TOTAL_MSGS)
.map(|_| rng.next_u64().to_le_bytes().into())
.collect();
let handles: Vec<_> = (0..PUBLISHERS)
.map(|id| {
let start = id * MSGS_PER_PUBLISHER;
let end = start + MSGS_PER_PUBLISHER;
let bus = bus.clone();
let slice = messages[start..end].to_vec();
tokio::spawn(async move {
for msg in slice {
bus.publish(TOPIC, &msg).unwrap();
}
})
})
.collect();
let mut received = Vec::new();
for _ in 0..TOTAL_MSGS {
let msg = sub
.next()
.await
.expect("Channel closed unexpectedly during stress test");
received.push(msg.to_vec());
}
for h in handles {
h.await.unwrap();
}
let mut expected_sorted: Vec<_> = messages.clone().iter().map(|v| v.to_vec()).collect();
expected_sorted.sort();
received.sort();
assert_eq!(
received, expected_sorted,
"Message mismatch under stress load!"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn stress_test_multiple_subscribers() {
const TOPIC: &str = "stress_multi_subs";
const PUBLISHERS: usize = 10;
const SUBSCRIBERS: usize = 15;
const MSGS_PER_PUBLISHER: usize = 150;
const TOTAL: usize = PUBLISHERS * MSGS_PER_PUBLISHER;
let bus = EventBus::new();
let mut rng = StdRng::seed_from_u64(9999);
let messages: Vec<Arc<[u8]>> = (0..TOTAL)
.map(|_| rng.next_u64().to_le_bytes().into())
.collect();
let subs: Vec<Subscription> = (0..SUBSCRIBERS).map(|_| bus.subscribe(TOPIC)).collect();
let pub_handles = (0..PUBLISHERS)
.map(|id| {
let start = id * MSGS_PER_PUBLISHER;
let end = start + MSGS_PER_PUBLISHER;
let bus = bus.clone();
let slice = messages[start..end].to_vec();
tokio::spawn(async move {
for msg in slice {
bus.publish(TOPIC, &msg).unwrap();
if rand::random::<bool>() {
tokio::task::yield_now().await;
}
}
})
})
.collect::<Vec<_>>();
let sub_handles = subs
.into_iter()
.map(|mut sub| {
tokio::spawn(async move {
let mut collected = Vec::with_capacity(TOTAL);
for _ in 0..TOTAL {
let msg = sub
.next()
.await
.expect("Channel closed unexpectedly during stress test");
collected.push(msg.to_vec());
}
collected
})
})
.collect::<Vec<_>>();
for h in pub_handles {
h.await.unwrap();
}
let mut sub_results = Vec::new();
for h in sub_handles {
sub_results.push(h.await.unwrap());
}
for mut received in sub_results {
received.sort();
let mut expected: Vec<_> = messages.clone().into_iter().map(|v| v.to_vec()).collect();
expected.sort();
assert_eq!(
received, expected,
"Subscriber missed or corrupted messages",
);
}
}
}