This library provides in-memory SDK for Azure Event Hubs which can be used as a drop-in replacement for the official Azure.Messaging.ServiceBus in your tests.
Recommended Usage | Fault Injection | Features | Available APIs | Fluent Assertions
Tip
See the whole In-Memory Azure Test SDK suite if you are interested in other Azure services.
To get started, add Datamole.InMemory.Azure.EventHubs
package to your project.
dotnet add Datamole.InMemory.Azure.ServiceBus
Create custom interface for creating the SDK clients. This interface should be as specific for given use-case as possible to make it simple as possible. Also, provide default implementation that is creating real Azure SDK clients:
interface IServiceBusClientFactory
{
ServiceBusSender CreateSender(string namespaceHostname, string queueOrTopicName);
}
class ServiceBusClientFactory(TokenCredential tokenCredential): IEventHubClientFactory
{
public ServiceBusSender CreateSender(string namespaceHostname, string queueOrTopicName) => ... ;
}
Use this interface to obtain ServiceBus clients in tested code:
class ExampleService(IServiceBusClientFactory clientFactory)
{
public async Task SendMessageAsync(string namespaceHostname, string queueName, BinaryData payload)
{
var client = clientFactory.CreateSender(namespaceHostname, queueName);
await client.SendMessageAsync(new ServiceBusMessage(payload));
}
}
Provide implementation of IServiceBusClientFactory
that is creating the in-memory clients:
class InMemoryServiceBusClientFactory(InMemoryServiceBusProvider provider) : IServiceBusClientFactory
{
public ServiceBusSender CreateSender(string namespaceHostname, string queueOrTopicName)
{
return new InMemoryServiceBusClient(namespaceHostname, provider).CreateSender(queueOrTopicName);
}
}
When testing, it is now enough to initialize InMemoryServiceBusProvider
and inject InMemoryServiceBusClientFactory
to the tested code (e.g. via Dependency Injection):
var serviceBusProvider = new InMemoryServiceBusProvider();
var queue = serviceBusProvider.AddNamespace().AddQueue("my-queue");
var services = new ServiceCollection();
services.AddSingleton<ExampleService>();
services.AddSingleton(serviceBusProvider);
services.AddSingleton<IServiceBusClientFactory, InMemoryServiceBusClientFactory>();
var exampleService = services.BuildServiceProvider().GetRequiredService<ExampleService>();
var payload = BinaryData.FromString("test-data");
await exampleService.SendMessageAsync(queue.Namespace.FullyQualifiedNamespace, queue.QueueName, payload);
var receiver = new InMemoryServiceBusClient(queue.Namespace.FullyQualifiedNamespace, serviceBusProvider)
.CreateReceiver(queue.QueueName);
var message = await receiver.ReceiveMessageAsync();
message.Body.ToString().Should().Be("test-data");
Fault injections are currently not supported for Azure Service Bus.
- Queues & topics.
- Sessions.
PeekLock
&ReceiveAndDelete
receive modes.- Client methods explicitly enumerated below are supported and all properties are supported.
Following features are not supported (behavior when using these features is undefined - it might throw an exception or ignore some parameters):
- Deferred messages.
- Scheduled messages.
- Dead-letter queues.
- Processors & rules managers.
CreateSender(...)
CreateReceiver(...)
AcceptNextSessionAsync(...)
AcceptSessionAsync(...)
DisposeAsync(...)
ReceiveMessagesAsync(...)
ReceiveMessageAsync()
AbandonMessageAsync(...)
CompleteMessageAsync(...)
RenewMessageLockAsync(...)
DisposeAsync(...)
CloseAsync(...)
ReceiveMessagesAsync(...)
ReceiveMessageAsync()
AbandonMessageAsync(...)
CompleteMessageAsync(...)
RenewMessageLockAsync(...)
RenewSessionLockAsync(...)
GetSessionStateAsync(...)
SetSessionStateAsync(...)
DisposeAsync(...)
CloseAsync(...)
SendMessageAsync(...)
SendMessagesAsync(...)
CreateMessageBatchAsync(...)
DisposeAsync(...)
CloseAsync(...)
There are following assertions available for in-memory service bus types:
BeEmptyAsync()
BeEmptyAsync()