Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Testcontainers.EventHubs/EventHubsServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ private EventHubsServiceConfiguration(NamespaceConfig namespaceConfig)

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

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

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

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

public string Build()
Expand Down
23 changes: 14 additions & 9 deletions tests/Testcontainers.EventHubs.Tests/EventHubsContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Task DisposeAsync()

private static EventHubsServiceConfiguration GetServiceConfiguration()
{
return EventHubsServiceConfiguration.Create().WithEntity(EventHubsName, 2, EventHubsConsumerGroupName);
return EventHubsServiceConfiguration.Create().WithEntity(EventHubsName, 2, [EventHubConsumerClient.DefaultConsumerGroupName, EventHubsConsumerGroupName]);
}

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

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

// When
var properties = await client.GetEventHubPropertiesAsync()
.ConfigureAwait(true);
await using var producer = new EventHubProducerClient(_eventHubsContainer.GetConnectionString(), EventHubsName);

await using var consumer = new EventHubConsumerClient(EventHubsConsumerGroupName, _eventHubsContainer.GetConnectionString(), EventHubsName);

using var eventDataBatch = await client.CreateBatchAsync()
// When
using var eventDataBatch = await producer.CreateBatchAsync()
.ConfigureAwait(true);

eventDataBatch.TryAdd(new EventData(message));
_ = eventDataBatch.TryAdd(new EventData(message));

await client.SendAsync(eventDataBatch)
await producer.SendAsync(eventDataBatch)
.ConfigureAwait(true);

var asyncEnumerator = consumer.ReadEventsAsync(readOptions).GetAsyncEnumerator();
_ = await asyncEnumerator.MoveNextAsync();

// Then
Assert.NotNull(properties);
Assert.Equal(message, Encoding.UTF8.GetString(asyncEnumerator.Current.Data.Body.Span));
}
// # --8<-- [end:UseEventHubsContainer]

Expand Down
2 changes: 2 additions & 0 deletions tests/Testcontainers.EventHubs.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
global using System;
global using System.Text;
global using System.Threading.Tasks;
global using Azure.Messaging.EventHubs;
global using Azure.Messaging.EventHubs.Consumer;
global using Azure.Messaging.EventHubs.Producer;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Commons;
Expand Down