use std::time::Duration;
use ruststream_amqp::prelude::*;
use serde::{Deserialize, Serialize};
#[cfg(test)]
#[path = "../../tests/live/mod.rs"]
mod live;
const INSTANT_LIMIT_CENTS: u64 = 100_000;
const HOLD: Duration = Duration::from_secs(60);
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing)]
#[outgoing(name = "card-payments")]
struct Payment {
id: u64,
card: String,
amount_cents: u64,
}
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing)]
#[outgoing(name = "card-receipts")]
struct Receipt {
payment: u64,
amount_cents: u64,
}
#[subscriber(AmqpAddress::queue("card-payments"), reply)]
async fn charge(payment: &Payment) -> Result<Receipt, HandlerOutcome> {
if payment.card.is_empty() {
tracing::warn!(payment.id, "payment without a card dropped");
return Err(HandlerOutcome::drop());
}
if payment.amount_cents > INSTANT_LIMIT_CENTS {
tracing::info!(payment.id, "payment held by the bank");
return Err(HandlerOutcome::retry_after(HOLD));
}
tracing::info!(payment.id, "payment charged");
Ok(Receipt {
payment: payment.id,
amount_cents: payment.amount_cents,
})
}
fn service(broker: AmqpBroker) -> RustStream {
RustStream::new(AppInfo::new("payments", "0.1.0")).with_broker(broker, |b| {
b.include(charge)
.max_attempts(nonzero!(3u32))
.dead_letter("card-payments.review")
.out_reply(Publish);
})
}
#[ruststream::app]
fn app() -> impl App {
service(AmqpBroker::new("amqp://artemis:artemis@localhost:5672"))
}
#[cfg(test)]
mod tests {
use std::error::Error;
use std::process;
use ruststream::runtime::RETRY_COUNT_HEADER;
use ruststream::testing::{Outcome, TestApp};
use ruststream_amqp::prelude::*;
use super::{HOLD, Payment, Receipt, live, service};
const URL: &str = "amqp://artemis:artemis@localhost:5672";
fn payment(id: u64, amount_cents: u64) -> Payment {
Payment {
id,
card: "4111".to_owned(),
amount_cents,
}
}
#[tokio::test]
async fn a_payment_gets_a_receipt() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(service(AmqpBroker::new(URL))).await?;
a_charged_payment_gets_its_receipt(tb, 1).await
}
async fn a_charged_payment_gets_its_receipt(
tb: TestApp<()>,
id: u64,
) -> Result<(), Box<dyn Error>> {
tb.broker::<AmqpBroker>()
.message(&payment(id, 2_500))
.publish()
.await?;
tb.broker::<AmqpBroker>()
.subscriber("card-payments")
.assert_called_once()
.settled(HandlerOutcome::ack());
tb.broker::<AmqpBroker>()
.published::<Receipt>("card-receipts")
.assert_called_once()
.with(&Receipt {
payment: id,
amount_cents: 2_500,
});
tb.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn a_payment_without_a_card_is_dropped() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(service(AmqpBroker::new(URL))).await?;
tb.broker::<AmqpBroker>()
.message(&Payment {
id: 2,
card: String::new(),
amount_cents: 2_500,
})
.publish()
.await?;
tb.broker::<AmqpBroker>()
.subscriber("card-payments")
.assert_called_once()
.settled(HandlerOutcome::drop());
tb.broker::<AmqpBroker>()
.published::<Receipt>("card-receipts")
.assert_not_called();
tb.shutdown().await?;
Ok(())
}
#[tokio::test(start_paused = true)]
async fn a_payment_the_bank_keeps_holding_goes_to_review() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(service(AmqpBroker::new(URL))).await?;
let held = payment(3, 250_000);
tb.broker::<AmqpBroker>().message(&held).publish().await?;
tb.broker::<AmqpBroker>()
.subscriber("card-payments")
.assert_called_once()
.settled(HandlerOutcome::retry_after(HOLD));
tb.advance(HOLD).await?;
tb.broker::<AmqpBroker>()
.published::<Payment>("card-payments")
.assert_called(2)
.with(&held)
.with_header(RETRY_COUNT_HEADER, "1");
tb.advance(HOLD).await?;
assert_eq!(
tb.broker::<AmqpBroker>()
.subscriber("card-payments")
.outcomes(),
[Outcome::Nack, Outcome::Nack, Outcome::Nack],
);
tb.broker::<AmqpBroker>()
.published::<Payment>("card-payments.review")
.assert_called_once()
.with(&held);
tb.shutdown().await?;
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_payment_gets_a_receipt_from_a_broker() -> Result<(), Box<dyn Error>> {
let Some(url) = live::url("AMQP_TEST_URL") else {
return Ok(());
};
let tb = TestApp::start_live(service(AmqpBroker::new(url))).await?;
a_charged_payment_gets_its_receipt(tb, u64::from(process::id())).await
}
}