#![cfg_attr(not(test), allow(dead_code, unused_imports))]
use ruststream::memory::prelude::*;
use ruststream::schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, JsonSchema)]
struct Order {
id: u64,
tenant: String,
}
#[derive(Debug, Serialize, Outgoing, JsonSchema)]
struct Confirmed {
id: u64,
}
#[subscriber("order-requests", reply("confirmations"))]
async fn confirm(order: &Order) -> Confirmed {
Confirmed { id: order.id }
}
#[derive(Debug, Serialize, Outgoing, JsonSchema)]
#[outgoing(name = "orders.{tenant}.accepted")]
struct Accepted {
id: u64,
}
#[derive(OutSlot)]
#[publishes(Accepted)]
struct Tenants;
#[subscriber("orders")]
async fn accept(order: &Order, Out(tenants): Out<impl Publisher, Tenants>) -> HandlerOutcome {
let accepted = Accepted { id: order.id };
match tenants
.message(&accepted)
.to()
.tenant(&order.tenant)
.publish()
.await
{
Ok(()) => HandlerOutcome::ack(),
Err(_) => HandlerOutcome::retry(),
}
}
#[derive(Debug, Deserialize, JsonSchema)]
struct ShipmentMeta {
trace_id: String,
attempt: u32,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct Shipment {
id: u64,
}
#[subscriber("shipments")]
async fn ship(shipment: &Shipment, Headers(meta): Headers<ShipmentMeta>) -> HandlerOutcome {
tracing::info!(shipment.id, trace_id = %meta.trace_id, meta.attempt, "shipped");
HandlerOutcome::ack()
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct Payment {
id: u64,
}
#[subscriber("payments")]
async fn charge(payment: &Payment) -> HandlerOutcome {
tracing::warn!(payment.id, "gateway refused the charge");
HandlerOutcome::retry()
}
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(confirm);
b.include(accept).out(Tenants, Publish).build();
b.include(ship);
b.include(charge)
.max_attempts(nonzero!(3u32))
.dead_letter("payments.dead");
})
}
fn main() {}
#[cfg(test)]
mod tests {
use std::error::Error;
use ruststream::asyncapi::build_spec;
use serde_json::{Value, json};
use super::app;
fn document() -> Result<Value, Box<dyn Error>> {
Ok(serde_json::from_str(&build_spec(&app()).to_json()?)?)
}
#[test]
fn the_reply_rides_the_receive_operation() -> Result<(), Box<dyn Error>> {
let spec = document()?;
let operation = &spec["operations"]["receive_order_requests"];
assert_eq!(
operation["reply"],
json!({
"channel": { "$ref": "#/channels/confirmations" },
"messages": [{ "$ref": "#/channels/confirmations/messages/Confirmed" }],
})
);
assert_eq!(
spec["channels"]["confirmations"]["address"],
"confirmations"
);
assert!(spec["components"]["messages"]["Confirmed"]["payload"].is_object());
assert!(spec["operations"]["send_order_requests_confirmations"].is_null());
Ok(())
}
#[test]
fn a_name_template_becomes_a_channel_with_parameters() -> Result<(), Box<dyn Error>> {
let spec = document()?;
let channel = &spec["channels"]["orders.{tenant}.accepted"];
assert_eq!(channel["address"], "orders.{tenant}.accepted");
assert_eq!(channel["parameters"], json!({ "tenant": {} }));
let send = &spec["operations"]["send_orders_orders__tenant__accepted"];
assert_eq!(send["action"], "send");
assert_eq!(
send["messages"],
json!([{ "$ref": "#/channels/orders.{tenant}.accepted/messages/Accepted" }])
);
Ok(())
}
#[test]
fn the_header_contract_becomes_the_headers_schema() -> Result<(), Box<dyn Error>> {
let spec = document()?;
let headers = &spec["components"]["messages"]["Shipment"]["headers"];
assert_eq!(headers["required"], json!(["trace_id", "attempt"]));
assert_eq!(
headers["properties"]["trace_id"]["description"],
"The trace the shipment belongs to."
);
Ok(())
}
#[test]
fn the_retry_declaration_is_an_extension_of_the_operation() -> Result<(), Box<dyn Error>> {
let spec = document()?;
assert_eq!(
spec["operations"]["receive_payments"]["x-ruststream-retry"],
json!({ "maxAttempts": 3, "deadLetter": "payments.dead" })
);
assert_eq!(
spec["channels"]["payments.dead"]["address"],
"payments.dead"
);
assert_eq!(
spec["operations"]["send_payments_payments_dead"]["action"],
"send"
);
Ok(())
}
}