Skip to main content

agent_runtime/runtime/
collecting_event_sink.rs

1//! In-memory event sink useful for tests and callers that need a run trace.
2
3use std::sync::Mutex;
4
5use super::{EventSink, RunEvent};
6
7/// An event sink that stores events in memory.
8#[derive(Default)]
9pub struct CollectingEventSink {
10    events: Mutex<Vec<RunEvent>>,
11}
12
13impl CollectingEventSink {
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    /// Returns a snapshot of all collected events.
19    pub fn events(&self) -> Vec<RunEvent> {
20        self.events
21            .lock()
22            .unwrap_or_else(|poisoned| poisoned.into_inner())
23            .clone()
24    }
25}
26
27impl EventSink for CollectingEventSink {
28    fn emit(&self, event: &RunEvent) {
29        self.events
30            .lock()
31            .unwrap_or_else(|poisoned| poisoned.into_inner())
32            .push(event.clone());
33    }
34}