Class KafkaTestingHelper
Exposes some helper methods and shortcuts to simplify testing.
Inherited Members
Namespace: Silverback.Testing
Assembly: Silverback.Integration.Kafka.Testing.dll
Syntax
public class KafkaTestingHelper : TestingHelper, IKafkaTestingHelper, ITestingHelper
Constructors
KafkaTestingHelper(IServiceProvider, ILogger<KafkaTestingHelper>)
Initializes a new instance of the KafkaTestingHelper class.
Declaration
public KafkaTestingHelper(IServiceProvider serviceProvider, ILogger<KafkaTestingHelper> logger)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | serviceProvider | The IServiceProvider. |
| ILogger<KafkaTestingHelper> | logger | The ISilverbackLogger. |
Properties
ConsumerGroups
Gets a collection of IMockedConsumerGroup representing all known consumer groups.
Declaration
public IReadOnlyCollection<IMockedConsumerGroup> ConsumerGroups { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyCollection<IMockedConsumerGroup> | The collection of IMockedConsumerGroup. |
Methods
GetConsumerGroup(string)
Returns the IMockedConsumerGroup representing the consumer group with the specified id.
Declaration
public IMockedConsumerGroup GetConsumerGroup(string groupId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | groupId | The consumer group id. |
Returns
| Type | Description |
|---|---|
| IMockedConsumerGroup | The IMockedConsumerGroup. |
Remarks
This method works with the mocked Kafka broker only. See UseMockedKafka(SilverbackBuilder, Action<IMockedKafkaOptionsBuilder>?) or AddMockedKafka(BrokerOptionsBuilder, Action<IMockedKafkaOptionsBuilder>?).
GetConsumerGroup(string, string)
Returns the IMockedConsumerGroup representing the consumer group with the specified id.
Declaration
public IMockedConsumerGroup GetConsumerGroup(string groupId, string bootstrapServers)
Parameters
| Type | Name | Description |
|---|---|---|
| string | groupId | The consumer group id. |
| string | bootstrapServers | The bootstrap servers string used to identify the target broker. |
Returns
| Type | Description |
|---|---|
| IMockedConsumerGroup | The IMockedConsumerGroup. |
Remarks
This method works with the mocked Kafka broker only. See UseMockedKafka(SilverbackBuilder, Action<IMockedKafkaOptionsBuilder>?) or AddMockedKafka(BrokerOptionsBuilder, Action<IMockedKafkaOptionsBuilder>?).
GetProducer(Action<KafkaProducerConfigurationBuilder>)
Gets a new producer with the specified configuration.
Declaration
public IProducer GetProducer(Action<KafkaProducerConfigurationBuilder> configurationBuilderAction)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<KafkaProducerConfigurationBuilder> | configurationBuilderAction | An Action<T> that takes the KafkaProducerConfigurationBuilder and configures it. |
Returns
| Type | Description |
|---|---|
| IProducer | The IProducer. |
GetProducerForConsumer(IConsumerCollection, string)
Gets an existing producer for the endpoint consumed by the specified consumer or initializes a new one mirroring the consumer configuration.
Declaration
protected override IProducer? GetProducerForConsumer(IConsumerCollection consumers, string endpointName)
Parameters
| Type | Name | Description |
|---|---|---|
| IConsumerCollection | consumers | The existing consumers. |
| string | endpointName | The endpoint name. It could be either the topic/queue name or the friendly name. |
Returns
| Type | Description |
|---|---|
| IProducer | The IProducer. |
Overrides
GetTopic(string, string?)
Returns the IInMemoryTopic with the specified name.
Declaration
public IInMemoryTopic GetTopic(string name, string? bootstrapServers = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The name of the topic. |
| string | bootstrapServers | The bootstrap servers string used to identify the target broker. This must be specified when testing with multiple brokers. |
Returns
| Type | Description |
|---|---|
| IInMemoryTopic | The IInMemoryTopic. |
Remarks
This method works with the mocked Kafka broker only. See UseMockedKafka(SilverbackBuilder, Action<IMockedKafkaOptionsBuilder>?) or AddMockedKafka(BrokerOptionsBuilder, Action<IMockedKafkaOptionsBuilder>?).
WaitUntilAllMessagesAreConsumedCoreAsync(IReadOnlyCollection<string>, CancellationToken)
Returns a ValueTask that completes when all messages routed to the consumers have been processed and committed.
Declaration
protected override Task WaitUntilAllMessagesAreConsumedCoreAsync(IReadOnlyCollection<string> endpointNames, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyCollection<string> | endpointNames | The names of the endpoints to wait for. If not specified, all endpoints are considered. |
| CancellationToken | cancellationToken | A CancellationToken to observe while waiting for the task to complete. |
Returns
| Type | Description |
|---|---|
| Task | A ValueTask that completes when all messages have been processed. |
Overrides
Remarks
This method works with the mocked brokers only.