use std::time::Duration;
use serde::{Deserialize, Serialize};
use ruststream_pulsar::prelude::*;
const REVIEW: Duration = Duration::from_secs(60);
#[derive(Debug, Serialize, Deserialize, PartialEq, Outgoing)]
#[outgoing(name = "payments")]
struct Payment {
amount: u64,
}
#[subscriber(PulsarSubscription::new("payments", "payments-svc"))]
async fn accept(payment: &Payment) -> HandlerOutcome {
if payment.amount == 0 {
return HandlerOutcome::drop();
}
if payment.amount > 10_000 {
tracing::info!(payment.amount, "payment held for review");
return HandlerOutcome::retry_after(REVIEW);
}
HandlerOutcome::ack()
}
#[ruststream::app]
fn app() -> impl App<State = ()> {
RustStream::new(AppInfo::new("payments", "0.1.0")).with_broker(
PulsarBroker::new("pulsar://localhost:6650"),
|b| {
b.include(accept);
},
)
}
#[cfg(test)]
mod tests {
use std::error::Error;
use ruststream::testing::TestApp;
use ruststream_pulsar::prelude::*;
use super::{Payment, REVIEW, app};
#[tokio::test]
async fn an_empty_payment_is_dropped() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<PulsarBroker>()
.message(&Payment { amount: 0 })
.publish()
.await?;
tb.broker::<PulsarBroker>()
.subscriber("payments")
.assert_called_once()
.with(&Payment { amount: 0 })
.settled(HandlerOutcome::drop());
Ok(())
}
#[tokio::test(start_paused = true)]
async fn a_held_payment_is_read_again_after_the_review_delay() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
tb.broker::<PulsarBroker>()
.message(&Payment { amount: 50_000 })
.publish()
.await?;
tb.broker::<PulsarBroker>()
.subscriber("payments")
.assert_called_once()
.settled(HandlerOutcome::retry_after(REVIEW));
tb.advance(REVIEW).await?;
tb.broker::<PulsarBroker>()
.subscriber("payments")
.assert_called(2);
Ok(())
}
}