use ruststream::OutSlot;
use ruststream::testing::TestApp;
use ruststream_lapin::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Outgoing)]
struct Payment {
amount: u64,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Outgoing)]
struct Receipt {
amount: u64,
}
#[derive(OutSlot)]
#[publishes(Receipt)]
struct Receipts;
#[subscriber(RabbitQueue::new("payments"))]
async fn accept(payment: &Payment) -> HandlerOutcome {
if payment.amount == 0 {
return HandlerOutcome::drop();
}
HandlerOutcome::ack()
}
#[subscriber(RabbitQueue::new("payments.receipted"))]
async fn receipt_for(
payment: &Payment,
Out(receipts): Out<impl Publisher<Options = LapinPublishOptions>, Receipts>,
) -> HandlerOutcome {
let receipt = Receipt {
amount: payment.amount,
};
let sent = if payment.amount > 1_000 {
receipts
.message(&receipt)
.to("receipts")
.priority(9)
.publish()
.await
} else {
receipts.message(&receipt).to("receipts").publish().await
};
if sent.is_err() {
return HandlerOutcome::retry();
}
HandlerOutcome::ack()
}
fn app() -> RustStream {
RustStream::new(AppInfo::new("payments", "0.1.0")).with_broker(
LapinBroker::new("amqp://localhost:5672"),
|b| {
b.include(accept);
b.include(receipt_for)
.out(Receipts, Publish::default().priority(3))
.build();
},
)
}
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
async fn main() {
let tb = TestApp::start(app()).await.expect("start");
tb.broker::<LapinBroker>()
.message(&Payment { amount: 100 })
.to("payments")
.publish()
.await
.expect("publish drives the handler to quiescence");
tb.broker::<LapinBroker>()
.subscriber("payments")
.assert_called_once()
.with(&Payment { amount: 100 })
.settled(HandlerOutcome::ack());
tb.broker::<LapinBroker>()
.message(&Payment { amount: 5_000 })
.to("payments.receipted")
.publish()
.await
.expect("publish drives the handler to quiescence");
tb.out::<Receipts>()
.assert_called_once()
.with_options(&LapinPublishOptions {
priority: Some(9),
..LapinPublishOptions::default()
});
tb.broker::<LapinBroker>()
.published::<Receipt>("receipts")
.assert_called_once()
.with(&Receipt { amount: 5_000 });
tb.shutdown().await.expect("shutdown");
println!("all in-process checks passed");
}