Skip to content

Commit b1d553d

Browse files
authored
Prevent dispatch when Service Bus is disabled (#110)
Added checks to ensure no messages are sent when the Service Bus is disabled. Included tests to verify correct behavior and updated the changelog.
1 parent e0bcf5f commit b1d553d

3 files changed

Lines changed: 189 additions & 0 deletions

File tree

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 5.6.1
8+
- fixed
9+
- Fixed an issue with dispatching messages when the Enabled flag is set to false.
10+
711
## 5.6.0
812
- Added
913
- netstandard2.0 target framework.

src/Ev.ServiceBus/Dispatch/DispatchSender.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public DispatchSender(
4242
/// <inheritdoc />
4343
public async Task SendDispatch(object messagePayload, CancellationToken token = default)
4444
{
45+
if (IsDisabled()) return;
46+
4547
var dispatch = new Abstractions.Dispatch(messagePayload);
4648

4749
await SendDispatch(dispatch, token);
@@ -50,6 +52,8 @@ public async Task SendDispatch(object messagePayload, CancellationToken token =
5052
/// <inheritdoc />
5153
public async Task SendDispatch(Abstractions.Dispatch messagePayload, CancellationToken token = default)
5254
{
55+
if (IsDisabled()) return;
56+
5357
var dispatches = CreateMessagesToSend([messagePayload]);
5458

5559
foreach (var messagePerResource in dispatches)
@@ -74,6 +78,8 @@ public async Task SendDispatches(IEnumerable<object> messagePayloads, Cancellati
7478
throw new ArgumentNullException(nameof(messagePayloads));
7579
}
7680

81+
if (IsDisabled()) return;
82+
7783
var dispatches = messagePayloads.Select(o => new Abstractions.Dispatch(o)).ToArray();
7884
await SendDispatches(dispatches, token);
7985
}
@@ -86,6 +92,8 @@ public async Task SendDispatches(IEnumerable<Abstractions.Dispatch> messagePaylo
8692
throw new ArgumentNullException(nameof(messagePayloads));
8793
}
8894

95+
if (IsDisabled()) return;
96+
8997
var dispatches = CreateMessagesToSend(messagePayloads);
9098
foreach (var messagesPerResource in dispatches)
9199
{
@@ -139,6 +147,8 @@ public async Task ScheduleDispatches(IEnumerable<object> messagePayloads, DateTi
139147
throw new ArgumentNullException(nameof(messagePayloads));
140148
}
141149

150+
if (IsDisabled()) return;
151+
142152
var dispatches = messagePayloads.Select(o => new Abstractions.Dispatch(o)).ToArray();
143153
await ScheduleDispatches(dispatches, scheduledEnqueueTime, token);
144154
}
@@ -151,6 +161,8 @@ public async Task ScheduleDispatches(IEnumerable<Abstractions.Dispatch> messageP
151161
throw new ArgumentNullException(nameof(messagePayloads));
152162
}
153163

164+
if (IsDisabled()) return;
165+
154166
var dispatches = CreateMessagesToSend(messagePayloads);
155167
foreach (var messagesPerResource in dispatches)
156168
{
@@ -279,4 +291,6 @@ private ServiceBusMessage CreateMessage(
279291
}
280292
return message;
281293
}
294+
295+
private bool IsDisabled() => _serviceBusOptions.Settings.Enabled == false;
282296
}

tests/Ev.ServiceBus.UnitTests/DispatchTest.cs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,159 @@ public async Task ShouldAssignIsolationKeyWhenInIsolationMode()
582582
_sentMessagesToQueue[0].ApplicationProperties.GetIsolationKey().Should().Be(isolationKey);
583583
}
584584

585+
[Fact]
586+
public async Task SendDispatch_Object_WhenDisabled_DoesNotInvokeSender()
587+
{
588+
var provider = await CreateDisabledProviderAsync();
589+
590+
using (var scope = provider.CreateScope())
591+
{
592+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
593+
await sender.SendDispatch((object)new SubscribedEvent { SomeNumber = 1, SomeString = "test" });
594+
}
595+
596+
provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
597+
await provider.SimulateStopHost(CancellationToken.None);
598+
}
599+
600+
[Fact]
601+
public async Task SendDispatch_Dispatch_WhenDisabled_DoesNotInvokeSender()
602+
{
603+
var provider = await CreateDisabledProviderAsync();
604+
605+
using (var scope = provider.CreateScope())
606+
{
607+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
608+
await sender.SendDispatch(new Ev.ServiceBus.Abstractions.Dispatch(new SubscribedEvent { SomeNumber = 1, SomeString = "test" }));
609+
}
610+
611+
provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
612+
await provider.SimulateStopHost(CancellationToken.None);
613+
}
614+
615+
[Fact]
616+
public async Task SendDispatches_Objects_WhenDisabled_DoesNotInvokeSender()
617+
{
618+
var provider = await CreateDisabledProviderAsync();
619+
620+
using (var scope = provider.CreateScope())
621+
{
622+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
623+
await sender.SendDispatches(new object[] { new SubscribedEvent { SomeNumber = 1, SomeString = "test" } });
624+
}
625+
626+
provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
627+
await provider.SimulateStopHost(CancellationToken.None);
628+
}
629+
630+
[Fact]
631+
public async Task SendDispatches_Dispatches_WhenDisabled_DoesNotInvokeSender()
632+
{
633+
var provider = await CreateDisabledProviderAsync();
634+
635+
using (var scope = provider.CreateScope())
636+
{
637+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
638+
await sender.SendDispatches(new[] { new Ev.ServiceBus.Abstractions.Dispatch(new SubscribedEvent { SomeNumber = 1, SomeString = "test" }) });
639+
}
640+
641+
provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
642+
await provider.SimulateStopHost(CancellationToken.None);
643+
}
644+
645+
[Fact]
646+
public async Task ScheduleDispatches_Objects_WhenDisabled_DoesNotInvokeSender()
647+
{
648+
var provider = await CreateDisabledProviderAsync();
649+
650+
using (var scope = provider.CreateScope())
651+
{
652+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
653+
await sender.ScheduleDispatches(
654+
new object[] { new SubscribedEvent { SomeNumber = 1, SomeString = "test" } },
655+
DateTimeOffset.UtcNow.AddDays(1));
656+
}
657+
658+
provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
659+
await provider.SimulateStopHost(CancellationToken.None);
660+
}
661+
662+
[Fact]
663+
public async Task ScheduleDispatches_Dispatches_WhenDisabled_DoesNotInvokeSender()
664+
{
665+
var provider = await CreateDisabledProviderAsync();
666+
667+
using (var scope = provider.CreateScope())
668+
{
669+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
670+
await sender.ScheduleDispatches(
671+
new[] { new Ev.ServiceBus.Abstractions.Dispatch(new SubscribedEvent { SomeNumber = 1, SomeString = "test" }) },
672+
DateTimeOffset.UtcNow.AddDays(1));
673+
}
674+
675+
provider.GetRequiredService<FakeClientFactory>().GetSenderMock("myQueue").Should().BeNull();
676+
await provider.SimulateStopHost(CancellationToken.None);
677+
}
678+
679+
// Null-argument checks must fire before IsDisabled() for all overloads that own them
680+
[Fact]
681+
public async Task SendDispatches_Objects_WhenDisabled_NullPayload_ThrowsArgumentNullException()
682+
{
683+
var provider = await CreateDisabledProviderAsync();
684+
685+
using (var scope = provider.CreateScope())
686+
{
687+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
688+
await Assert.ThrowsAsync<ArgumentNullException>(() => sender.SendDispatches((IEnumerable<object>)null!));
689+
}
690+
691+
await provider.SimulateStopHost(CancellationToken.None);
692+
}
693+
694+
[Fact]
695+
public async Task SendDispatches_Dispatches_WhenDisabled_NullPayload_ThrowsArgumentNullException()
696+
{
697+
var provider = await CreateDisabledProviderAsync();
698+
699+
using (var scope = provider.CreateScope())
700+
{
701+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
702+
await Assert.ThrowsAsync<ArgumentNullException>(() => sender.SendDispatches((IEnumerable<Ev.ServiceBus.Abstractions.Dispatch>)null!));
703+
}
704+
705+
await provider.SimulateStopHost(CancellationToken.None);
706+
}
707+
708+
[Fact]
709+
public async Task ScheduleDispatches_Objects_WhenDisabled_NullPayload_ThrowsArgumentNullException()
710+
{
711+
var provider = await CreateDisabledProviderAsync();
712+
713+
using (var scope = provider.CreateScope())
714+
{
715+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
716+
await Assert.ThrowsAsync<ArgumentNullException>(() =>
717+
sender.ScheduleDispatches((IEnumerable<object>)null!, DateTimeOffset.UtcNow.AddDays(1)));
718+
}
719+
720+
await provider.SimulateStopHost(CancellationToken.None);
721+
}
722+
723+
[Fact]
724+
public async Task ScheduleDispatches_Dispatches_WhenDisabled_NullPayload_ThrowsArgumentNullException()
725+
{
726+
var provider = await CreateDisabledProviderAsync();
727+
728+
using (var scope = provider.CreateScope())
729+
{
730+
var sender = scope.ServiceProvider.GetRequiredService<IDispatchSender>();
731+
await Assert.ThrowsAsync<ArgumentNullException>(() =>
732+
sender.ScheduleDispatches((IEnumerable<Ev.ServiceBus.Abstractions.Dispatch>)null!, DateTimeOffset.UtcNow.AddDays(1)));
733+
}
734+
735+
await provider.SimulateStopHost(CancellationToken.None);
736+
}
737+
585738
private void GivenIsolationKeyInMetadata(string isolationKey)
586739
{
587740
var appProperties = new Dictionary<string, object> { { UserProperties.IsolationKey , isolationKey } };
@@ -601,6 +754,24 @@ private ServiceBusMessage GetMessageFrom(string clientToCheck)
601754
return _sentMessagesToQueue.FirstOrDefault();
602755
}
603756

757+
private static async Task<ServiceProvider> CreateDisabledProviderAsync()
758+
{
759+
var services = new ServiceCollection();
760+
services.AddServiceBus(settings =>
761+
{
762+
settings.Enabled = false;
763+
settings.WithConnection("Endpoint=testConnectionString;", new ServiceBusClientOptions());
764+
});
765+
services.OverrideClientFactory();
766+
services.RegisterServiceBusDispatch().ToQueue("myQueue", builder =>
767+
{
768+
builder.RegisterDispatch<SubscribedEvent>();
769+
});
770+
var provider = services.BuildServiceProvider();
771+
await provider.SimulateStartHost(CancellationToken.None);
772+
return provider;
773+
}
774+
604775
public void Dispose()
605776
{
606777
_composer?.Dispose();

0 commit comments

Comments
 (0)