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
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 5.6.1
- fixed
- Fixed an issue with dispatching messages when the Enabled flag is set to false.

## 5.6.0
- Added
- netstandard2.0 target framework.
Expand Down
14 changes: 14 additions & 0 deletions src/Ev.ServiceBus/Dispatch/DispatchSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
/// <inheritdoc />
public async Task SendDispatch(object messagePayload, CancellationToken token = default)
{
if (IsDisabled()) return;

var dispatch = new Abstractions.Dispatch(messagePayload);

await SendDispatch(dispatch, token);
Expand All @@ -50,6 +52,8 @@
/// <inheritdoc />
public async Task SendDispatch(Abstractions.Dispatch messagePayload, CancellationToken token = default)
{
if (IsDisabled()) return;

var dispatches = CreateMessagesToSend([messagePayload]);

foreach (var messagePerResource in dispatches)
Expand All @@ -74,6 +78,8 @@
throw new ArgumentNullException(nameof(messagePayloads));
}

if (IsDisabled()) return;

var dispatches = messagePayloads.Select(o => new Abstractions.Dispatch(o)).ToArray();
await SendDispatches(dispatches, token);
}
Expand All @@ -86,6 +92,8 @@
throw new ArgumentNullException(nameof(messagePayloads));
}

if (IsDisabled()) return;

var dispatches = CreateMessagesToSend(messagePayloads);
foreach (var messagesPerResource in dispatches)
{
Expand Down Expand Up @@ -139,6 +147,8 @@
throw new ArgumentNullException(nameof(messagePayloads));
}

if (IsDisabled()) return;

var dispatches = messagePayloads.Select(o => new Abstractions.Dispatch(o)).ToArray();
await ScheduleDispatches(dispatches, scheduledEnqueueTime, token);
}
Expand All @@ -151,6 +161,8 @@
throw new ArgumentNullException(nameof(messagePayloads));
}

if (IsDisabled()) return;

var dispatches = CreateMessagesToSend(messagePayloads);
foreach (var messagesPerResource in dispatches)
{
Expand Down Expand Up @@ -189,10 +201,10 @@

private class MessagesPerResource
{
public MessageToSend[] Messages { get; set; }

Check warning on line 204 in src/Ev.ServiceBus/Dispatch/DispatchSender.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Messages' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public ClientType ClientType { get; set; }
public string ResourceId { get; set; }

Check warning on line 206 in src/Ev.ServiceBus/Dispatch/DispatchSender.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ResourceId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public IMessageSender Sender { get; set; }

Check warning on line 207 in src/Ev.ServiceBus/Dispatch/DispatchSender.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Sender' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}

private class MessageToSend
Expand Down Expand Up @@ -279,4 +291,6 @@
}
return message;
}

private bool IsDisabled() => _serviceBusOptions.Settings.Enabled == false;
}
171 changes: 171 additions & 0 deletions tests/Ev.ServiceBus.UnitTests/DispatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,159 @@ public async Task ShouldAssignIsolationKeyWhenInIsolationMode()
_sentMessagesToQueue[0].ApplicationProperties.GetIsolationKey().Should().Be(isolationKey);
}

[Fact]
public async Task SendDispatch_Object_WhenDisabled_DoesNotInvokeSender()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await sender.SendDispatch((object)new SubscribedEvent { SomeNumber = 1, SomeString = "test" });
}

provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task SendDispatch_Dispatch_WhenDisabled_DoesNotInvokeSender()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await sender.SendDispatch(new Ev.ServiceBus.Abstractions.Dispatch(new SubscribedEvent { SomeNumber = 1, SomeString = "test" }));
}

provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task SendDispatches_Objects_WhenDisabled_DoesNotInvokeSender()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await sender.SendDispatches(new object[] { new SubscribedEvent { SomeNumber = 1, SomeString = "test" } });
}

provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task SendDispatches_Dispatches_WhenDisabled_DoesNotInvokeSender()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await sender.SendDispatches(new[] { new Ev.ServiceBus.Abstractions.Dispatch(new SubscribedEvent { SomeNumber = 1, SomeString = "test" }) });
}

provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task ScheduleDispatches_Objects_WhenDisabled_DoesNotInvokeSender()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await sender.ScheduleDispatches(
new object[] { new SubscribedEvent { SomeNumber = 1, SomeString = "test" } },
DateTimeOffset.UtcNow.AddDays(1));
}

provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task ScheduleDispatches_Dispatches_WhenDisabled_DoesNotInvokeSender()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await sender.ScheduleDispatches(
new[] { new Ev.ServiceBus.Abstractions.Dispatch(new SubscribedEvent { SomeNumber = 1, SomeString = "test" }) },
DateTimeOffset.UtcNow.AddDays(1));
}

provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
await provider.SimulateStopHost(CancellationToken.None);
}

// Null-argument checks must fire before IsDisabled() for all overloads that own them
[Fact]
public async Task SendDispatches_Objects_WhenDisabled_NullPayload_ThrowsArgumentNullException()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await Assert.ThrowsAsync<ArgumentNullException>(() => sender.SendDispatches((IEnumerable<object>)null!));
}

await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task SendDispatches_Dispatches_WhenDisabled_NullPayload_ThrowsArgumentNullException()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await Assert.ThrowsAsync<ArgumentNullException>(() => sender.SendDispatches((IEnumerable<Ev.ServiceBus.Abstractions.Dispatch>)null!));
}

await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task ScheduleDispatches_Objects_WhenDisabled_NullPayload_ThrowsArgumentNullException()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await Assert.ThrowsAsync<ArgumentNullException>(() =>
sender.ScheduleDispatches((IEnumerable<object>)null!, DateTimeOffset.UtcNow.AddDays(1)));
}

await provider.SimulateStopHost(CancellationToken.None);
}

[Fact]
public async Task ScheduleDispatches_Dispatches_WhenDisabled_NullPayload_ThrowsArgumentNullException()
{
var provider = await CreateDisabledProviderAsync();

using (var scope = provider.CreateScope())
{
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
await Assert.ThrowsAsync<ArgumentNullException>(() =>
sender.ScheduleDispatches((IEnumerable<Ev.ServiceBus.Abstractions.Dispatch>)null!, DateTimeOffset.UtcNow.AddDays(1)));
}

await provider.SimulateStopHost(CancellationToken.None);
}

private void GivenIsolationKeyInMetadata(string isolationKey)
{
var appProperties = new Dictionary<string, object> { { UserProperties.IsolationKey , isolationKey } };
Expand All @@ -601,6 +754,24 @@ private ServiceBusMessage GetMessageFrom(string clientToCheck)
return _sentMessagesToQueue.FirstOrDefault();
}

private static async Task<ServiceProvider> CreateDisabledProviderAsync()
{
var services = new ServiceCollection();
services.AddServiceBus(settings =>
{
settings.Enabled = false;
settings.WithConnection("Endpoint=testConnectionString;", new ServiceBusClientOptions());
});
services.OverrideClientFactory();
services.RegisterServiceBusDispatch().ToQueue("myQueue", builder =>
{
builder.RegisterDispatch<SubscribedEvent>();
});
var provider = services.BuildServiceProvider();
await provider.SimulateStartHost(CancellationToken.None);
return provider;
}

public void Dispose()
{
_composer?.Dispose();
Expand Down
Loading