use std::time::Duration;
use ruststream::runtime::RETRY_COUNT_HEADER;
use ruststream_lapin::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing)]
#[outgoing(name = "shipments")]
struct Shipment {
id: u64,
}
const WAIT: Duration = Duration::from_secs(60);
#[subscriber(RabbitQueue::new("shipments").delay(Delay::dlx_ttl()))]
async fn dispatch(shipment: &Shipment, ctx: &mut Context<'_>) -> HandlerOutcome {
if ctx.headers().get_str(RETRY_COUNT_HEADER).is_none() {
tracing::info!(shipment.id, "warehouse closed, waiting");
return HandlerOutcome::retry_after(WAIT);
}
tracing::info!(shipment.id, "dispatched");
HandlerOutcome::ack()
}
#[ruststream::app]
fn app() -> impl App {
let broker = LapinBroker::new("amqp://localhost:5672").declare_topology(true);
RustStream::new(AppInfo::new("shipping", "0.1.0")).with_broker(broker, |b| {
b.include(dispatch);
})
}
#[cfg(test)]
mod tests {
use std::error::Error;
use ruststream::testing::TestApp;
use ruststream_lapin::prelude::*;
use super::{Shipment, WAIT, app};
#[tokio::test(start_paused = true)]
async fn the_waiting_queue_releases_the_copy() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<LapinBroker>()
.message(&Shipment { id: 2 })
.publish()
.await?;
tb.advance(WAIT).await?;
tb.broker::<LapinBroker>()
.subscriber("shipments")
.assert_called(2)
.settled(HandlerOutcome::ack());
tb.broker::<LapinBroker>()
.published::<Shipment>("shipments")
.assert_called_once();
tb.shutdown().await?;
Ok(())
}
#[tokio::test(start_paused = true)]
async fn nothing_comes_back_before_the_ttl() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<LapinBroker>()
.message(&Shipment { id: 2 })
.publish()
.await?;
tb.advance(WAIT / 2).await?;
tb.broker::<LapinBroker>()
.subscriber("shipments")
.assert_called_once()
.settled(HandlerOutcome::retry_after(WAIT));
tb.shutdown().await?;
Ok(())
}
}