Class TestingHelper
- Namespace
- Silverback.Testing
- Assembly
- Silverback.Integration.Testing.dll
Exposes some helper methods and shortcuts to simplify testing.
public abstract class TestingHelper : ITestingHelper
- Inheritance
-
TestingHelper
- Implements
- Derived
- Inherited Members
Constructors
TestingHelper(IServiceProvider, ILogger<TestingHelper>)
Initializes a new instance of the TestingHelper class.
protected TestingHelper(IServiceProvider serviceProvider, ILogger<TestingHelper> logger)
Parameters
serviceProviderIServiceProviderThe IServiceProvider.
loggerILogger<TestingHelper>
Fields
DefaultWaitTimeout
The default timeout for testing helper waits.
protected static readonly TimeSpan DefaultWaitTimeout
Field Value
Properties
Outbox
Gets the IOutboxTestingHelper, exposing the helpers to test the applications using the transactional outbox.
public IOutboxTestingHelper Outbox { get; }
Property Value
Spy
Gets the IIntegrationSpy.
public IIntegrationSpy Spy { get; }
Property Value
Remarks
The IIntegrationSpy must be enabled calling AddIntegrationSpy or AddIntegrationSpyAndSubscriber.
Methods
GetConsumer(string)
Gets the consumer with the specified name.
public IConsumer GetConsumer(string name)
Parameters
namestringThe consumer name.
Returns
GetConsumerEndpointConfiguration(string)
Returns the ConsumerEndpointConfiguration for the specified endpoint.
protected ConsumerEndpointConfiguration? GetConsumerEndpointConfiguration(string endpointName)
Parameters
endpointNamestringThe endpoint name. It could be either the topic/queue name or the friendly name.
Returns
GetConsumerForEndpoint(string)
Gets the existing consumer connected to specified endpoint.
public IConsumer GetConsumerForEndpoint(string endpointName)
Parameters
endpointNamestringThe endpoint name. It could be either the topic/queue name or the friendly name.
Returns
GetEndpointRawName(string)
Resolves the raw name (topic/queue name) of the specified endpoint. If the endpoint is not found in the producers or consumers configurations an InvalidOperationException is thrown.
protected string GetEndpointRawName(string endpointName)
Parameters
endpointNamestringThe endpoint name. It could be either the topic/queue name or the friendly name.
Returns
- string
The raw name (topic/queue).
GetProducerEndpointConfiguration(string)
Returns the ProducerEndpointConfiguration for the specified endpoint.
protected ProducerEndpointConfiguration? GetProducerEndpointConfiguration(string endpointName)
Parameters
endpointNamestringThe endpoint name. It could be either the topic/queue name or the friendly name.
Returns
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 abstract 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
GetProducerForEndpoint(string)
Gets a producer for the specified endpoint.
public IProducer GetProducerForEndpoint(string endpointName)
Parameters
endpointNamestringThe endpoint name. It could be either the topic/queue name or the friendly name.
Returns
IsOutboxEmptyAsync()
Checks whether the outbox (table) is empty.
public ValueTask<bool> IsOutboxEmptyAsync()
Returns
- ValueTask<bool>
A ValueTask<TResult> representing the asynchronous operation. The task result contains
trueif the outbox is empty, otherwisefalse.
Remarks
Only the outboxes actively drained by a registered and not paused outbox worker are considered (see IOutboxTestingHelper).
WaitUntilAllMessagesAreConsumedAsync(bool, TimeSpan?)
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
public ValueTask WaitUntilAllMessagesAreConsumedAsync(bool throwTimeoutException, TimeSpan? timeout = null)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the timeout elapses.
timeoutTimeSpan?The time to wait for the messages to be consumed and processed. The default is 30 seconds.
Returns
Remarks
This method works with the mocked brokers only.
WaitUntilAllMessagesAreConsumedAsync(bool, TimeSpan?, params string[])
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
public ValueTask WaitUntilAllMessagesAreConsumedAsync(bool throwTimeoutException, TimeSpan? timeout, params string[] endpointNames)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the timeout elapses.
timeoutTimeSpan?The time to wait for the messages to be consumed and processed. The default is 30 seconds.
endpointNamesstring[]The names of the endpoints to wait for. If not specified, all endpoints are considered.
Returns
Remarks
This method works with the mocked brokers only.
WaitUntilAllMessagesAreConsumedAsync(bool, params string[])
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
public ValueTask WaitUntilAllMessagesAreConsumedAsync(bool throwTimeoutException, params string[] endpointNames)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the timeout elapses.
endpointNamesstring[]The names of the endpoints to wait for. If not specified, all endpoints are considered.
Returns
Remarks
This method works with the mocked brokers only.
WaitUntilAllMessagesAreConsumedAsync(bool, CancellationToken, params string[])
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
public ValueTask WaitUntilAllMessagesAreConsumedAsync(bool throwTimeoutException, CancellationToken cancellationToken, params string[] endpointNames)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the CancellationToken is canceled.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
endpointNamesstring[]The names of the endpoints to wait for. If not specified, all endpoints are considered.
Returns
Remarks
This method works with the mocked brokers only.
WaitUntilAllMessagesAreConsumedAsync(TimeSpan?, params string[])
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
public ValueTask WaitUntilAllMessagesAreConsumedAsync(TimeSpan? timeout, params string[] endpointNames)
Parameters
timeoutTimeSpan?endpointNamesstring[]The names of the endpoints to wait for. If not specified, all endpoints are considered.
Returns
Remarks
This method works with the mocked brokers only.
WaitUntilAllMessagesAreConsumedAsync(params string[])
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
public ValueTask WaitUntilAllMessagesAreConsumedAsync(params string[] endpointNames)
Parameters
endpointNamesstring[]The names of the endpoints to wait for. If not specified, all endpoints are considered.
Returns
Remarks
This method works with the mocked brokers only.
WaitUntilAllMessagesAreConsumedAsync(CancellationToken, params string[])
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
public ValueTask WaitUntilAllMessagesAreConsumedAsync(CancellationToken cancellationToken, params string[] endpointNames)
Parameters
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
endpointNamesstring[]The names of the endpoints to wait for. If not specified, all endpoints are considered.
Returns
Remarks
This method works with the mocked brokers only.
WaitUntilAllMessagesAreConsumedCoreAsync(IReadOnlyCollection<string>, CancellationToken)
Returns a ValueTask that completes when all messages routed to the consumers have been processed.
protected abstract Task WaitUntilAllMessagesAreConsumedCoreAsync(IReadOnlyCollection<string> endpointNames, CancellationToken cancellationToken)
Parameters
endpointNamesIReadOnlyCollection<string>The names of the endpoints to wait for. If not specified, 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.
WaitUntilAllMessagesCoreAsync(Func<IReadOnlyCollection<string>, CancellationToken, Task>, string, bool, string[], CancellationToken)
Waits for the outbox and the specified broker operation, repeating if consumers produce more outbox messages.
protected ValueTask WaitUntilAllMessagesCoreAsync(Func<IReadOnlyCollection<string>, CancellationToken, Task> waitAction, string timeoutMessage, bool throwTimeoutException, string[] endpointNames, CancellationToken cancellationToken)
Parameters
waitActionFunc<IReadOnlyCollection<string>, CancellationToken, Task>The broker operation to await.
timeoutMessagestringThe message to log or include in the exception when the wait is canceled.
throwTimeoutExceptionboolWhether to throw instead of logging when the wait is canceled.
endpointNamesstring[]The endpoint names to wait for, or an empty array to consider all endpoints.
cancellationTokenCancellationTokenThe cancellation token to observe.
Returns
- ValueTask
A task that completes when the broker operation and the outbox have completed.
WaitUntilConnectedAsync(bool, TimeSpan?)
Returns a ValueTask that completes when all consumers are connected and ready.
public ValueTask WaitUntilConnectedAsync(bool throwTimeoutException, TimeSpan? timeout = null)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the connection isn't established before the timeout elapses.
timeoutTimeSpan?The time to wait for the consumers to connect. The default is 30 seconds.
Returns
WaitUntilConnectedAsync(bool, CancellationToken)
Returns a ValueTask that completes when all consumers are connected and ready.
public ValueTask WaitUntilConnectedAsync(bool throwTimeoutException, CancellationToken cancellationToken)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the connection isn't established before the CancellationToken is canceled.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
WaitUntilConnectedAsync(TimeSpan?)
Returns a ValueTask that completes when all consumers are connected and ready.
public ValueTask WaitUntilConnectedAsync(TimeSpan? timeout = null)
Parameters
timeoutTimeSpan?The time to wait for the consumers to connect. The default is 30 seconds.
Returns
WaitUntilConnectedAsync(CancellationToken)
Returns a ValueTask that completes when all consumers are connected and ready.
public ValueTask WaitUntilConnectedAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
WaitUntilOutboxIsEmptyAsync(TimeSpan?)
Returns a ValueTask that completes when all messages stored in the outbox have been produced.
public ValueTask WaitUntilOutboxIsEmptyAsync(TimeSpan? timeout = null)
Parameters
timeoutTimeSpan?The time to wait for the messages to be consumed and processed. The default is 30 seconds.
Returns
Remarks
Only the outboxes actively drained by a registered and not paused outbox worker are considered (see IOutboxTestingHelper).
Exceptions
- TimeoutException
The timeout elapsed before the outbox was emptied.
WaitUntilOutboxIsEmptyAsync(CancellationToken)
Returns a ValueTask that completes when all messages stored in the outbox have been produced.
public ValueTask WaitUntilOutboxIsEmptyAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Remarks
Only the outboxes actively drained by a registered and not paused outbox worker are considered (see IOutboxTestingHelper).
Exceptions
- OperationCanceledException
The
cancellationTokenwas canceled before the outbox was emptied.