Class MqttTestingHelper
- Namespace
- Silverback.Testing
- Assembly
- Silverback.Integration.MQTT.Testing.dll
Exposes some helper methods and shortcuts to simplify testing.
public class MqttTestingHelper : TestingHelper, IMqttTestingHelper, ITestingHelper
- Inheritance
-
MqttTestingHelper
- Implements
- Inherited Members
Constructors
MqttTestingHelper(IServiceProvider, ILogger<MqttTestingHelper>)
Initializes a new instance of the MqttTestingHelper class.
public MqttTestingHelper(IServiceProvider serviceProvider, ILogger<MqttTestingHelper> logger)
Parameters
serviceProviderIServiceProviderThe IServiceProvider.
loggerILogger<MqttTestingHelper>The ISilverbackLogger.
Methods
GetClientSession(string)
Gets the IClientSession of the specified client.
public IClientSession GetClientSession(string clientId)
Parameters
clientIdstringThe client id.
Returns
Remarks
This method works with the mocked MQTT broker only. See UseMockedMqtt(SilverbackBuilder, Action<IMockedMqttOptionsBuilder>?) or AddMockedMqtt(BrokerOptionsBuilder, Action<IMockedMqttOptionsBuilder>?).
Exceptions
- InvalidOperationException
No session (or more than one) exists for the specified client.
GetMessages(string)
Gets the messages that have been published to the specified topic.
public IReadOnlyList<MqttApplicationMessage> GetMessages(string topic)
Parameters
topicstringThe topic.
Returns
- IReadOnlyList<MqttApplicationMessage>
The messages published to the topic.
Remarks
This method works with the mocked MQTT broker only. See UseMockedMqtt(SilverbackBuilder, Action<IMockedMqttOptionsBuilder>?) or AddMockedMqtt(BrokerOptionsBuilder, Action<IMockedMqttOptionsBuilder>?).
GetProducer(Action<MqttClientConfigurationBuilder>)
Gets a new producer with the specified configuration.
public IProducer GetProducer(Action<MqttClientConfigurationBuilder> configurationBuilderAction)
Parameters
configurationBuilderActionAction<MqttClientConfigurationBuilder>An Action<T> that takes the MqttClientConfigurationBuilder and configures it.
Returns
Exceptions
- BrokerConfigurationException
The client configuration is not valid.
- InvalidOperationException
The MQTT integration hasn't been added (
AddMqtthasn't been called), the configuration doesn't define exactly one producer endpoint, or a producer endpoint with the same name has already been added.
GetProducerForConsumer(IConsumerCollection, string)
Gets an existing producer for the endpoint consumed by the specified consumer or initializes a new one mirroring the consumer configuration.
protected override IProducer? GetProducerForConsumer(IConsumerCollection consumers, string endpointName)
Parameters
consumersIConsumerCollectionThe existing consumers.
endpointNamestringThe endpoint name. It could be either the topic/queue name or the friendly name.
Returns
Exceptions
- InvalidOperationException
The endpoint is a topic pattern or wildcard filter, the matching consumer endpoint subscribes only topic patterns or wildcard filters, or the key provider of the consumer's decryption settings doesn't return a key.
- NotSupportedException
The decryption settings of the consumer endpoint to mirror aren't supported.
WaitUntilAllMessagesAreConsumedCoreAsync(IReadOnlyCollection<string>, CancellationToken)
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
protected override Task WaitUntilAllMessagesAreConsumedCoreAsync(IReadOnlyCollection<string> endpointNames, CancellationToken cancellationToken)
Parameters
endpointNamesIReadOnlyCollection<string>The names of the endpoints to wait for. If not specified, or if one of them is the name of a producer endpoint of the tested broker whose topic is resolved dynamically per message, all endpoints are considered.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Remarks
This method works with the mocked brokers only.
Exceptions
- InvalidOperationException
One of the
endpointNamesdoesn't match any endpoint.