Skip to content

Commit a8b0fb6

Browse files
fix(EventHubs): Support default consumer group name (#1432)
Co-authored-by: Andre Hofmeister <[email protected]>
1 parent 70bece4 commit a8b0fb6

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/Testcontainers.EventHubs/EventHubsServiceConfiguration.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ private EventHubsServiceConfiguration(NamespaceConfig namespaceConfig)
5858

5959
public static EventHubsServiceConfiguration Create()
6060
{
61-
var namespaceConfig = new NamespaceConfig("EventHub", "ns-1", Array.Empty<Entity>());
61+
var namespaceConfig = new NamespaceConfig("EventHub", "emulatorns1", Array.Empty<Entity>());
6262
return new EventHubsServiceConfiguration(namespaceConfig);
6363
}
6464

6565
public EventHubsServiceConfiguration WithEntity(string name, int partitionCount, params string[] consumerGroupNames)
6666
{
67-
return WithEntity(name, partitionCount, new ReadOnlyCollection<string>(consumerGroupNames));
67+
// Filter out the consumer group name `$default` because the `$default` group
68+
// is created automatically by the container image.
69+
var validConsumerGroupNames = consumerGroupNames.Where(consumerGroupName => !"$default".Equals(consumerGroupName, StringComparison.OrdinalIgnoreCase)).ToList();
70+
return WithEntity(name, partitionCount, new ReadOnlyCollection<string>(validConsumerGroupNames));
6871
}
6972

7073
public EventHubsServiceConfiguration WithEntity(string name, int partitionCount, IEnumerable<string> consumerGroupNames)
@@ -77,8 +80,10 @@ public EventHubsServiceConfiguration WithEntity(string name, int partitionCount,
7780

7881
public bool Validate()
7982
{
80-
Predicate<Entity> isValidEntity = entity => entity.PartitionCount > 0 && entity.PartitionCount <= 32 && entity.ConsumerGroups.Count > 0 && entity.ConsumerGroups.Count <= 20;
81-
return _namespaceConfig.Entities.All(entity => isValidEntity(entity));
83+
// The emulator provides the usage quotas as described at:
84+
// https://learn.microsoft.com/en-us/azure/event-hubs/overview-emulator#usage-quotas.
85+
Predicate<Entity> isValidEntity = entity => entity.PartitionCount > 0 && entity.PartitionCount <= 32 && entity.ConsumerGroups.Count >= 0 && entity.ConsumerGroups.Count <= 20;
86+
return _namespaceConfig.Entities.Count > 0 && _namespaceConfig.Entities.Count <= 10 && _namespaceConfig.Entities.All(entity => isValidEntity(entity));
8287
}
8388

8489
public string Build()

tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Task DisposeAsync()
2626

2727
private static EventHubsServiceConfiguration GetServiceConfiguration()
2828
{
29-
return EventHubsServiceConfiguration.Create().WithEntity(EventHubsName, 2, EventHubsConsumerGroupName);
29+
return EventHubsServiceConfiguration.Create().WithEntity(EventHubsName, 2, [EventHubConsumerClient.DefaultConsumerGroupName, EventHubsConsumerGroupName]);
3030
}
3131

3232
[Fact]
@@ -36,22 +36,27 @@ public async Task SendEventDataBatchShouldNotThrowException()
3636
// Given
3737
var message = Guid.NewGuid().ToString();
3838

39-
await using var client = new EventHubProducerClient(_eventHubsContainer.GetConnectionString(), EventHubsName);
39+
var readOptions = new ReadEventOptions();
40+
readOptions.MaximumWaitTime = TimeSpan.FromSeconds(5);
4041

41-
// When
42-
var properties = await client.GetEventHubPropertiesAsync()
43-
.ConfigureAwait(true);
42+
await using var producer = new EventHubProducerClient(_eventHubsContainer.GetConnectionString(), EventHubsName);
43+
44+
await using var consumer = new EventHubConsumerClient(EventHubsConsumerGroupName, _eventHubsContainer.GetConnectionString(), EventHubsName);
4445

45-
using var eventDataBatch = await client.CreateBatchAsync()
46+
// When
47+
using var eventDataBatch = await producer.CreateBatchAsync()
4648
.ConfigureAwait(true);
4749

48-
eventDataBatch.TryAdd(new EventData(message));
50+
_ = eventDataBatch.TryAdd(new EventData(message));
4951

50-
await client.SendAsync(eventDataBatch)
52+
await producer.SendAsync(eventDataBatch)
5153
.ConfigureAwait(true);
5254

55+
var asyncEnumerator = consumer.ReadEventsAsync(readOptions).GetAsyncEnumerator();
56+
_ = await asyncEnumerator.MoveNextAsync();
57+
5358
// Then
54-
Assert.NotNull(properties);
59+
Assert.Equal(message, Encoding.UTF8.GetString(asyncEnumerator.Current.Data.Body.Span));
5560
}
5661
// # --8<-- [end:UseEventHubsContainer]
5762

tests/Testcontainers.EventHubs.Tests/Usings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
global using System;
2+
global using System.Text;
23
global using System.Threading.Tasks;
34
global using Azure.Messaging.EventHubs;
5+
global using Azure.Messaging.EventHubs.Consumer;
46
global using Azure.Messaging.EventHubs.Producer;
57
global using DotNet.Testcontainers.Builders;
68
global using DotNet.Testcontainers.Commons;

0 commit comments

Comments
 (0)