use crate::agents::BaseAsyncAgent;
use crate::event::Event;
use crate::{MojenticError, Result};
use async_trait::async_trait;
use std::any::TypeId;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{oneshot, Mutex};
use tracing::debug;
type EventStore = Arc<Mutex<HashMap<String, Vec<Box<dyn Event>>>>>;
type WaiterStore = Arc<Mutex<HashMap<String, Vec<oneshot::Sender<Vec<Box<dyn Event>>>>>>>;
pub struct AsyncAggregatorAgent {
event_types_needed: Vec<TypeId>,
results: EventStore,
waiters: WaiterStore,
}
impl AsyncAggregatorAgent {
pub fn new(event_types_needed: Vec<TypeId>) -> Self {
Self {
event_types_needed,
results: Arc::new(Mutex::new(HashMap::new())),
waiters: Arc::new(Mutex::new(HashMap::new())),
}
}
pub async fn wait_for_events(
&self,
correlation_id: &str,
timeout: Option<Duration>,
) -> Result<Vec<Box<dyn Event>>> {
{
let results = self.results.lock().await;
if let Some(events) = results.get(correlation_id) {
if self.has_all_needed_types(events) {
debug!(
"All needed events already available for correlation_id: {}",
correlation_id
);
return Ok(events.iter().map(|e| e.clone_box()).collect());
}
}
}
let (tx, rx) = oneshot::channel();
{
let mut waiters = self.waiters.lock().await;
waiters.entry(correlation_id.to_string()).or_default().push(tx);
}
if let Some(timeout_duration) = timeout {
match tokio::time::timeout(timeout_duration, rx).await {
Ok(Ok(events)) => Ok(events),
Ok(Err(_)) => Err(MojenticError::EventError(
"Channel closed before events arrived".to_string(),
)),
Err(_) => {
debug!("Timeout waiting for events for correlation_id: {}", correlation_id);
Err(MojenticError::TimeoutError(format!(
"Timeout waiting for events for correlation_id: {}",
correlation_id
)))
}
}
} else {
rx.await.map_err(|_| {
MojenticError::EventError("Channel closed before events arrived".to_string())
})
}
}
pub async fn process_events(
&self,
_events: Vec<Box<dyn Event>>,
) -> Result<Vec<Box<dyn Event>>> {
Ok(vec![])
}
fn has_all_needed_types(&self, events: &[Box<dyn Event>]) -> bool {
let event_types: Vec<TypeId> = events.iter().map(|e| e.as_any().type_id()).collect();
self.event_types_needed
.iter()
.all(|needed_type| event_types.contains(needed_type))
}
async fn capture_event(&self, event: Box<dyn Event>) -> Result<Option<Vec<Box<dyn Event>>>> {
let correlation_id = event
.correlation_id()
.ok_or_else(|| MojenticError::EventError("Event missing correlation_id".to_string()))?
.to_string();
{
let mut results = self.results.lock().await;
results.entry(correlation_id.clone()).or_default().push(event);
}
let all_events: Option<Vec<Box<dyn Event>>> = {
let results = self.results.lock().await;
results
.get(&correlation_id)
.map(|events| events.iter().map(|e| e.clone_box()).collect())
};
if let Some(events) = all_events {
if self.has_all_needed_types(&events) {
debug!("All needed events collected for correlation_id: {}", correlation_id);
{
let mut waiters = self.waiters.lock().await;
if let Some(senders) = waiters.remove(&correlation_id) {
for sender in senders {
let events_for_waiter: Vec<Box<dyn Event>> =
events.iter().map(|e| e.clone_box()).collect();
let _ = sender.send(events_for_waiter);
}
}
}
{
let mut results = self.results.lock().await;
results.remove(&correlation_id);
}
return Ok(Some(events));
}
}
Ok(None)
}
}
#[async_trait]
impl BaseAsyncAgent for AsyncAggregatorAgent {
async fn receive_event_async(&self, event: Box<dyn Event>) -> Result<Vec<Box<dyn Event>>> {
debug!("AsyncAggregatorAgent received event");
if let Some(events) = self.capture_event(event).await? {
return self.process_events(events).await;
}
Ok(vec![])
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};
use std::any::Any;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Event1 {
source: String,
correlation_id: Option<String>,
data: String,
}
impl Event for Event1 {
fn source(&self) -> &str {
&self.source
}
fn correlation_id(&self) -> Option<&str> {
self.correlation_id.as_deref()
}
fn set_correlation_id(&mut self, id: String) {
self.correlation_id = Some(id);
}
fn as_any(&self) -> &dyn Any {
self
}
fn clone_box(&self) -> Box<dyn Event> {
Box::new(self.clone())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Event2 {
source: String,
correlation_id: Option<String>,
value: i32,
}
impl Event for Event2 {
fn source(&self) -> &str {
&self.source
}
fn correlation_id(&self) -> Option<&str> {
self.correlation_id.as_deref()
}
fn set_correlation_id(&mut self, id: String) {
self.correlation_id = Some(id);
}
fn as_any(&self) -> &dyn Any {
self
}
fn clone_box(&self) -> Box<dyn Event> {
Box::new(self.clone())
}
}
#[tokio::test(start_paused = true)]
async fn test_new_aggregator() {
let agent = AsyncAggregatorAgent::new(vec![TypeId::of::<Event1>(), TypeId::of::<Event2>()]);
assert_eq!(agent.event_types_needed.len(), 2);
}
#[tokio::test(start_paused = true)]
async fn test_single_event_does_not_trigger() {
let agent = AsyncAggregatorAgent::new(vec![TypeId::of::<Event1>(), TypeId::of::<Event2>()]);
let event1 = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: Some("test-123".to_string()),
data: "data".to_string(),
}) as Box<dyn Event>;
let result = agent.receive_event_async(event1).await.unwrap();
assert_eq!(result.len(), 0); }
#[tokio::test(start_paused = true)]
async fn test_both_events_trigger_processing() {
let agent = Arc::new(AsyncAggregatorAgent::new(vec![
TypeId::of::<Event1>(),
TypeId::of::<Event2>(),
]));
let event1 = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: Some("test-123".to_string()),
data: "data".to_string(),
}) as Box<dyn Event>;
let event2 = Box::new(Event2 {
source: "Test".to_string(),
correlation_id: Some("test-123".to_string()),
value: 42,
}) as Box<dyn Event>;
let result1 = agent.receive_event_async(event1).await.unwrap();
assert_eq!(result1.len(), 0);
let result2 = agent.receive_event_async(event2).await.unwrap();
assert_eq!(result2.len(), 0);
}
#[tokio::test(start_paused = true)]
async fn test_wait_for_events() {
let agent = Arc::new(AsyncAggregatorAgent::new(vec![
TypeId::of::<Event1>(),
TypeId::of::<Event2>(),
]));
let correlation_id = "wait-test-456";
let agent_clone = agent.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
let event1 = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: Some(correlation_id.to_string()),
data: "data".to_string(),
}) as Box<dyn Event>;
agent_clone.receive_event_async(event1).await.unwrap();
tokio::time::sleep(Duration::from_millis(100)).await;
let event2 = Box::new(Event2 {
source: "Test".to_string(),
correlation_id: Some(correlation_id.to_string()),
value: 42,
}) as Box<dyn Event>;
agent_clone.receive_event_async(event2).await.unwrap();
});
let result = agent
.wait_for_events(correlation_id, Some(Duration::from_secs(5)))
.await
.unwrap();
assert_eq!(result.len(), 2);
}
#[tokio::test(start_paused = true)]
async fn test_wait_for_events_timeout() {
let agent = AsyncAggregatorAgent::new(vec![TypeId::of::<Event1>(), TypeId::of::<Event2>()]);
let event1 = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: Some("timeout-test".to_string()),
data: "data".to_string(),
}) as Box<dyn Event>;
agent.receive_event_async(event1).await.unwrap();
let result = agent.wait_for_events("timeout-test", Some(Duration::from_millis(100))).await;
assert!(result.is_err());
match result {
Err(MojenticError::TimeoutError(_)) => {}
_ => panic!("Expected TimeoutError"),
}
}
#[tokio::test(start_paused = true)]
async fn test_different_correlation_ids() {
let agent = Arc::new(AsyncAggregatorAgent::new(vec![
TypeId::of::<Event1>(),
TypeId::of::<Event2>(),
]));
let event1_a = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: Some("corr-a".to_string()),
data: "data-a".to_string(),
}) as Box<dyn Event>;
let event1_b = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: Some("corr-b".to_string()),
data: "data-b".to_string(),
}) as Box<dyn Event>;
agent.receive_event_async(event1_a).await.unwrap();
agent.receive_event_async(event1_b).await.unwrap();
let event2_a = Box::new(Event2 {
source: "Test".to_string(),
correlation_id: Some("corr-a".to_string()),
value: 1,
}) as Box<dyn Event>;
let result = agent.receive_event_async(event2_a).await.unwrap();
assert_eq!(result.len(), 0);
let event2_b = Box::new(Event2 {
source: "Test".to_string(),
correlation_id: Some("corr-b".to_string()),
value: 2,
}) as Box<dyn Event>;
let result = agent.receive_event_async(event2_b).await.unwrap();
assert_eq!(result.len(), 0); }
#[tokio::test(start_paused = true)]
async fn test_event_without_correlation_id_fails() {
let agent = AsyncAggregatorAgent::new(vec![TypeId::of::<Event1>()]);
let event = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: None,
data: "data".to_string(),
}) as Box<dyn Event>;
let result = agent.receive_event_async(event).await;
assert!(result.is_err());
match result {
Err(MojenticError::EventError(_)) => {}
_ => panic!("Expected EventError"),
}
}
#[tokio::test(start_paused = true)]
async fn test_process_events_override() {
struct CustomAggregator {
inner: AsyncAggregatorAgent,
processed_count: Arc<Mutex<usize>>,
}
impl CustomAggregator {
fn new(event_types: Vec<TypeId>) -> Self {
Self {
inner: AsyncAggregatorAgent::new(event_types),
processed_count: Arc::new(Mutex::new(0)),
}
}
}
#[async_trait]
impl BaseAsyncAgent for CustomAggregator {
async fn receive_event_async(
&self,
event: Box<dyn Event>,
) -> Result<Vec<Box<dyn Event>>> {
if let Some(_events) = self.inner.capture_event(event).await? {
let mut count = self.processed_count.lock().await;
*count += 1;
return Ok(vec![]);
}
Ok(vec![])
}
}
let agent = CustomAggregator::new(vec![TypeId::of::<Event1>(), TypeId::of::<Event2>()]);
let count_clone = agent.processed_count.clone();
let event1 = Box::new(Event1 {
source: "Test".to_string(),
correlation_id: Some("custom-test".to_string()),
data: "data".to_string(),
}) as Box<dyn Event>;
let event2 = Box::new(Event2 {
source: "Test".to_string(),
correlation_id: Some("custom-test".to_string()),
value: 42,
}) as Box<dyn Event>;
agent.receive_event_async(event1).await.unwrap();
agent.receive_event_async(event2).await.unwrap();
let count = *count_clone.lock().await;
assert_eq!(count, 1); }
}