Skip to main content

aa_storage_memory/
audit_sink.rs

1//! In-memory [`AuditSink`] backed by a `parking_lot::Mutex<Vec<_>>`.
2
3use std::sync::Arc;
4
5use aa_storage::{AuditEntry, AuditSink, Result};
6use async_trait::async_trait;
7use parking_lot::Mutex;
8
9/// An [`AuditSink`] that appends entries to an in-memory, unbounded buffer.
10///
11/// Intended for tests that need to assert on emitted entries. Cloning shares
12/// the same underlying buffer. The buffer is unbounded — there is no
13/// backpressure — which is acceptable for the ephemeral memory driver.
14#[derive(Clone, Default)]
15pub struct MemoryAuditSink {
16    entries: Arc<Mutex<Vec<AuditEntry>>>,
17}
18
19impl MemoryAuditSink {
20    /// Create an empty sink.
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    /// Number of entries currently buffered.
26    pub fn len(&self) -> usize {
27        self.entries.lock().len()
28    }
29
30    /// Whether the buffer holds no entries.
31    pub fn is_empty(&self) -> bool {
32        self.entries.lock().is_empty()
33    }
34
35    /// Remove and return all buffered entries in emit order.
36    pub fn drain(&self) -> Vec<AuditEntry> {
37        std::mem::take(&mut *self.entries.lock())
38    }
39}
40
41#[async_trait]
42impl AuditSink for MemoryAuditSink {
43    async fn emit(&self, event: AuditEntry) -> Result<()> {
44        self.entries.lock().push(event);
45        Ok(())
46    }
47}