#![cfg(all(
feature = "inbox",
feature = "chrono",
feature = "json",
feature = "testing"
))]
mod events;
mod in_work;
mod keys;
mod own_lock;
mod sessions;
mod settle;
mod shutdown;
use std::future::{Future, ready};
use std::time::Duration;
use ruststream::{IncomingMessage, Outgoing};
use serde::{Deserialize, Serialize};
use sqlx::{Database, Pool};
use crate::live;
const POLL: Duration = Duration::from_millis(20);
const AT_ONCE: Duration = Duration::from_secs(5);
#[derive(Debug, Serialize, Deserialize, PartialEq, Outgoing)]
struct Email {
to: String,
}
fn email() -> Email {
Email {
to: "a@example.com".to_owned(),
}
}
trait DatabaseLocks: Database {
fn locks_held(pool: &Pool<Self>, keys: &[&str]) -> impl Future<Output = Option<i64>> + Send;
}
#[cfg(feature = "postgres")]
impl DatabaseLocks for sqlx::Postgres {
async fn locks_held(pool: &Pool<Self>, _: &[&str]) -> Option<i64> {
Some(live::postgres::advisory_locks(pool).await)
}
}
#[cfg(feature = "mysql")]
impl DatabaseLocks for sqlx::MySql {
async fn locks_held(pool: &Pool<Self>, keys: &[&str]) -> Option<i64> {
let mut held = 0;
for key in keys {
let name = live::mysql::lock_name(pool, key).await;
if live::mysql::lock_held(pool, &name).await {
held += 1;
}
}
Some(held)
}
}
#[cfg(feature = "sqlite")]
impl DatabaseLocks for sqlx::Sqlite {
fn locks_held(_: &Pool<Self>, _: &[&str]) -> impl Future<Output = Option<i64>> + Send {
ready(None)
}
}
async fn assert_no_lock<DB: DatabaseLocks>(pool: &Pool<DB>, keys: &[&str]) {
if let Some(held) = DB::locks_held(pool, keys).await {
assert_eq!(held, 0, "the database holds advisory locks of the broker");
}
}
fn payload_id(delivery: &impl IncomingMessage) -> i64 {
str::from_utf8(delivery.payload())
.expect("the payload is text")
.parse()
.expect("the payload is an id")
}