#![cfg_attr(not(test), allow(dead_code, unused_imports))]
use serde::{Deserialize, Serialize};
use ruststream_fred::stream::prelude::*;
#[cfg(test)]
#[path = "../tests/live/mod.rs"]
mod live;
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing)]
#[outgoing(name = "testing-live.payments")]
struct Payment {
amount: u64,
}
#[subscriber(RedisStream::new("testing-live.payments").group("workers"))]
async fn accept(payment: &Payment) -> HandlerOutcome {
if payment.amount == 0 {
return HandlerOutcome::drop();
}
HandlerOutcome::ack()
}
fn app(url: &str) -> impl App<State = ()> {
RustStream::new(AppInfo::new("payments", "0.1.0")).with_broker(
RedisBroker::standalone(url),
|b| {
b.include(accept);
},
)
}
#[ruststream::app]
fn service() -> impl App<State = ()> {
app("redis://localhost:6379")
}
#[cfg(test)]
mod tests {
use std::error::Error;
use ruststream::testing::TestApp;
use ruststream_fred::stream::prelude::*;
use super::{Payment, app, live};
async fn accepts_a_payment(tb: TestApp<()>) -> Result<(), Box<dyn Error>> {
tb.broker::<RedisBroker>()
.message(&Payment { amount: 100 })
.publish()
.await?;
tb.broker::<RedisBroker>()
.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("redis://localhost:6379")).await?).await
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn against_the_server() -> Result<(), Box<dyn Error>> {
let Some(url) = live::url("REDIS_TEST_URL") else {
return Ok(());
};
accepts_a_payment(TestApp::start_live(app(&url)).await?).await
}
}