use async_trait::async_trait;
use thiserror::Error;
use uuid::Uuid;
use super::SobreEvento;
use super::entrega::Entrega;
pub const LIMITE_PAYLOAD_DESCARTADO: usize = 4000;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum ErrorTransporte {
#[error("transporte.no_disponible")]
NoDisponible,
#[error("transporte.datos_invalidos")]
DatosInvalidos,
}
#[async_trait]
pub trait Outbox: Send + Sync {
async fn intentar_liderar(&self) -> Result<bool, ErrorTransporte>;
async fn buscar_sin_publicar(&self, limite: u32) -> Result<Vec<SobreEvento>, ErrorTransporte>;
async fn marcar_publicados(&self, ids: &[Uuid]) -> Result<(), ErrorTransporte>;
}
#[async_trait]
pub trait PublicadorEventos: Send + Sync {
async fn publicar(&self, event: &SobreEvento) -> Result<(), ErrorTransporte>;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct Reservado {
pub sobre: SobreEvento,
pub intento: u32,
}
impl Reservado {
pub fn new(sobre: SobreEvento, intento: u32) -> Self {
Self { sobre, intento }
}
}
#[async_trait]
pub trait SuscripcionEventos: Send {
fn consumidor(&self) -> &str;
fn lote_maximo(&self) -> u32 {
u32::MAX
}
async fn siguiente_lote(&mut self, max: u32) -> Result<Vec<Reservado>, ErrorTransporte>;
async fn resolver(
&mut self,
reservado: &Reservado,
entrega: &Entrega,
) -> Result<(), ErrorTransporte>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RegistroDescartado {
pub id_evento: Option<Uuid>,
pub consumidor: String,
pub tipo_evento: Option<String>,
pub intentos: u32,
pub motivo: String,
pub detalle: String,
pub payload: String,
}
impl RegistroDescartado {
pub fn para_evento(
consumidor: impl Into<String>,
event: &SobreEvento,
intentos: u32,
motivo: &str,
detalle: impl Into<String>,
) -> Self {
Self {
id_evento: Some(event.id),
consumidor: consumidor.into(),
tipo_evento: Some(event.tipo_evento.clone()),
intentos,
motivo: motivo.to_string(),
detalle: detalle.into(),
payload: trim_payload(&event.payload.to_string()),
}
}
pub fn ilegible(
consumidor: impl Into<String>,
raw: &str,
intentos: u32,
motivo: &str,
detalle: impl Into<String>,
) -> Self {
Self {
id_evento: None,
consumidor: consumidor.into(),
tipo_evento: None,
intentos,
motivo: motivo.to_string(),
detalle: detalle.into(),
payload: trim_payload(raw),
}
}
}
fn trim_payload(payload: &str) -> String {
payload.chars().take(LIMITE_PAYLOAD_DESCARTADO).collect()
}
#[async_trait]
pub trait DestinoDescartados: Send + Sync {
async fn registrar(&self, registrar: &RegistroDescartado) -> Result<(), ErrorTransporte>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::eventos::entrega::{ENTREGAS_MAXIMAS_POR_DEFECTO, decidir};
use crate::eventos::{
EventoDominio, MOTIVO_PAYLOAD_INVALIDO, ManejadorEvento, ResultadoManejo,
};
use serde_json::{Value, json};
use std::sync::Mutex;
use std::time::Duration;
struct Created(Uuid);
impl EventoDominio for Created {
fn tipo_evento(&self) -> &'static str {
"product_created"
}
fn tipo_agregado(&self) -> &'static str {
"product"
}
fn id_agregado(&self) -> Uuid {
self.0
}
fn version_esquema(&self) -> u16 {
1
}
fn payload(&self) -> Value {
json!({ "id": self.0, "name": "Desk lamp" })
}
}
fn sobre() -> SobreEvento {
SobreEvento::new(&Created(Uuid::now_v7()))
}
#[derive(Default)]
struct FakeOutbox {
rows: Mutex<Vec<(SobreEvento, bool)>>,
}
#[async_trait]
impl Outbox for FakeOutbox {
async fn intentar_liderar(&self) -> Result<bool, ErrorTransporte> {
Ok(true)
}
async fn buscar_sin_publicar(
&self,
limite: u32,
) -> Result<Vec<SobreEvento>, ErrorTransporte> {
let rows = self
.rows
.lock()
.map_err(|_| ErrorTransporte::NoDisponible)?;
Ok(rows
.iter()
.filter(|(_, published)| !published)
.take(limite as usize)
.map(|(event, _)| event.clone())
.collect())
}
async fn marcar_publicados(&self, ids: &[Uuid]) -> Result<(), ErrorTransporte> {
let mut rows = self
.rows
.lock()
.map_err(|_| ErrorTransporte::NoDisponible)?;
for (event, published) in rows.iter_mut() {
*published |= ids.contains(&event.id);
}
Ok(())
}
}
struct FakePublisher {
capacity: usize,
published: Mutex<Vec<Uuid>>,
}
#[async_trait]
impl PublicadorEventos for FakePublisher {
async fn publicar(&self, event: &SobreEvento) -> Result<(), ErrorTransporte> {
let mut published = self
.published
.lock()
.map_err(|_| ErrorTransporte::NoDisponible)?;
if published.len() == self.capacity {
return Err(ErrorTransporte::NoDisponible);
}
published.push(event.id);
Ok(())
}
}
struct FakeSubscription {
due: Vec<Reservado>,
settled: Vec<(Uuid, Entrega)>,
}
#[async_trait]
impl SuscripcionEventos for FakeSubscription {
fn consumidor(&self) -> &str {
"notify_new_product"
}
async fn siguiente_lote(&mut self, max: u32) -> Result<Vec<Reservado>, ErrorTransporte> {
let take = self.due.len().min(max as usize);
Ok(self.due.drain(..take).collect())
}
async fn resolver(
&mut self,
reservado: &Reservado,
entrega: &Entrega,
) -> Result<(), ErrorTransporte> {
self.settled.push((reservado.sobre.id, entrega.clone()));
Ok(())
}
}
#[derive(Default)]
struct FakeSink {
records: Mutex<Vec<RegistroDescartado>>,
}
#[async_trait]
impl DestinoDescartados for FakeSink {
async fn registrar(&self, registrar: &RegistroDescartado) -> Result<(), ErrorTransporte> {
let mut records = self
.records
.lock()
.map_err(|_| ErrorTransporte::NoDisponible)?;
let seen = records.iter().any(|r| {
r.id_evento == registrar.id_evento && r.consumidor == registrar.consumidor
});
if !seen {
records.push(registrar.clone());
}
Ok(())
}
}
struct NeedsName;
#[async_trait]
impl ManejadorEvento for NeedsName {
async fn manejar(&self, event: &SobreEvento) -> ResultadoManejo {
match event.payload.get("name") {
Some(_) => ResultadoManejo::Ack,
None => ResultadoManejo::descartar(MOTIVO_PAYLOAD_INVALIDO, "missing `nombre`"),
}
}
}
#[test]
fn should_hand_out_whole_batches_unless_a_transport_keeps_an_order() {
let subscription = FakeSubscription {
due: vec![],
settled: vec![],
};
assert_eq!(subscription.lote_maximo(), u32::MAX);
}
#[tokio::test]
async fn should_mark_only_the_confirmed_prefix_when_the_publisher_fails_midway() {
let eventos: Vec<SobreEvento> = (0..3).map(|_| sobre()).collect();
let outbox = FakeOutbox {
rows: Mutex::new(eventos.iter().cloned().map(|e| (e, false)).collect()),
};
let publisher = FakePublisher {
capacity: 2,
published: Mutex::new(Vec::new()),
};
assert_eq!(outbox.intentar_liderar().await, Ok(true));
let mut confirmed = Vec::new();
for event in outbox.buscar_sin_publicar(10).await.unwrap_or_default() {
if publisher.publicar(&event).await.is_err() {
break;
}
confirmed.push(event.id);
}
let marked = outbox.marcar_publicados(&confirmed).await;
assert_eq!(marked, Ok(()));
let left = outbox.buscar_sin_publicar(10).await.unwrap_or_default();
assert_eq!(left, vec![eventos[2].clone()]);
}
#[tokio::test]
async fn should_leave_marked_events_marked_when_marked_again() {
let event = sobre();
let outbox = FakeOutbox {
rows: Mutex::new(vec![(event.clone(), false)]),
};
let first = outbox.marcar_publicados(&[event.id]).await;
let second = outbox.marcar_publicados(&[event.id]).await;
assert_eq!((first, second), (Ok(()), Ok(())));
assert_eq!(outbox.buscar_sin_publicar(10).await, Ok(vec![]));
}
#[tokio::test]
async fn should_settle_every_claim_and_dead_letter_first_when_driven_by_a_consume_pass() {
let good = sobre();
let mut poisoned = sobre();
poisoned.payload = json!({ "id": poisoned.id_agregado });
let mut subscription = FakeSubscription {
due: vec![
Reservado::new(good.clone(), 1),
Reservado::new(poisoned.clone(), 2),
],
settled: Vec::new(),
};
let sink = FakeSink::default();
for reservado in subscription.siguiente_lote(10).await.unwrap_or_default() {
let outcome = NeedsName.manejar(&reservado.sobre).await;
let entrega = decidir(outcome, reservado.intento, ENTREGAS_MAXIMAS_POR_DEFECTO);
if let Entrega::Descartar { motivo, detalle } = &entrega {
let registrar = RegistroDescartado::para_evento(
subscription.consumidor(),
&reservado.sobre,
reservado.intento,
motivo,
detalle.clone(),
);
assert_eq!(sink.registrar(®istrar).await, Ok(()));
}
assert_eq!(subscription.resolver(&reservado, &entrega).await, Ok(()));
}
assert_eq!(
subscription.settled,
vec![
(good.id, Entrega::Ack),
(
poisoned.id,
Entrega::Descartar {
motivo: "payload_invalido",
detalle: "missing `nombre`".into()
}
),
]
);
let records = sink.records.lock().map(|r| r.clone()).unwrap_or_default();
assert_eq!(records.len(), 1);
assert_eq!(records[0].id_evento, Some(poisoned.id));
assert_eq!(records[0].consumidor, "notify_new_product");
assert_eq!(records[0].intentos, 2);
}
#[tokio::test]
async fn should_hand_out_at_most_the_batch_size_when_more_are_due() {
let mut subscription = FakeSubscription {
due: (0..3).map(|_| Reservado::new(sobre(), 1)).collect(),
settled: Vec::new(),
};
let first = subscription.siguiente_lote(2).await.unwrap_or_default();
let second = subscription.siguiente_lote(2).await.unwrap_or_default();
assert_eq!((first.len(), second.len()), (2, 1));
}
#[tokio::test]
async fn should_keep_one_dead_letter_when_the_same_one_is_recorded_twice() {
let sink = FakeSink::default();
let registrar =
RegistroDescartado::para_evento("audit", &sobre(), 5, "error_permanente", "no");
let _ = sink.registrar(®istrar).await;
let _ = sink.registrar(®istrar).await;
assert_eq!(sink.records.lock().map(|r| r.len()).unwrap_or(0), 1);
}
#[test]
fn should_describe_the_event_when_dead_lettering_an_envelope() {
let event = sobre();
let registrar = RegistroDescartado::para_evento(
"audit",
&event,
5,
"entregas_maximas_superadas",
"timeout",
);
assert_eq!(
registrar,
RegistroDescartado {
id_evento: Some(event.id),
consumidor: "audit".into(),
tipo_evento: Some("product_created".into()),
intentos: 5,
motivo: "entregas_maximas_superadas".into(),
detalle: "timeout".into(),
payload: event.payload.to_string(),
}
);
}
#[test]
fn should_keep_the_raw_message_when_dead_lettering_one_that_is_not_an_envelope() {
let registrar =
RegistroDescartado::ilegible("audit", "not json", 1, "payload_invalido", "eof");
assert_eq!(registrar.id_evento, None);
assert_eq!(registrar.tipo_evento, None);
assert_eq!(registrar.payload, "not json");
assert_eq!(registrar.motivo, "payload_invalido");
}
#[test]
fn should_trim_the_payload_when_it_is_longer_than_the_limit() {
let at_limit = "é".repeat(LIMITE_PAYLOAD_DESCARTADO);
let over = format!("{at_limit}tail");
let kept = RegistroDescartado::ilegible("audit", &at_limit, 1, "payload_invalido", "");
let trimmed = RegistroDescartado::ilegible("audit", &over, 1, "payload_invalido", "");
assert_eq!(kept.payload, at_limit);
assert_eq!(trimmed.payload, at_limit);
}
#[test]
fn should_speak_machine_readable_codes_when_a_transport_fails() {
assert_eq!(
ErrorTransporte::NoDisponible.to_string(),
"transporte.no_disponible"
);
assert_eq!(
ErrorTransporte::DatosInvalidos.to_string(),
"transporte.datos_invalidos"
);
}
#[test]
fn should_keep_the_retry_delay_when_a_claim_is_settled_for_later() {
let entrega = decidir(ResultadoManejo::Reintentar, 2, ENTREGAS_MAXIMAS_POR_DEFECTO);
assert_eq!(
entrega,
Entrega::Reintentar {
espera: Duration::from_secs(5)
}
);
}
}