Struct TestClient

Source
pub struct TestClient { /* private fields */ }
Expand description

Client for use in tests.

After creating a TestClient, it is fully connected to the TestBroker, but neither the underlying Client nor the Connection are running. This must be done manually by taking both parts out and calling their respective run methods (see take_client and take_connection.

TestClient dereferences to aldrin::Handle and thus all methods on Handle can be called on TestClient as well.

Implementations§

Source§

impl TestClient

Source

pub fn handle(&self) -> &Handle

Returns a handle to the client.

Source

pub fn connection(&self) -> &ConnectionHandle

Returns a handle to the connection.

Source

pub fn take_client(&mut self) -> Client<BoxedTransport<'static, Disconnected>>

Takes the Client out of this struct.

After creating a TestClient, the actual Client must be taken out and run.

§Panics

This function panics when the Client has already been taken out. It must be called only once.

Source

pub fn take_connection( &mut self, ) -> Connection<BoxedTransport<'static, Disconnected>>

Takes the Connection out of this struct.

After creating a TestClient, the actual Connection must be taken out and run.

§Panics

This function panics when the Connection has already been taken out. It must be called only once.

Methods from Deref<Target = Handle>§

Source

pub fn shutdown(&self)

Shuts down the client.

Shutdown happens asynchronously, in the sense that when this function returns, the Client has only been requested to shut down and not yet necessarily done so. As soon as Client::run returns, it has fully shut down.

If the Client has already shut down (due to any reason), this function will not treat that as an error. This is different than most other functions, which would return Error::Shutdown instead.

Source

pub async fn create_object( &self, uuid: impl Into<ObjectUuid>, ) -> Result<Object, Error>

Creates a new object on the bus.

The uuid must not yet exists on the bus, or else Error::DuplicateObject will be returned. Use ObjectUuid::new_v4 to create a new random v4 UUID.

§Examples
use aldrin::Error;
use aldrin::core::ObjectUuid;
use uuid::uuid;

const OBJECT2_UUID: ObjectUuid = ObjectUuid(uuid!("6173e119-8066-4776-989b-145a5f16ed4c"));

// Create an object with a random UUID:
let object1 = handle.create_object(ObjectUuid::new_v4()).await?;

// Create an object with a fixed UUID:
let object2 = handle.create_object(OBJECT2_UUID).await?;

// Using the same UUID again will cause an error:
assert_eq!(
    handle.create_object(OBJECT2_UUID).await.unwrap_err(),
    Error::DuplicateObject,
);
Source

pub fn create_low_level_channel(&self) -> ChannelBuilder<'_>

Creates a low-level ChannelBuilder.

Alternatively, ChannelBuilder::new can be used as well.

Source

pub fn create_channel<T>(&self) -> ChannelBuilder<'_, T>

Creates a ChannelBuilder.

Alternatively, ChannelBuilder::new can be used as well.

Source

pub async fn sync_client(&self) -> Result<Instant, Error>

Synchronizes with the client.

Returns the timestamp when the Client has processed the request.

This function ensures that all previous requests to the client have been processed. There are some occasions in which requests are sent outside of an async context, e.g. when dropping values such as Object. By synchronizing with the client, it is possible to ensure that it has processed such a non-async request.

See also sync_broker, which ensures that such requests have been processed by the broker.

§Examples
use aldrin::core::ObjectUuid;
use std::mem;

let obj = handle.create_object(ObjectUuid::new_v4()).await?;

// Dropping obj will request the client to destroy the object.
mem::drop(obj);

// Ensure the request has actually been processed by the client.
handle.sync_client().await?;
Source

pub async fn sync_broker(&self) -> Result<Instant, Error>

Synchronizes with the broker.

Returns the timestamp when the Client has received the broker’s reply.

Certain requests such as emitting an event or sending an item on a channel don’t synchronize with the broker in the same way as e.g. creating an object does. This function can be used to ensure that such a request has been processed by the broker.

See also sync_client, which ensures only that such requests have been processed by the client.

§Examples

service.emit(0, "Hi!")?;

// Synchronize with the broker to ensure that the event has actually been processed.
handle.sync_broker().await?;
Source

pub async fn create_bus_listener(&self) -> Result<BusListener, Error>

Creates a new bus listener.

Bus listeners enable monitoring the bus for events about the creation and destruction of objects and services. See BusListener for more information and usage examples.

Source

pub fn create_discoverer<Key>(&self) -> DiscovererBuilder<'_, Key>
where Key: Copy + Eq + Hash,

Create a new DiscovererBuilder.

Source

pub async fn find_object<const N: usize>( &self, object: Option<ObjectUuid>, services: &[ServiceUuid; N], ) -> Result<Option<(ObjectId, [ServiceId; N])>, Error>

Find an object with a specific set of services.

If object is None, then any object that has all required services may be returned. Repeated calls to this function can return different objects.

This is a convenience function for using a Discoverer to find a single object among all current objects on the bus.

§Examples
// Create an object and 2 services to find.
let obj = client.create_object(ObjectUuid::new_v4()).await?;
let info = ServiceInfo::new(0);
let svc1 = obj.create_service(ServiceUuid::new_v4(), info).await?;
let svc2 = obj.create_service(ServiceUuid::new_v4(), info).await?;

// Find the object.
let (object_id, service_ids) = client
    .find_object(Some(obj.id().uuid), &[svc1.id().uuid, svc2.id().uuid])
    .await?
    .unwrap();

assert_eq!(object_id, obj.id());
assert_eq!(service_ids[0], svc1.id());
assert_eq!(service_ids[1], svc2.id());

Without specifying an ObjectUuid:

// Create 2 objects and sets of services to find.
let obj1 = client.create_object(ObjectUuid::new_v4()).await?;
let info = ServiceInfo::new(0);
let svc11 = obj1.create_service(ServiceUuid::new_v4(), info).await?;
let svc12 = obj1.create_service(ServiceUuid::new_v4(), info).await?;

let obj2 = client.create_object(ObjectUuid::new_v4()).await?;
let svc21 = obj2.create_service(svc11.id().uuid, info).await?;
let svc22 = obj2.create_service(svc12.id().uuid, info).await?;

// Find any one of the objects above.
let (object_id, service_ids) = client
    .find_object(None, &[svc11.id().uuid, svc12.id().uuid])
    .await?
    .unwrap();

assert!((object_id == obj1.id()) || (object_id == obj2.id()));
assert!((service_ids[0] == svc11.id()) || (service_ids[0] == svc21.id()));
assert!((service_ids[1] == svc12.id()) || (service_ids[1] == svc22.id()));
Source

pub async fn find_any_object<const N: usize>( &self, services: &[ServiceUuid; N], ) -> Result<Option<(ObjectId, [ServiceId; N])>, Error>

Finds any object implementing a set of services.

This is a shorthand for calling find_object(None, services).

Source

pub async fn find_specific_object<const N: usize>( &self, object: impl Into<ObjectUuid>, services: &[ServiceUuid; N], ) -> Result<Option<(ObjectId, [ServiceId; N])>, Error>

Finds a specific object implementing a set of services.

This is a shorthand for calling find_object(Some(object), services).

Source

pub async fn wait_for_object<const N: usize>( &self, object: Option<ObjectUuid>, services: &[ServiceUuid; N], ) -> Result<(ObjectId, [ServiceId; N]), Error>

Waits for an object with a specific set of services.

If object is None, then any object that has all required services may be returned. Repeated calls to this function can return different objects.

This is a convenience function for using a Discoverer to find a single object.

Source

pub async fn wait_for_any_object<const N: usize>( &self, services: &[ServiceUuid; N], ) -> Result<(ObjectId, [ServiceId; N]), Error>

Wait for any object implementing a set of services.

This is a shorthand for calling wait_for_object(None, services).

Source

pub async fn wait_for_specific_object<const N: usize>( &self, object: impl Into<ObjectUuid>, services: &[ServiceUuid; N], ) -> Result<(ObjectId, [ServiceId; N]), Error>

Wait for a specific object implementing a set of services.

This is a shorthand for calling wait_for_object(Some(object), services).

Source

pub async fn create_lifetime_scope(&self) -> Result<LifetimeScope, Error>

Creates a new lifetime scope.

Source

pub async fn create_lifetime(&self, id: LifetimeId) -> Result<Lifetime, Error>

Create a Lifetime from an id.

Source

pub async fn version(&self) -> Result<ProtocolVersion, Error>

Returns the protocol version that was negotiated with the broker.

Source

pub async fn create_proxy(&self, service: ServiceId) -> Result<Proxy, Error>

Creates a new proxy to a service.

Trait Implementations§

Source§

impl Debug for TestClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for TestClient

Source§

type Target = Handle

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Handle

Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.