#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#[cfg(test)]
mod test;
#[cfg(feature = "tokio")]
pub mod tokio;
use aldrin::{Client, Handle};
use aldrin_broker::{Broker, BrokerHandle, Connection, ConnectionHandle};
use aldrin_core::channel::{self, Disconnected};
use aldrin_core::transport::{AsyncTransportExt, BoxedTransport};
use futures_util::future;
use std::ops::{Deref, DerefMut};
#[doc(hidden)]
pub use {aldrin, aldrin_broker};
#[derive(Debug)]
pub struct TestBroker {
handle: BrokerHandle,
broker: Option<Broker>,
}
impl TestBroker {
pub fn new() -> Self {
let broker = Broker::new();
Self {
handle: broker.handle().clone(),
broker: Some(broker),
}
}
pub fn handle(&self) -> &BrokerHandle {
&self.handle
}
pub fn take_broker(&mut self) -> Broker {
self.broker.take().expect("broker already taken")
}
pub async fn add_client(&mut self) -> TestClient {
let (t1, t2) = channel::unbounded();
let client = Client::connect(t1.boxed());
let conn = self.handle.connect(t2.boxed());
let (client, conn) = future::join(client, conn).await;
let client = client.expect("client failed to connect");
let handle = client.handle().clone();
let conn = conn.expect("connection failed to establish");
let connection_handle = conn.handle().clone();
TestClient::new(handle, connection_handle, client, conn)
}
}
impl Default for TestBroker {
fn default() -> Self {
Self::new()
}
}
impl Deref for TestBroker {
type Target = BrokerHandle;
fn deref(&self) -> &BrokerHandle {
&self.handle
}
}
impl DerefMut for TestBroker {
fn deref_mut(&mut self) -> &mut BrokerHandle {
&mut self.handle
}
}
#[derive(Debug)]
pub struct TestClient {
handle: Handle,
connection_handle: ConnectionHandle,
client: Option<Client<BoxedTransport<'static, Disconnected>>>,
conn: Option<Connection<BoxedTransport<'static, Disconnected>>>,
}
impl TestClient {
fn new(
handle: Handle,
connection_handle: ConnectionHandle,
client: Client<BoxedTransport<'static, Disconnected>>,
conn: Connection<BoxedTransport<'static, Disconnected>>,
) -> Self {
Self {
handle,
connection_handle,
client: Some(client),
conn: Some(conn),
}
}
pub fn handle(&self) -> &Handle {
&self.handle
}
pub fn connection(&self) -> &ConnectionHandle {
&self.connection_handle
}
pub fn take_client(&mut self) -> Client<BoxedTransport<'static, Disconnected>> {
self.client.take().expect("client already taken")
}
pub fn take_connection(&mut self) -> Connection<BoxedTransport<'static, Disconnected>> {
self.conn.take().expect("connection already taken")
}
}
impl Deref for TestClient {
type Target = Handle;
fn deref(&self) -> &Handle {
&self.handle
}
}