Table of Contents

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

serviceProvider IServiceProvider

The IServiceProvider.

logger ILogger<TestingHelper>

The ILogger<TCategoryName>.

Fields

DefaultWaitTimeout

The default timeout for testing helper waits.

protected static readonly TimeSpan DefaultWaitTimeout

Field Value

TimeSpan

Properties

Outbox

Gets the IOutboxTestingHelper, exposing the helpers to test the applications using the transactional outbox.

public IOutboxTestingHelper Outbox { get; }

Property Value

IOutboxTestingHelper

Spy

Gets the IIntegrationSpy.

public IIntegrationSpy Spy { get; }

Property Value

IIntegrationSpy

Remarks

The IIntegrationSpy must be enabled calling AddIntegrationSpy or AddIntegrationSpyAndSubscriber.

Exceptions

InvalidOperationException

The IIntegrationSpy isn't registered.

Methods

GetConsumer(string)

Gets the consumer with the specified name.

public IConsumer GetConsumer(string name)

Parameters

name string

The consumer name.

Returns

IConsumer

The IConsumer with the specified name.

Exceptions

KeyNotFoundException

No consumer with the specified name exists.

GetConsumerEndpointConfiguration(string)

Returns the ConsumerEndpointConfiguration for the specified endpoint.

protected ConsumerEndpointConfiguration? GetConsumerEndpointConfiguration(string endpointName)

Parameters

endpointName string

The endpoint name. It could be either the topic/queue name or the friendly name. An endpoint with the specified friendly name takes precedence over an endpoint with the specified topic/queue name.

Returns

ConsumerEndpointConfiguration

The ConsumerEndpointConfiguration, or null if no endpoint matches.

GetConsumerForEndpoint(string)

Gets the existing consumer connected to specified endpoint.

public IConsumer GetConsumerForEndpoint(string endpointName)

Parameters

endpointName string

The endpoint name. It could be either the topic/queue name or the friendly name. An endpoint with the specified friendly name takes precedence over an endpoint with the specified topic/queue name.

Returns

IConsumer

The IConsumer.

Exceptions

InvalidOperationException

No consumer is configured for the specified endpoint.

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

endpointName string

The endpoint name. It could be either the topic/queue name or the friendly name.

Returns

string

The raw name (topic/queue).

Exceptions

InvalidOperationException

No producer or consumer endpoint matches the specified endpointName.

GetProducerEndpointConfiguration(string)

Returns the ProducerEndpointConfiguration for the specified endpoint.

protected ProducerEndpointConfiguration? GetProducerEndpointConfiguration(string endpointName)

Parameters

endpointName string

The endpoint name. It could be either the topic/queue name or the friendly name.

Returns

ProducerEndpointConfiguration

The ProducerEndpointConfiguration.

GetProducerEndpointConfigurations(string)

Returns the ProducerEndpointConfiguration of every producer endpoint matching the specified name: the endpoints with the specified friendly name or, if none, the endpoints with the specified topic/queue name.

protected IReadOnlyCollection<ProducerEndpointConfiguration> GetProducerEndpointConfigurations(string endpointName)

Parameters

endpointName string

The endpoint name. It could be either the topic/queue name or the friendly name.

Returns

IReadOnlyCollection<ProducerEndpointConfiguration>

The ProducerEndpointConfiguration of the matching endpoints, or an empty collection if none matches.

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

consumers IConsumerCollection

The existing consumers.

endpointName string

The endpoint name. It could be either the topic/queue name or the friendly name.

Returns

IProducer

The IProducer.

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.

GetProducerForEndpoint(string)

Gets a producer for the specified endpoint.

public IProducer GetProducerForEndpoint(string endpointName)

Parameters

endpointName string

The endpoint name. It could be either the topic/queue name or the friendly name.

Returns

IProducer

The IProducer.

Remarks

If no producer endpoint has the specified name, a new producer is created mirroring the consumer endpoint subscribing the topic or having the specified friendly name: it serializes and encrypts the messages the way the consumer expects them and produces them to the consumed topic. The connection security settings aren't mirrored.

Exceptions

InvalidOperationException

No producer or consumer is configured for the specified endpoint, the endpoint is a topic pattern or wildcard filter, the 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.

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 true if the outbox is empty, otherwise false.

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

throwTimeoutException bool

A value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the timeout elapses.

timeout TimeSpan?

The time to wait for the messages to be consumed and processed. The default is 30 seconds.

Returns

ValueTask

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

TimeoutException

The throwTimeoutException is true and the timeout elapsed before all messages were consumed and processed.

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

throwTimeoutException bool

A value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the timeout elapses.

timeout TimeSpan?

The time to wait for the messages to be consumed and processed. The default is 30 seconds.

endpointNames 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.

Returns

ValueTask

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

TimeoutException

The throwTimeoutException is true and the timeout elapsed before all messages were consumed and processed.

InvalidOperationException

One of the endpointNames doesn't match any endpoint.

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

throwTimeoutException bool

A value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the timeout elapses.

endpointNames 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.

Returns

ValueTask

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

TimeoutException

The throwTimeoutException is true and the default timeout elapsed before all messages were consumed and processed.

InvalidOperationException

One of the endpointNames doesn't match any endpoint.

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

throwTimeoutException bool

A value specifying whether a TimeoutException has to be thrown when the messages aren't consumed before the CancellationToken is canceled.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

endpointNames 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.

Returns

ValueTask

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

TimeoutException

The throwTimeoutException is true and the cancellationToken was canceled before all messages were consumed and processed.

InvalidOperationException

One of the endpointNames doesn't match any endpoint.

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

timeout TimeSpan?

The time to wait for the messages to be consumed and processed. The default is 30 seconds.

endpointNames 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.

Returns

ValueTask

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

TimeoutException

The timeout elapsed before all messages were consumed and processed.

InvalidOperationException

One of the endpointNames doesn't match any endpoint.

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

endpointNames 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.

Returns

ValueTask

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

TimeoutException

The default timeout elapsed before all messages were consumed and processed.

InvalidOperationException

One of the endpointNames doesn't match any endpoint.

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

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

endpointNames 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.

Returns

ValueTask

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

TimeoutException

The cancellationToken was canceled before all messages were consumed and processed.

InvalidOperationException

One of the endpointNames doesn't match any endpoint.

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

endpointNames IReadOnlyCollection<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.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A ValueTask that completes when all messages have been processed.

Remarks

This method works with the mocked brokers only.

Exceptions

InvalidOperationException

One of the endpointNames doesn't match any endpoint.

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

waitAction Func<IReadOnlyCollection<string>, CancellationToken, Task>

The broker operation to await.

timeoutMessage string

The message to log or include in the exception when the wait is canceled.

throwTimeoutException bool

Whether to throw instead of logging when the wait is canceled.

endpointNames string[]

The endpoint names to wait for, or an empty array to consider all endpoints.

cancellationToken CancellationToken

The cancellation token to observe.

Returns

ValueTask

A task that completes when the broker operation and the outbox have completed.

Exceptions

TimeoutException

The throwTimeoutException is true and the cancellationToken was canceled before the broker operation and the outbox 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

throwTimeoutException bool

A value specifying whether a TimeoutException has to be thrown when the connection isn't established before the timeout elapses.

timeout TimeSpan?

The time to wait for the consumers to connect. The default is 30 seconds.

Returns

ValueTask

A ValueTask that completes when all consumers are successfully connected and ready.

Exceptions

TimeoutException

The throwTimeoutException is true and the timeout elapsed before all consumers were connected.

WaitUntilConnectedAsync(bool, CancellationToken)

Returns a ValueTask that completes when all consumers are connected and ready.

public ValueTask WaitUntilConnectedAsync(bool throwTimeoutException, CancellationToken cancellationToken)

Parameters

throwTimeoutException bool

A value specifying whether a TimeoutException has to be thrown when the connection isn't established before the CancellationToken is canceled.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask

A Task that completes when all consumers are successfully connected and ready.

Exceptions

TimeoutException

The throwTimeoutException is true and the cancellationToken was canceled before all consumers were connected.

WaitUntilConnectedAsync(TimeSpan?)

Returns a ValueTask that completes when all consumers are connected and ready.

public ValueTask WaitUntilConnectedAsync(TimeSpan? timeout = null)

Parameters

timeout TimeSpan?

The time to wait for the consumers to connect. The default is 30 seconds.

Returns

ValueTask

A ValueTask that completes when all consumers are successfully connected and ready.

Exceptions

TimeoutException

The timeout elapsed before all consumers were connected.

WaitUntilConnectedAsync(CancellationToken)

Returns a ValueTask that completes when all consumers are connected and ready.

public ValueTask WaitUntilConnectedAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask

A ValueTask that completes when all consumers are successfully connected and ready.

Exceptions

TimeoutException

The cancellationToken was canceled before all consumers were connected.

WaitUntilOutboxIsEmptyAsync(TimeSpan?)

Returns a ValueTask that completes when all messages stored in the outbox have been produced.

public ValueTask WaitUntilOutboxIsEmptyAsync(TimeSpan? timeout = null)

Parameters

timeout TimeSpan?

The time to wait for the messages to be consumed and processed. The default is 30 seconds.

Returns

ValueTask

A ValueTask that completes when the outbox is empty.

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

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask

A ValueTask that completes when the outbox is empty.

Remarks

Only the outboxes actively drained by a registered and not paused outbox worker are considered (see IOutboxTestingHelper).

Exceptions

OperationCanceledException

The cancellationToken was canceled before the outbox was emptied.