1use crate::event::Event;
58use async_trait::async_trait;
59use std::sync::Arc;
60use thiserror::Error;
61use tokio::sync::Mutex;
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum HandlerLane {
66 Sync,
68 Async,
70}
71
72#[derive(Debug, Error)]
74pub enum EventBusError {
75 #[error("Event handler failed for event '{event_type}' (event_id: {event_id}): {message}")]
77 HandlerFailed {
78 event_type: String,
79 event_id: String,
80 message: String,
81 },
82
83 #[error("No handlers registered for event type '{event_type}'")]
85 NoHandlers { event_type: String },
86
87 #[error("Failed to subscribe handler: {message}")]
89 SubscriptionFailed { message: String },
90
91 #[error("Event bus error: {message}")]
93 Other { message: String },
94}
95
96impl EventBusError {
97 pub fn handler_failed(
99 event_type: impl Into<String>,
100 event_id: impl Into<String>,
101 message: impl Into<String>,
102 ) -> Self {
103 EventBusError::HandlerFailed {
104 event_type: event_type.into(),
105 event_id: event_id.into(),
106 message: message.into(),
107 }
108 }
109
110 pub fn no_handlers(event_type: impl Into<String>) -> Self {
112 EventBusError::NoHandlers {
113 event_type: event_type.into(),
114 }
115 }
116
117 pub fn subscription_failed(message: impl Into<String>) -> Self {
119 EventBusError::SubscriptionFailed {
120 message: message.into(),
121 }
122 }
123
124 pub fn other(message: impl Into<String>) -> Self {
126 EventBusError::Other {
127 message: message.into(),
128 }
129 }
130}
131
132pub type EventBusResult<T> = Result<T, EventBusError>;
134
135#[async_trait]
172pub trait EventHandler: Send + Sync {
173 fn handles(&self) -> Vec<String>;
182
183 async fn handle(&self, event: &Event) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
203
204 fn lane(&self) -> HandlerLane {
206 HandlerLane::Sync
207 }
208}
209
210#[async_trait]
238pub trait EventBus: Send + Sync {
239 async fn publish(&self, events: Vec<Event>) -> EventBusResult<()>;
268
269 async fn subscribe(&mut self, handler: Box<dyn EventHandler>) -> EventBusResult<()>;
292}
293
294#[derive(Clone)]
341pub struct InProcessEventBus {
342 handlers: Arc<Mutex<Vec<Box<dyn EventHandler>>>>,
343}
344
345impl InProcessEventBus {
346 pub fn new() -> Self {
356 Self {
357 handlers: Arc::new(Mutex::new(Vec::new())),
358 }
359 }
360
361 pub async fn handler_count(&self) -> usize {
377 self.handlers.lock().await.len()
378 }
379}
380
381impl Default for InProcessEventBus {
382 fn default() -> Self {
383 Self::new()
384 }
385}
386
387#[async_trait]
388impl EventBus for InProcessEventBus {
389 async fn publish(&self, events: Vec<Event>) -> EventBusResult<()> {
390 let handlers = self.handlers.lock().await;
391
392 for event in &events {
393 for handler in handlers.iter() {
395 let handled_types = handler.handles();
396
397 if handled_types.contains(&event.event_type) {
398 handler.handle(event).await.map_err(|e| {
400 EventBusError::handler_failed(
401 &event.event_type,
402 event.event_id.to_string(),
403 e.to_string(),
404 )
405 })?;
406 }
407 }
408 }
409
410 Ok(())
411 }
412
413 async fn subscribe(&mut self, handler: Box<dyn EventHandler>) -> EventBusResult<()> {
414 let mut handlers = self.handlers.lock().await;
415 handlers.push(handler);
416 Ok(())
417 }
418}
419
420#[derive(Clone)]
426pub struct TwoLaneEventBus {
427 sync: InProcessEventBus,
428 async_lane: AsyncLaneEventBus,
429}
430
431#[derive(Clone)]
432enum AsyncLaneEventBus {
433 InProcess(InProcessEventBus),
434 External(Arc<dyn EventBus>),
435}
436
437impl TwoLaneEventBus {
438 pub fn new() -> Self {
440 Self {
441 sync: InProcessEventBus::new(),
442 async_lane: AsyncLaneEventBus::InProcess(InProcessEventBus::new()),
443 }
444 }
445
446 pub fn with_async_bus(async_bus: Arc<dyn EventBus>) -> Self {
448 Self {
449 sync: InProcessEventBus::new(),
450 async_lane: AsyncLaneEventBus::External(async_bus),
451 }
452 }
453
454 pub async fn sync_handler_count(&self) -> usize {
456 self.sync.handler_count().await
457 }
458
459 pub async fn async_handler_count(&self) -> usize {
461 match &self.async_lane {
462 AsyncLaneEventBus::InProcess(async_lane) => async_lane.handler_count().await,
463 AsyncLaneEventBus::External(_) => 0,
464 }
465 }
466}
467
468impl Default for TwoLaneEventBus {
469 fn default() -> Self {
470 Self::new()
471 }
472}
473
474#[async_trait]
475impl EventBus for TwoLaneEventBus {
476 async fn publish(&self, events: Vec<Event>) -> EventBusResult<()> {
477 self.sync.publish(events.clone()).await?;
478
479 let async_lane = self.async_lane.clone();
480 match async_lane {
481 AsyncLaneEventBus::InProcess(async_lane) => {
482 let handle = tokio::spawn(async move {
483 if let Err(error) = async_lane.publish(events).await {
484 tracing::warn!(error = ?error, "async event handler failed");
485 }
486 });
487 drop(handle);
488 }
489 AsyncLaneEventBus::External(async_bus) => async_bus.publish(events).await?,
490 }
491
492 Ok(())
493 }
494
495 async fn subscribe(&mut self, handler: Box<dyn EventHandler>) -> EventBusResult<()> {
496 match handler.lane() {
497 HandlerLane::Sync => self.sync.subscribe(handler).await,
498 HandlerLane::Async => match &mut self.async_lane {
499 AsyncLaneEventBus::InProcess(async_lane) => async_lane.subscribe(handler).await,
500 AsyncLaneEventBus::External(_) => Ok(()),
501 },
502 }
503 }
504}
505
506#[cfg(test)]
507mod tests {
508 use super::*;
509 use serde_json::json;
510 use std::sync::Arc;
511 use tokio::sync::Mutex as TokioMutex;
512
513 struct CountingHandler {
515 count: Arc<TokioMutex<usize>>,
516 event_types: Vec<String>,
517 }
518
519 impl CountingHandler {
520 fn new(event_types: Vec<String>) -> Self {
521 Self {
522 count: Arc::new(TokioMutex::new(0)),
523 event_types,
524 }
525 }
526 }
527
528 struct LaneCountingHandler {
529 count: Arc<TokioMutex<usize>>,
530 event_types: Vec<String>,
531 lane: HandlerLane,
532 }
533
534 #[async_trait]
535 impl EventHandler for LaneCountingHandler {
536 fn handles(&self) -> Vec<String> {
537 self.event_types.clone()
538 }
539
540 async fn handle(
541 &self,
542 _event: &Event,
543 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
544 let mut count = self.count.lock().await;
545 *count += 1;
546 Ok(())
547 }
548
549 fn lane(&self) -> HandlerLane {
550 self.lane
551 }
552 }
553
554 #[async_trait]
555 impl EventHandler for CountingHandler {
556 fn handles(&self) -> Vec<String> {
557 self.event_types.clone()
558 }
559
560 async fn handle(
561 &self,
562 _event: &Event,
563 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
564 let mut count = self.count.lock().await;
565 *count += 1;
566 Ok(())
567 }
568 }
569
570 struct FailingHandler {
572 fail_on: String,
573 }
574
575 struct LaneFailingHandler {
576 fail_on: String,
577 lane: HandlerLane,
578 }
579
580 #[async_trait]
581 impl EventHandler for FailingHandler {
582 fn handles(&self) -> Vec<String> {
583 vec![self.fail_on.clone()]
584 }
585
586 async fn handle(
587 &self,
588 _event: &Event,
589 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
590 Err("Intentional test failure".into())
591 }
592 }
593
594 #[async_trait]
595 impl EventHandler for LaneFailingHandler {
596 fn handles(&self) -> Vec<String> {
597 vec![self.fail_on.clone()]
598 }
599
600 async fn handle(
601 &self,
602 _event: &Event,
603 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
604 Err("Intentional test failure".into())
605 }
606
607 fn lane(&self) -> HandlerLane {
608 self.lane
609 }
610 }
611
612 #[tokio::test]
613 async fn test_new_event_bus() {
614 let bus = InProcessEventBus::new();
615 assert_eq!(bus.handler_count().await, 0);
616 }
617
618 #[tokio::test]
619 async fn test_subscribe_handler() {
620 let mut bus = InProcessEventBus::new();
621 let handler = Box::new(CountingHandler::new(vec!["UserCreated".to_string()]));
622
623 bus.subscribe(handler).await.unwrap();
624 assert_eq!(bus.handler_count().await, 1);
625 }
626
627 #[tokio::test]
628 async fn test_subscribe_multiple_handlers() {
629 let mut bus = InProcessEventBus::new();
630
631 bus.subscribe(Box::new(CountingHandler::new(vec![
632 "UserCreated".to_string()
633 ])))
634 .await
635 .unwrap();
636 bus.subscribe(Box::new(CountingHandler::new(vec![
637 "UserUpdated".to_string()
638 ])))
639 .await
640 .unwrap();
641
642 assert_eq!(bus.handler_count().await, 2);
643 }
644
645 #[tokio::test]
646 async fn test_publish_single_event() {
647 let mut bus = InProcessEventBus::new();
648 let counter = Arc::new(TokioMutex::new(0));
649 let counter_clone = counter.clone();
650
651 let handler = CountingHandler {
652 count: counter_clone,
653 event_types: vec!["UserCreated".to_string()],
654 };
655
656 bus.subscribe(Box::new(handler)).await.unwrap();
657
658 let event = Event::new(
659 "User",
660 "user-123",
661 1,
662 "UserCreated",
663 json!({ "name": "Alice" }),
664 );
665
666 bus.publish(vec![event]).await.unwrap();
667
668 let count = *counter.lock().await;
669 assert_eq!(count, 1);
670 }
671
672 #[tokio::test]
673 async fn test_publish_multiple_events() {
674 let mut bus = InProcessEventBus::new();
675 let counter = Arc::new(TokioMutex::new(0));
676 let counter_clone = counter.clone();
677
678 let handler = CountingHandler {
679 count: counter_clone,
680 event_types: vec!["UserCreated".to_string(), "UserUpdated".to_string()],
681 };
682
683 bus.subscribe(Box::new(handler)).await.unwrap();
684
685 let events = vec![
686 Event::new(
687 "User",
688 "user-1",
689 1,
690 "UserCreated",
691 json!({ "name": "Alice" }),
692 ),
693 Event::new(
694 "User",
695 "user-1",
696 2,
697 "UserUpdated",
698 json!({ "name": "Alice Smith" }),
699 ),
700 Event::new("User", "user-2", 1, "UserCreated", json!({ "name": "Bob" })),
701 ];
702
703 bus.publish(events).await.unwrap();
704
705 let count = *counter.lock().await;
706 assert_eq!(count, 3);
707 }
708
709 #[tokio::test]
710 async fn test_handler_filters_event_types() {
711 let mut bus = InProcessEventBus::new();
712 let counter = Arc::new(TokioMutex::new(0));
713 let counter_clone = counter.clone();
714
715 let handler = CountingHandler {
717 count: counter_clone,
718 event_types: vec!["UserCreated".to_string()],
719 };
720
721 bus.subscribe(Box::new(handler)).await.unwrap();
722
723 let events = vec![
724 Event::new("User", "user-1", 1, "UserCreated", json!({})),
725 Event::new("User", "user-1", 2, "UserUpdated", json!({})),
726 Event::new("User", "user-1", 3, "UserDeleted", json!({})),
727 ];
728
729 bus.publish(events).await.unwrap();
730
731 let count = *counter.lock().await;
733 assert_eq!(count, 1);
734 }
735
736 #[tokio::test]
737 async fn test_multiple_handlers_same_event() {
738 let mut bus = InProcessEventBus::new();
739 let counter1 = Arc::new(TokioMutex::new(0));
740 let counter2 = Arc::new(TokioMutex::new(0));
741
742 let handler1 = CountingHandler {
743 count: counter1.clone(),
744 event_types: vec!["UserCreated".to_string()],
745 };
746
747 let handler2 = CountingHandler {
748 count: counter2.clone(),
749 event_types: vec!["UserCreated".to_string()],
750 };
751
752 bus.subscribe(Box::new(handler1)).await.unwrap();
753 bus.subscribe(Box::new(handler2)).await.unwrap();
754
755 let event = Event::new("User", "user-1", 1, "UserCreated", json!({}));
756 bus.publish(vec![event]).await.unwrap();
757
758 assert_eq!(*counter1.lock().await, 1);
760 assert_eq!(*counter2.lock().await, 1);
761 }
762
763 #[tokio::test]
764 async fn test_handler_failure_propagates() {
765 let mut bus = InProcessEventBus::new();
766
767 let failing_handler = Box::new(FailingHandler {
768 fail_on: "UserCreated".to_string(),
769 });
770
771 bus.subscribe(failing_handler).await.unwrap();
772
773 let event = Event::new("User", "user-1", 1, "UserCreated", json!({}));
774 let result = bus.publish(vec![event]).await;
775
776 assert!(result.is_err());
777 match result.unwrap_err() {
778 EventBusError::HandlerFailed {
779 event_type,
780 event_id,
781 message,
782 } => {
783 assert_eq!(event_type, "UserCreated");
784 assert!(!event_id.is_empty());
785 assert!(message.contains("Intentional test failure"));
786 }
787 _ => panic!("Expected HandlerFailed error"),
788 }
789 }
790
791 #[tokio::test]
792 async fn test_event_handler_default_lane_is_sync() {
793 let handler = CountingHandler::new(vec!["UserCreated".to_string()]);
794
795 assert_eq!(handler.lane(), HandlerLane::Sync);
796 }
797
798 #[tokio::test]
799 async fn test_two_lane_sync_handler_failure_propagates() {
800 let mut bus = TwoLaneEventBus::new();
801
802 bus.subscribe(Box::new(LaneFailingHandler {
803 fail_on: "UserCreated".to_string(),
804 lane: HandlerLane::Sync,
805 }))
806 .await
807 .unwrap();
808
809 let event = Event::new("User", "user-1", 1, "UserCreated", json!({}));
810 let result = bus.publish(vec![event]).await;
811
812 assert!(matches!(result, Err(EventBusError::HandlerFailed { .. })));
813 }
814
815 #[tokio::test]
816 async fn test_two_lane_async_handler_failure_does_not_propagate() {
817 let mut bus = TwoLaneEventBus::new();
818
819 bus.subscribe(Box::new(LaneFailingHandler {
820 fail_on: "UserCreated".to_string(),
821 lane: HandlerLane::Async,
822 }))
823 .await
824 .unwrap();
825
826 let event = Event::new("User", "user-1", 1, "UserCreated", json!({}));
827 let result = bus.publish(vec![event]).await;
828
829 assert!(result.is_ok());
830 }
831
832 #[tokio::test]
833 async fn test_two_lane_routes_handlers_by_lane() {
834 let mut bus = TwoLaneEventBus::new();
835 let sync_count = Arc::new(TokioMutex::new(0));
836 let async_count = Arc::new(TokioMutex::new(0));
837
838 bus.subscribe(Box::new(LaneCountingHandler {
839 count: sync_count,
840 event_types: vec!["UserCreated".to_string()],
841 lane: HandlerLane::Sync,
842 }))
843 .await
844 .unwrap();
845 bus.subscribe(Box::new(LaneCountingHandler {
846 count: async_count,
847 event_types: vec!["UserCreated".to_string()],
848 lane: HandlerLane::Async,
849 }))
850 .await
851 .unwrap();
852
853 assert_eq!(bus.sync_handler_count().await, 1);
854 assert_eq!(bus.async_handler_count().await, 1);
855 }
856
857 #[tokio::test]
858 async fn test_no_handlers_for_event_type() {
859 let bus = InProcessEventBus::new();
860
861 let event = Event::new("User", "user-1", 1, "UserCreated", json!({}));
863 let result = bus.publish(vec![event]).await;
864
865 assert!(result.is_ok());
867 }
868
869 #[tokio::test]
870 async fn test_handler_called_in_order() {
871 let mut bus = InProcessEventBus::new();
872 let order = Arc::new(TokioMutex::new(Vec::new()));
873
874 struct OrderTracker {
875 id: usize,
876 order: Arc<TokioMutex<Vec<usize>>>,
877 }
878
879 #[async_trait]
880 impl EventHandler for OrderTracker {
881 fn handles(&self) -> Vec<String> {
882 vec!["TestEvent".to_string()]
883 }
884
885 async fn handle(
886 &self,
887 _event: &Event,
888 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
889 self.order.lock().await.push(self.id);
890 Ok(())
891 }
892 }
893
894 for i in 1..=3 {
896 bus.subscribe(Box::new(OrderTracker {
897 id: i,
898 order: order.clone(),
899 }))
900 .await
901 .unwrap();
902 }
903
904 let event = Event::new("Test", "test-1", 1, "TestEvent", json!({}));
905 bus.publish(vec![event]).await.unwrap();
906
907 let call_order = order.lock().await;
909 assert_eq!(*call_order, vec![1, 2, 3]);
910 }
911
912 #[tokio::test]
913 async fn test_two_lane_sync_handlers_called_in_order() {
914 let mut bus = TwoLaneEventBus::new();
915 let order = Arc::new(TokioMutex::new(Vec::new()));
916
917 struct OrderTracker {
918 id: usize,
919 order: Arc<TokioMutex<Vec<usize>>>,
920 }
921
922 #[async_trait]
923 impl EventHandler for OrderTracker {
924 fn handles(&self) -> Vec<String> {
925 vec!["TestEvent".to_string()]
926 }
927
928 async fn handle(
929 &self,
930 _event: &Event,
931 ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
932 self.order.lock().await.push(self.id);
933 Ok(())
934 }
935 }
936
937 for i in 1..=3 {
938 bus.subscribe(Box::new(OrderTracker {
939 id: i,
940 order: order.clone(),
941 }))
942 .await
943 .unwrap();
944 }
945
946 let event = Event::new("Test", "test-1", 1, "TestEvent", json!({}));
947 bus.publish(vec![event]).await.unwrap();
948
949 let call_order = order.lock().await;
950 assert_eq!(*call_order, vec![1, 2, 3]);
951 }
952
953 #[tokio::test]
954 async fn test_event_bus_clone() {
955 let mut bus1 = InProcessEventBus::new();
956 let counter = Arc::new(TokioMutex::new(0));
957
958 let handler = CountingHandler {
959 count: counter.clone(),
960 event_types: vec!["UserCreated".to_string()],
961 };
962
963 bus1.subscribe(Box::new(handler)).await.unwrap();
964
965 let bus2 = bus1.clone();
967
968 assert_eq!(bus1.handler_count().await, 1);
970 assert_eq!(bus2.handler_count().await, 1);
971
972 let event = Event::new("User", "user-1", 1, "UserCreated", json!({}));
974 bus2.publish(vec![event]).await.unwrap();
975
976 assert_eq!(*counter.lock().await, 1);
977 }
978
979 #[test]
980 fn test_error_messages() {
981 let error = EventBusError::handler_failed("UserCreated", "event-123", "Connection timeout");
982 let msg = error.to_string();
983 assert!(msg.contains("UserCreated"));
984 assert!(msg.contains("event-123"));
985 assert!(msg.contains("Connection timeout"));
986
987 let error = EventBusError::no_handlers("UnknownEvent");
988 assert!(error.to_string().contains("UnknownEvent"));
989
990 let error = EventBusError::subscription_failed("Handler invalid");
991 assert!(error.to_string().contains("Handler invalid"));
992 }
993}