#![cfg_attr(not(test), allow(dead_code, unused_imports))]
use serde::{Deserialize, Serialize};
use ruststream_pulsar::prelude::*;
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing)]
#[outgoing(name = "testing-live-payments")]
struct Payment {
amount: u64,
}
#[subscriber("testing-live-payments")]
async fn accept(payment: &Payment) -> HandlerOutcome {
if payment.amount == 0 {
return HandlerOutcome::drop();
}
HandlerOutcome::ack()
}
fn app(url: &str, subscription: &str) -> impl App<State = ()> {
RustStream::new(AppInfo::new("payments", "0.1.0")).with_broker(
PulsarBroker::new(url).default_subscription(subscription),
|b| {
b.include(accept);
},
)
}
#[ruststream::app]
fn service() -> impl App<State = ()> {
app("pulsar://localhost:6650", "payments-svc")
}
#[cfg(test)]
mod tests {
use std::env;
use std::error::Error;
use std::process;
use ruststream::testing::TestApp;
use ruststream_pulsar::prelude::*;
use super::{Payment, app};
fn stand() -> Option<String> {
match env::var("PULSAR_TEST_URL") {
Ok(url) if !url.is_empty() => Some(url),
_ => {
assert!(
env::var_os("RUSTSTREAM_REQUIRE_LIVE").is_none(),
"RUSTSTREAM_REQUIRE_LIVE is set, but PULSAR_TEST_URL is missing or empty",
);
None
}
}
}
async fn accepts_a_payment(tb: TestApp<()>) -> Result<(), Box<dyn Error>> {
tb.broker::<PulsarBroker>()
.message(&Payment { amount: 100 })
.publish()
.await?;
tb.broker::<PulsarBroker>()
.subscriber("testing-live-payments")
.assert_called_once()
.with(&Payment { amount: 100 })
.settled(HandlerOutcome::ack());
tb.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn in_process() -> Result<(), Box<dyn Error>> {
accepts_a_payment(TestApp::start(app("pulsar://localhost:6650", "payments-svc")).await?)
.await
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn against_the_stand() -> Result<(), Box<dyn Error>> {
let Some(url) = stand() else {
return Ok(());
};
let subscription = format!("payments-{}", process::id());
accepts_a_payment(TestApp::start_live(app(&url, &subscription)).await?).await
}
}