pub struct TestClient { /* private fields */ }Expand description
Tokio-based client for use in tests.
TestClient dereferences to aldrin::Handle and thus all methods on Handle can
be called on TestClient as well.
Implementations§
Source§impl TestClient
impl TestClient
Sourcepub fn connection(&self) -> &ConnectionHandle
pub fn connection(&self) -> &ConnectionHandle
Returns a handle to the connection.
Sourcepub async fn join(&mut self)
pub async fn join(&mut self)
Shuts down the client and joins the client and connection tasks.
This function cannot be canceled in a meaningful way after it has been polled once, because it would panic if called (and polled) again. Ensure you call it only once and then poll it to completion.
§Panics
This function will panic if the tasks have already been joined or attempted to join (see notes above as well).
Methods from Deref<Target = Handle>§
Sourcepub fn shutdown(&self)
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.
Sourcepub async fn create_object(
&self,
uuid: impl Into<ObjectUuid>,
) -> Result<Object, Error>
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,
);Sourcepub fn create_low_level_channel(&self) -> ChannelBuilder<'_>
pub fn create_low_level_channel(&self) -> ChannelBuilder<'_>
Creates a low-level ChannelBuilder.
Alternatively, ChannelBuilder::new can be used as well.
Sourcepub fn create_channel<T>(&self) -> ChannelBuilder<'_, T>
pub fn create_channel<T>(&self) -> ChannelBuilder<'_, T>
Creates a ChannelBuilder.
Alternatively, ChannelBuilder::new can be used as well.
Sourcepub async fn sync_client(&self) -> Result<Instant, Error>
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?;Sourcepub async fn sync_broker(&self) -> Result<Instant, Error>
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?;
Sourcepub async fn create_bus_listener(&self) -> Result<BusListener, Error>
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.
Sourcepub fn create_discoverer<Key>(&self) -> DiscovererBuilder<'_, Key>
pub fn create_discoverer<Key>(&self) -> DiscovererBuilder<'_, Key>
Create a new DiscovererBuilder.
Sourcepub async fn find_object<const N: usize>(
&self,
object: Option<ObjectUuid>,
services: &[ServiceUuid; N],
) -> Result<Option<(ObjectId, [ServiceId; N])>, Error>
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()));
Sourcepub async fn find_any_object<const N: usize>(
&self,
services: &[ServiceUuid; N],
) -> Result<Option<(ObjectId, [ServiceId; N])>, Error>
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).
Sourcepub async fn find_specific_object<const N: usize>(
&self,
object: impl Into<ObjectUuid>,
services: &[ServiceUuid; N],
) -> Result<Option<(ObjectId, [ServiceId; N])>, Error>
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).
Sourcepub async fn wait_for_object<const N: usize>(
&self,
object: Option<ObjectUuid>,
services: &[ServiceUuid; N],
) -> Result<(ObjectId, [ServiceId; N]), Error>
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.
Sourcepub async fn wait_for_any_object<const N: usize>(
&self,
services: &[ServiceUuid; N],
) -> Result<(ObjectId, [ServiceId; N]), Error>
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).
Sourcepub async fn wait_for_specific_object<const N: usize>(
&self,
object: impl Into<ObjectUuid>,
services: &[ServiceUuid; N],
) -> Result<(ObjectId, [ServiceId; N]), Error>
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).
Sourcepub async fn create_lifetime_scope(&self) -> Result<LifetimeScope, Error>
pub async fn create_lifetime_scope(&self) -> Result<LifetimeScope, Error>
Creates a new lifetime scope.
Sourcepub async fn create_lifetime(&self, id: LifetimeId) -> Result<Lifetime, Error>
pub async fn create_lifetime(&self, id: LifetimeId) -> Result<Lifetime, Error>
Create a Lifetime from an id.
Sourcepub async fn version(&self) -> Result<ProtocolVersion, Error>
pub async fn version(&self) -> Result<ProtocolVersion, Error>
Returns the protocol version that was negotiated with the broker.