use std::future::Future;
use std::time::Duration;
use encore::pubsub::{
Encoding, Json, Message, OrderedTopic, PublishOptions, Raw, Subscription, Topic,
};
use encore::Error;
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct SignupEvent {
user_id: String,
}
static SIGNUPS: Topic<SignupEvent> = Topic::new("signups");
static PAYMENTS: Topic<SignupEvent> = Topic::new("payments").exactly_once();
static BLOBS: Topic<Vec<u8>, Raw> = Topic::new("blobs");
static CHANGES: OrderedTopic<SignupEvent> = OrderedTopic::new("changes").exactly_once();
static ELSEWHERE: Topic<SignupEvent> = Topic::named("elsewhere");
async fn send_welcome(msg: Message<SignupEvent>) -> Result<(), Error> {
let _ = (msg.id, msg.data.user_id, msg.delivery_attempt);
Ok(())
}
async fn store(msg: Message<Vec<u8>>) -> Result<(), Error> {
let _ = msg.raw_data;
Ok(())
}
#[encore::pubsub::register]
static WELCOME: Subscription<SignupEvent> =
Subscription::new(&SIGNUPS, "send-welcome", send_welcome)
.ack_deadline(Duration::from_secs(60))
.message_retention(Duration::from_secs(24 * 60 * 60))
.min_backoff(Duration::from_millis(500))
.max_backoff(Duration::from_secs(60))
.max_retries(5)
.max_concurrency(4);
#[encore::pubsub::register]
static STORE: Subscription<Vec<u8>> = Subscription::new(&BLOBS, "store", store);
#[encore::pubsub::register]
static ORDERED: Subscription<SignupEvent> =
Subscription::new(&CHANGES, "apply", |msg| Box::pin(send_welcome(msg)));
static BY_HAND: Subscription<SignupEvent> =
Subscription::new(&PAYMENTS, "audit", |msg| Box::pin(send_welcome(msg)));
fn assert_send<F: Future + Send>(_: F) {}
#[test]
fn a_topic_publishes_what_its_type_says() {
let event = SignupEvent {
user_id: "ada".to_string(),
};
assert_send(SIGNUPS.publish(&event));
assert_send(ELSEWHERE.publish_with(
&event,
PublishOptions {
attributes: [("source".to_string(), "test".to_string())].into(),
},
));
assert_send(BLOBS.publish(&vec![1, 2, 3]));
assert_send(CHANGES.publish(&event, "ada"));
assert_send(CHANGES.publish_with(&event, "ada", PublishOptions::default()));
assert_eq!(SIGNUPS.name(), "signups");
}
#[tokio::test]
async fn an_ordered_topic_needs_a_key() {
let event = SignupEvent {
user_id: "ada".to_string(),
};
let err = CHANGES.publish(&event, "").await.unwrap_err();
assert_eq!(err.code, encore::ErrCode::InvalidArgument, "{err}");
}
#[test]
fn a_subscription_is_declared_with_or_without_the_attribute() {
assert_eq!(WELCOME.name(), "send-welcome");
assert_eq!(STORE.name(), "store");
assert_eq!(ORDERED.name(), "apply");
assert_eq!(BY_HAND.name(), "audit");
let _start: fn(&'static Subscription<SignupEvent>) -> Result<(), Error> = Subscription::start;
}
#[test]
fn an_encoding_round_trips_a_message() {
let event = SignupEvent {
user_id: "ada".to_string(),
};
let json = <Json as Encoding<SignupEvent>>::encode(&event).unwrap();
assert_eq!(json, br#"{"user_id":"ada"}"#);
assert_eq!(
<Json as Encoding<SignupEvent>>::decode(&json).unwrap(),
event
);
assert!(<Json as Encoding<SignupEvent>>::decode(b"not json").is_err());
let raw = <Raw as Encoding<Vec<u8>>>::encode(&vec![0, 159, 146]).unwrap();
assert_eq!(raw, [0, 159, 146]);
assert_eq!(<Raw as Encoding<Vec<u8>>>::decode(&raw).unwrap(), raw);
}