Skip to content

Commit aee785d

Browse files
committed
tests(WIP): eventhubs kafka usage example
1 parent e9df035 commit aee785d

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

docs/modules/eventhubs.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle
3333
```csharp
3434
--8<-- "tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs:EventHubsUsage"
3535
```
36+
37+
=== "Kafka support"
38+
39+
Azure Event Hubs is compatible with Apache Kafka. You can use the Azure Event Hubs Kafka endpoint to connect to the Event Hubs instance.
40+
The following example demonstrates how to use the Azure Event Hubs Kafka endpoint with the Testcontainers library. Please,
41+
keep in mind that only consumer and producer API is supported. More information about known limitations can be found [here](https://learn.microsoft.com/en-us/azure/event-hubs/overview-emulator#known-limitations).
42+
43+
```csharp
44+
--8<-- "tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs:EventHubsKafkaUsage"
45+
```

tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,59 @@ await client.SendAsync(eventDataBatch)
5555
Assert.NotNull(properties);
5656
}
5757
// # --8<-- [end:EventHubsUsage]
58+
59+
// # --8<-- [start:EventHubsKafkaUsage]
60+
61+
[Fact]
62+
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
63+
public async Task UsingKafkaShouldNotThrowException()
64+
{
65+
// Given
66+
var kafkaPort = _eventHubsContainer.GetMappedPublicPort(EventHubsBuilder.KafkaPort);
67+
var bootstrapServer = $"localhost:{kafkaPort}";
68+
var connectionString = _eventHubsContainer.GetConnectionString();
69+
70+
var producerConfig = new ProducerConfig
71+
{
72+
BootstrapServers = bootstrapServer,
73+
SecurityProtocol = SecurityProtocol.SaslPlaintext,
74+
SaslMechanism = SaslMechanism.Plain,
75+
SaslUsername = "$ConnectionString",
76+
SaslPassword = connectionString
77+
};
78+
79+
var consumerConfig = new ConsumerConfig
80+
{
81+
BootstrapServers = bootstrapServer,
82+
SecurityProtocol = SecurityProtocol.SaslPlaintext,
83+
SaslMechanism = SaslMechanism.Plain,
84+
SaslUsername = "$ConnectionString",
85+
SaslPassword = connectionString,
86+
GroupId = EventHubsConsumerGroupName,
87+
AutoOffsetReset = AutoOffsetReset.Earliest,
88+
};
89+
90+
var message = new Message<string, string>
91+
{
92+
Value = Guid.NewGuid().ToString("D"),
93+
};
94+
95+
// When
96+
using var producer = new ProducerBuilder<string, string>(producerConfig).Build();
97+
_ = await producer.ProduceAsync(EventHubsName, message)
98+
.ConfigureAwait(true);
99+
100+
using var consumer = new ConsumerBuilder<string, string>(consumerConfig).Build();
101+
consumer.Subscribe(EventHubsName);
102+
103+
var result = consumer.Consume(TimeSpan.FromSeconds(15));
104+
105+
// Then
106+
Assert.NotNull(result);
107+
Assert.Equal(message.Value, result.Message.Value);
108+
}
109+
110+
// # --8<-- [end:EventHubsKafkaUsage]
58111

59112
// # --8<-- [start:MinimalConfigurationEventHubs]
60113
[UsedImplicitly]

tests/Testcontainers.EventHubs.Tests/Testcontainers.EventHubs.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="xunit.runner.visualstudio"/>
1111
<PackageReference Include="xunit"/>
1212
<PackageReference Include="Azure.Messaging.EventHubs"/>
13+
<PackageReference Include="Confluent.Kafka"/>
1314
</ItemGroup>
1415
<ItemGroup>
1516
<ProjectReference Include="../../src/Testcontainers.EventHubs/Testcontainers.EventHubs.csproj"/>

tests/Testcontainers.EventHubs.Tests/Usings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
global using System;
2-
global using System.Text;
32
global using System.Threading.Tasks;
43
global using Azure.Messaging.EventHubs;
54
global using Azure.Messaging.EventHubs.Producer;
5+
global using Confluent.Kafka;
66
global using DotNet.Testcontainers.Builders;
77
global using DotNet.Testcontainers.Commons;
88
global using DotNet.Testcontainers.Networks;

0 commit comments

Comments
 (0)