Skip to content

Commit

Permalink
Add tests for ServiceBus publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rickykaare committed Nov 17, 2024
1 parent b1bc56e commit 789c589
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 66 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
using System.Collections.Concurrent;
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
using Microsoft.Extensions.Options;

namespace Cabazure.Messaging.ServiceBus.Internal;

public interface IServiceBusClientProvider
{
ServiceBusClient GetClient(
string? connectionName = null);
}

public class ServiceBusClientProvider(
IOptionsMonitor<CabazureServiceBusOptions> options)
: IServiceBusClientProvider
, IAsyncDisposable
{
private sealed record ClientKey(string? Connection);
private readonly ConcurrentDictionary<ClientKey, ServiceBusClient> clients = new();
private readonly ConcurrentDictionary<ClientKey, ServiceBusAdministrationClient> adminClients = new();

public ServiceBusClient GetClient(
string? connectionName)
=> clients.GetOrAdd(
new(connectionName),
CreateClient);

public ServiceBusAdministrationClient GetAdminClient(
string? connectionName)
=> adminClients.GetOrAdd(
new(connectionName),
CreateAdminClient);

private ServiceBusClient CreateClient(
ClientKey key)
=> options.Get(key.Connection) switch
{
{ FullyQualifiedNamespace: { } n, Credential: { } c } => new(n, c),
{ ConnectionString: { } cs } => new(cs),
_ => throw new ArgumentException(
$"Unknown connection name `{key.Connection}`")
};

private ServiceBusAdministrationClient CreateAdminClient(
ClientKey key)
=> options.Get(key.Connection) switch
{
{ FullyQualifiedNamespace: { } n, Credential: { } c } => new(n, c),
{ ConnectionString: { } cs } => new(cs),
_ => throw new ArgumentException(
$"Unknown connection name `{key.Connection}`")
$"Missing configuration for Service Bus connection `{key.Connection}`")
};

public async ValueTask DisposeAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Collections.Concurrent;
using Azure.Messaging.ServiceBus;
using Microsoft.Extensions.Options;

namespace Cabazure.Messaging.ServiceBus.Internal;

public interface IServiceBusSenderProvider
{
ServiceBusSender GetSender<TMessage>(
string? connectionName = null);
}

public class ServiceBusSenderProvider(
IOptionsMonitor<CabazureServiceBusOptions> options,
IEnumerable<ServiceBusPublisherRegistration> registrations,
IServiceBusClientProvider clientProvider)
: IServiceBusSenderProvider
Expand All @@ -25,8 +29,6 @@ public ServiceBusSender GetSender<TMessage>(
$"Type {typeof(TMessage).Name} not configured as a ServiceBus publisher");
}

var config = options.Get(connectionName);

return senders.GetOrAdd(
new(connectionName, publisher.TopicOrQueueName),
key => clientProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ namespace Cabazure.Messaging.EventHub.Tests.Internal;

public class EventHubProducerClientFactoryTests
{
private const string EventHubConnectionString = "Endpoint=sb://localhost;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;";

[Theory, AutoNSubstituteData]
public void Create_Throws_If_No_Options(
[Frozen] IOptionsMonitor<CabazureEventHubOptions> monitor,
EventHubProducerClientFactory sut,
string connectionName,
string topicName)
string eventHubName)
{
FluentActions
.Invoking(() =>
sut.Create(connectionName, topicName))
sut.Create(connectionName, eventHubName))
.Should()
.Throw<ArgumentException>()
.WithMessage(
Expand All @@ -34,65 +32,92 @@ public void Create_Gets_Options(
[Frozen] IOptionsMonitor<CabazureEventHubOptions> monitor,
EventHubProducerClientFactory sut,
string connectionName,
string topicName)
string eventHubName)
{
monitor.Get(default).ReturnsForAnyArgs(options);

sut.Create(
connectionName,
topicName);
eventHubName);

monitor.Received(1).Get(connectionName);
}

[Theory, AutoNSubstituteData]
public void Creates_Client_From_ConnectionString(
public void Creates_Client_Returns_Client(
[Frozen, NoAutoProperties]
JsonSerializerOptions serializerOptions,
CabazureEventHubOptions options,
[Frozen] IOptionsMonitor<CabazureEventHubOptions> monitor,
EventHubProducerClientFactory sut,
string fullyQualifiedNamespace,
TokenCredential credential,
string connectionName,
string eventHubName)
{
monitor.Get(default).ReturnsForAnyArgs(options);

var client = sut.Create(
connectionName,
eventHubName);

client
.Should()
.BeOfType<EventHubProducerClient>();
}

[Theory, AutoNSubstituteData]
public void Creates_Client_From_Namespace_And_Credential(
[Frozen, NoAutoProperties]
JsonSerializerOptions serializerOptions,
[Frozen] IOptionsMonitor<CabazureEventHubOptions> monitor,
EventHubProducerClientFactory sut,
string fqns,
TokenCredential credential,
string connectionName,
string topicName)
string eventHubName)
{
var options = new CabazureEventHubOptions
{
ConnectionString = EventHubConnectionString,
FullyQualifiedNamespace = fqns,
Credential = credential,
};
monitor.Get(default).ReturnsForAnyArgs(options);

var client = sut.Create(
connectionName,
topicName);
eventHubName);

client
client.FullyQualifiedNamespace
.Should()
.BeOfType<EventHubProducerClient>();
.Be(fqns);
}

[Theory, AutoNSubstituteData]
public void Creates_Client_From_Namespace_And_Credential(
public void Creates_Uses_Namespace_From_ConnectionString_In_Options(
[Frozen, NoAutoProperties]
JsonSerializerOptions serializerOptions,
[Frozen] IOptionsMonitor<CabazureEventHubOptions> monitor,
EventHubProducerClientFactory sut,
string fullyQualifiedNamespace,
TokenCredential credential,
string connectionName,
string topicName)
string fqns,
string eventHubName)
{
var options = new CabazureEventHubOptions
{
FullyQualifiedNamespace = fullyQualifiedNamespace,
Credential = credential,
ConnectionString =
$"Endpoint=sb://{fqns};" +
$"SharedAccessKeyName=RootManageSharedAccessKey;" +
$"SharedAccessKey=SAS_KEY_VALUE;",
};
monitor.Get(default).ReturnsForAnyArgs(options);

var client = sut.Create(
connectionName,
topicName);
eventHubName);

client
client.FullyQualifiedNamespace
.Should()
.BeOfType<EventHubProducerClient>();
.Be(fqns);
}
}
Loading

0 comments on commit 789c589

Please sign in to comment.