Class KafkaTestingHelper
- Namespace
- Silverback.Testing
- Assembly
- Silverback.Integration.Kafka.Testing.dll
Exposes some helper methods and shortcuts to simplify testing.
public class KafkaTestingHelper : TestingHelper, IKafkaTestingHelper, ITestingHelper
- Inheritance
-
KafkaTestingHelper
- Implements
- Inherited Members
Constructors
KafkaTestingHelper(IServiceProvider, ILogger<KafkaTestingHelper>)
Initializes a new instance of the KafkaTestingHelper class.
public KafkaTestingHelper(IServiceProvider serviceProvider, ILogger<KafkaTestingHelper> logger)
Parameters
serviceProviderIServiceProviderThe IServiceProvider.
loggerILogger<KafkaTestingHelper>The ISilverbackLogger.
Properties
ConsumerGroups
Gets a collection of IMockedConsumerGroup representing all known consumer groups.
public IReadOnlyCollection<IMockedConsumerGroup> ConsumerGroups { get; }
Property Value
- IReadOnlyCollection<IMockedConsumerGroup>
The collection of IMockedConsumerGroup.
Methods
GetConsumerGroup(string)
Returns the IMockedConsumerGroup representing the consumer group with the specified id.
public IMockedConsumerGroup GetConsumerGroup(string groupId)
Parameters
groupIdstringThe consumer group id.
Returns
Remarks
This method works with the mocked Kafka broker only. See UseMockedKafka(SilverbackBuilder, Action<IMockedKafkaOptionsBuilder>?) or AddMockedKafka(BrokerOptionsBuilder, Action<IMockedKafkaOptionsBuilder>?).
Exceptions
- InvalidOperationException
No consumer group with the specified id exists, or more than one exists (with different bootstrap servers).
GetConsumerGroup(string, string)
Returns the IMockedConsumerGroup representing the consumer group with the specified id.
public IMockedConsumerGroup GetConsumerGroup(string groupId, string bootstrapServers)
Parameters
groupIdstringThe consumer group id.
bootstrapServersstringThe bootstrap servers string used to identify the target broker.
Returns
Remarks
This method works with the mocked Kafka broker only. See UseMockedKafka(SilverbackBuilder, Action<IMockedKafkaOptionsBuilder>?) or AddMockedKafka(BrokerOptionsBuilder, Action<IMockedKafkaOptionsBuilder>?).
Exceptions
- InvalidOperationException
No consumer group with the specified id exists for the specified bootstrap servers.
GetProducer(Action<KafkaProducerConfigurationBuilder>)
Gets a new producer with the specified configuration.
public IProducer GetProducer(Action<KafkaProducerConfigurationBuilder> configurationBuilderAction)
Parameters
configurationBuilderActionAction<KafkaProducerConfigurationBuilder>An Action<T> that takes the KafkaProducerConfigurationBuilder and configures it.
Returns
Exceptions
- BrokerConfigurationException
The producer configuration is not valid.
- InvalidOperationException
The Kafka integration hasn't been added (
AddKafkahasn't been called), the configuration defines more than 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.
GetTopic(string, string?)
Returns the IInMemoryTopic with the specified name.
public IInMemoryTopic GetTopic(string name, string? bootstrapServers = null)
Parameters
namestringThe name of the topic. Must not be empty.
bootstrapServersstringThe bootstrap servers string used to identify the target broker. This must be specified when testing with multiple brokers. If specified, it must not be empty.
Returns
Remarks
This method works with the mocked Kafka broker only. See UseMockedKafka(SilverbackBuilder, Action<IMockedKafkaOptionsBuilder>?) or AddMockedKafka(BrokerOptionsBuilder, Action<IMockedKafkaOptionsBuilder>?).
Exceptions
- InvalidOperationException
The
bootstrapServersaren't specified and either more than one topic with the specified name exists, or the topic doesn't exist and the existing topics don't belong to exactly one broker (for example, because no topic exists yet).
WaitUntilAllMessagesAreCommittedAsync(bool, TimeSpan?)
Returns a ValueTask that completes when the offsets of all messages routed to the consumers have been committed to the mocked Kafka broker.
public ValueTask WaitUntilAllMessagesAreCommittedAsync(bool throwTimeoutException, TimeSpan? timeout = null)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the offsets aren't committed before the timeout elapses.
timeoutTimeSpan?The time to wait for the message offsets to be committed. The default is 30 seconds.
Returns
Remarks
This method works with the mocked Kafka broker only and does not force offset commits.
Exceptions
- TimeoutException
The
throwTimeoutExceptionistrueand the timeout elapsed before all message offsets were committed.
WaitUntilAllMessagesAreCommittedAsync(bool, TimeSpan?, params string[])
Returns a ValueTask that completes when the offsets of all messages routed to the consumers have been committed to the mocked Kafka broker.
public ValueTask WaitUntilAllMessagesAreCommittedAsync(bool throwTimeoutException, TimeSpan? timeout, params string[] endpointNames)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the offsets aren't committed before the timeout elapses.
timeoutTimeSpan?The time to wait for the message offsets to be committed. The default is 30 seconds.
endpointNamesstring[]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
Remarks
This method works with the mocked Kafka broker only and does not force offset commits.
Exceptions
- TimeoutException
The
throwTimeoutExceptionistrueand the timeout elapsed before all message offsets were committed.- InvalidOperationException
One of the
endpointNamesdoesn't match any endpoint.
WaitUntilAllMessagesAreCommittedAsync(bool, params string[])
Returns a ValueTask that completes when the offsets of all messages routed to the consumers have been committed to the mocked Kafka broker.
public ValueTask WaitUntilAllMessagesAreCommittedAsync(bool throwTimeoutException, params string[] endpointNames)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the offsets aren't committed before the timeout elapses.
endpointNamesstring[]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
Remarks
This method works with the mocked Kafka broker only and does not force offset commits.
Exceptions
- TimeoutException
The
throwTimeoutExceptionistrueand the default timeout elapsed before all message offsets were committed.- InvalidOperationException
One of the
endpointNamesdoesn't match any endpoint.
WaitUntilAllMessagesAreCommittedAsync(bool, CancellationToken, params string[])
Returns a ValueTask that completes when the offsets of all messages routed to the consumers have been committed to the mocked Kafka broker.
public ValueTask WaitUntilAllMessagesAreCommittedAsync(bool throwTimeoutException, CancellationToken cancellationToken, params string[] endpointNames)
Parameters
throwTimeoutExceptionboolA value specifying whether a TimeoutException has to be thrown when the offsets aren't committed 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, 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
Remarks
This method works with the mocked Kafka broker only and does not force offset commits.
Exceptions
- TimeoutException
The
throwTimeoutExceptionistrueand thecancellationTokenwas canceled before all message offsets were committed.- InvalidOperationException
One of the
endpointNamesdoesn't match any endpoint.
WaitUntilAllMessagesAreCommittedAsync(TimeSpan?, params string[])
Returns a ValueTask that completes when the offsets of all messages routed to the consumers have been committed to the mocked Kafka broker.
public ValueTask WaitUntilAllMessagesAreCommittedAsync(TimeSpan? timeout, params string[] endpointNames)
Parameters
timeoutTimeSpan?The time to wait for the message offsets to be committed. The default is 30 seconds.
endpointNamesstring[]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
Remarks
This method works with the mocked Kafka broker only and does not force offset commits.
Exceptions
- TimeoutException
The timeout elapsed before all message offsets were committed.
- InvalidOperationException
One of the
endpointNamesdoesn't match any endpoint.
WaitUntilAllMessagesAreCommittedAsync(params string[])
Returns a ValueTask that completes when the offsets of all messages routed to the consumers have been committed to the mocked Kafka broker.
public ValueTask WaitUntilAllMessagesAreCommittedAsync(params string[] endpointNames)
Parameters
endpointNamesstring[]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
Remarks
This method works with the mocked Kafka broker only and does not force offset commits.
Exceptions
- TimeoutException
The default timeout elapsed before all message offsets were committed.
- InvalidOperationException
One of the
endpointNamesdoesn't match any endpoint.
WaitUntilAllMessagesAreCommittedAsync(CancellationToken, params string[])
Returns a ValueTask that completes when the offsets of all messages routed to the consumers have been committed to the mocked Kafka broker.
public ValueTask WaitUntilAllMessagesAreCommittedAsync(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, 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
Remarks
This method works with the mocked Kafka broker only and does not force offset commits.
Exceptions
- TimeoutException
The
cancellationTokenwas canceled before all message offsets were committed.- InvalidOperationException
One of the
endpointNamesdoesn't match any endpoint.
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.