From 2c8a39f7f1551f839c45dfc9c1db78663a0fee2c Mon Sep 17 00:00:00 2001 From: Felix Pelletier Date: Tue, 27 Feb 2024 10:23:45 -0800 Subject: [PATCH 1/7] chore: cleanup unused warnings --- src/Shared/DomainEventWrapperCollection.cs | 1 - .../EventGridClientAdapter/IEventGridClientAdapter.cs | 4 +--- .../Apis/EventsApiIntegrationTests.cs | 2 -- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Shared/DomainEventWrapperCollection.cs b/src/Shared/DomainEventWrapperCollection.cs index 5c2f0947..1a49899d 100644 --- a/src/Shared/DomainEventWrapperCollection.cs +++ b/src/Shared/DomainEventWrapperCollection.cs @@ -1,5 +1,4 @@ using System.Collections; -using System.Runtime.InteropServices.ComTypes; namespace Workleap.DomainEventPropagation; diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventGridClientAdapter/IEventGridClientAdapter.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventGridClientAdapter/IEventGridClientAdapter.cs index ea079a25..763a8f47 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventGridClientAdapter/IEventGridClientAdapter.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventGridClientAdapter/IEventGridClientAdapter.cs @@ -1,6 +1,4 @@ -using Azure.Messaging.EventGrid.Namespaces; - -namespace Workleap.DomainEventPropagation.EventGridClientAdapter; +namespace Workleap.DomainEventPropagation.EventGridClientAdapter; internal interface IEventGridClientAdapter { diff --git a/src/Workleap.DomainEventPropagation.Subscription.Tests/Apis/EventsApiIntegrationTests.cs b/src/Workleap.DomainEventPropagation.Subscription.Tests/Apis/EventsApiIntegrationTests.cs index cf66f66e..3e84d2d2 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.Tests/Apis/EventsApiIntegrationTests.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.Tests/Apis/EventsApiIntegrationTests.cs @@ -1,7 +1,5 @@ using System.Net; using System.Net.Http.Json; -using System.Net.Mime; -using System.Text; using System.Text.Json; using Azure.Messaging.EventGrid; using Azure.Messaging.EventGrid.SystemEvents; From 792f8aaed9f01063ad86f0938911dc7bc5541774 Mon Sep 17 00:00:00 2001 From: Felix Pelletier Date: Tue, 27 Feb 2024 10:42:22 -0800 Subject: [PATCH 2/7] feat(refactor): rename the new Event Grid client being involved for event publishing --- .../EventPropagationClient.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Workleap.DomainEventPropagation.Publishing/EventPropagationClient.cs b/src/Workleap.DomainEventPropagation.Publishing/EventPropagationClient.cs index da33fbf9..8bbadc7f 100644 --- a/src/Workleap.DomainEventPropagation.Publishing/EventPropagationClient.cs +++ b/src/Workleap.DomainEventPropagation.Publishing/EventPropagationClient.cs @@ -16,7 +16,7 @@ internal sealed class EventPropagationClient : IEventPropagationClient private readonly EventPropagationPublisherOptions _eventPropagationPublisherOptions; private readonly DomainEventsHandlerDelegate _pipeline; private readonly EventGridPublisherClient _eventGridPublisherClient; - private readonly EventGridClient _eventGridClient; + private readonly EventGridClient _eventGridNamespaceClient; /// /// To support Namespace topic, we need to use the following EventGridClient https://github.com/Azure/azure-sdk-for-net/blob/Azure.Messaging.EventGrid_4.17.0-beta.1/sdk/eventgrid/Azure.Messaging.EventGridV2/src/Generated/EventGridClient.cs @@ -31,7 +31,7 @@ public EventPropagationClient( this._eventPropagationPublisherOptions = eventPropagationPublisherOptions.Value; this._pipeline = publishingDomainEventBehaviors.Reverse().Aggregate((DomainEventsHandlerDelegate)this.SendDomainEventsAsync, BuildPipeline); this._eventGridPublisherClient = eventGridPublisherClientFactory.CreateClient(EventPropagationPublisherOptions.CustomTopicClientName); - this._eventGridClient = eventGridClientFactory.CreateClient(EventPropagationPublisherOptions.NamespaceTopicClientName); + this._eventGridNamespaceClient = eventGridClientFactory.CreateClient(EventPropagationPublisherOptions.NamespaceTopicClientName); } private static DomainEventsHandlerDelegate BuildPipeline(DomainEventsHandlerDelegate accumulator, IPublishingDomainEventBehavior next) @@ -110,7 +110,7 @@ private Task SendCloudEvents( return topicType switch { TopicType.Custom => this._eventGridPublisherClient.SendEventsAsync(cloudEvents, cancellationToken), - TopicType.Namespace => this._eventGridClient.PublishCloudEventsAsync(topicName, cloudEvents, cancellationToken), + TopicType.Namespace => this._eventGridNamespaceClient.PublishCloudEventsAsync(topicName, cloudEvents, cancellationToken), _ => throw new NotSupportedException($"Topic type {topicType} is not supported"), }; } From f39965eeaa77c956be0d8193e01068ff7b56da08 Mon Sep 17 00:00:00 2001 From: Felix Pelletier Date: Tue, 27 Feb 2024 10:44:04 -0800 Subject: [PATCH 3/7] feat(refactor): rename the DI setup method to configure the Event Grid publisher clients --- .../EventPropagationPublisherBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Workleap.DomainEventPropagation.Publishing/EventPropagationPublisherBuilder.cs b/src/Workleap.DomainEventPropagation.Publishing/EventPropagationPublisherBuilder.cs index cffb4bdf..ec0ac305 100644 --- a/src/Workleap.DomainEventPropagation.Publishing/EventPropagationPublisherBuilder.cs +++ b/src/Workleap.DomainEventPropagation.Publishing/EventPropagationPublisherBuilder.cs @@ -31,7 +31,7 @@ private void AddRegistrations(Action configure this.Services.TryAddEnumerable(ServiceDescriptor.Singleton, EventPropagationPublisherOptionsValidator>()); this.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); - this.Services.AddAzureClients(ConfigureEventPublisher); + this.Services.AddAzureClients(ConfigureEventPublisherClients); } private static void BindFromWellKnownConfigurationSection(EventPropagationPublisherOptions options, IConfiguration configuration) @@ -39,7 +39,7 @@ private static void BindFromWellKnownConfigurationSection(EventPropagationPublis configuration.GetSection(EventPropagationPublisherOptions.SectionName).Bind(options); } - private static void ConfigureEventPublisher(AzureClientFactoryBuilder builder) + private static void ConfigureEventPublisherClients(AzureClientFactoryBuilder builder) { builder.AddClient(EventGridPublisherClientFactory) .WithName(EventPropagationPublisherOptions.CustomTopicClientName) From e7eb821ac5d9ec33cf83ca38ac017198131fced6 Mon Sep 17 00:00:00 2001 From: Felix Pelletier Date: Tue, 27 Feb 2024 10:57:08 -0800 Subject: [PATCH 4/7] test(refactor): use xUnit's assertions rather than FluentAssertions for new test project --- .../CloudEventHandlerUnitTests.cs | 19 +++++++++--------- ...lectionEventSubscriptionExtensionsTests.cs | 20 +++++++++---------- ...ion.Subscription.PullDelivery.Tests.csproj | 1 - 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/CloudEventHandlerUnitTests.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/CloudEventHandlerUnitTests.cs index 6d6472a8..c89fd03e 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/CloudEventHandlerUnitTests.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/CloudEventHandlerUnitTests.cs @@ -1,6 +1,5 @@ using System.Reflection; using Azure.Messaging; -using FluentAssertions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; @@ -34,7 +33,7 @@ public async Task Given_IllFormedEvent_When_HandleCloudEventAsync_Then_ReturnsRe var result = await handler.HandleCloudEventAsync(cloudEvent, CancellationToken.None); // Then - result.Should().Be(EventProcessingStatus.Rejected); + Assert.Equal(EventProcessingStatus.Rejected, result); } [Fact] @@ -50,7 +49,7 @@ public async Task Given_EventTypeNotRegistered_When_HandleCloudEventAsync_Then_R var result = await handler.HandleCloudEventAsync(cloudEvent, CancellationToken.None); // Then - result.Should().Be(EventProcessingStatus.Rejected); + Assert.Equal(EventProcessingStatus.Rejected, result); } [Fact] @@ -69,7 +68,7 @@ public async Task Given_TypeWasRegisteredButNoHandler_When_HandleCloudEventAsync var result = await handler.HandleCloudEventAsync(cloudEvent, CancellationToken.None); // Then - result.Should().Be(EventProcessingStatus.Rejected); + Assert.Equal(EventProcessingStatus.Rejected, result); } [Fact] @@ -92,7 +91,7 @@ public async Task Given_FailingEventHandler_When_HandleCloudEventAsync_Then_Retu var result = await handler.HandleCloudEventAsync(cloudEvent, CancellationToken.None); // Then - result.Should().Be(EventProcessingStatus.Released); + Assert.Equal(EventProcessingStatus.Released, result); } [Fact] @@ -112,8 +111,9 @@ public async Task Given_EventHandler_When_HandleCloudEventAsync_Then_ReturnsHand var result = await handler.HandleCloudEventAsync(cloudEvent, CancellationToken.None); // Then - SampleEventTestHandler.ReceivedEvents.Should().Contain(e => e.Message == eventMessage); - result.Should().Be(EventProcessingStatus.Handled); + Assert.Single(SampleEventTestHandler.ReceivedEvents, e => e.Message == eventMessage); + + Assert.Equal(EventProcessingStatus.Handled, result); } [Fact] @@ -133,8 +133,9 @@ public async Task Given_EventHandlersFromAssembly_When_HandleCloudEventAsync_The var result = await handler.HandleCloudEventAsync(cloudEvent, CancellationToken.None); // Then - SampleEventTestHandler.ReceivedEvents.Should().Contain(e => e.Message == eventMessage); - result.Should().Be(EventProcessingStatus.Handled); + Assert.Single(SampleEventTestHandler.ReceivedEvents, e => e.Message == eventMessage); + + Assert.Equal(EventProcessingStatus.Handled, result); } private static CloudEvent GivenSampleEvent(string message = "Hello World!") diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs index 2691b4cd..b4d82d71 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs @@ -1,7 +1,6 @@ using System.Reflection; using Azure.Messaging.EventGrid.Namespaces; using FakeItEasy; -using FluentAssertions; using Microsoft.Extensions.Azure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -117,8 +116,9 @@ public void GivenNamedConfigurations_WhenResolveClientDescriptors_ThenListEveryR var clientDescriptors = serviceProvider.GetRequiredService>().ToArray(); // Then - clientDescriptors.Should().HaveCount(2); - clientDescriptors.Select(d => d.Name).Should().BeEquivalentTo(sectionName1, sectionName2); + Assert.Equal(2, clientDescriptors.Length); + + Assert.True(clientDescriptors.Select(d => d.Name).SequenceEqual([sectionName1, sectionName2])); } [Fact] @@ -157,7 +157,7 @@ public void GivenNoSubscriber_WhenRegisterHandlerIndividually_ThenExceptionIsThr .AddDomainEventHandler(); // Then - act.Should().Throw(); + Assert.Throws(act); } [Fact] @@ -171,7 +171,7 @@ public void GivenNoSubscriber_WhenRegisterHandlersFromAssembly_ThenExceptionIsTh .AddDomainEventHandlers(Assembly.GetAssembly(typeof(ServiceCollectionEventSubscriptionExtensionsTests))!); // Then - act.Should().Throw(); + Assert.Throws(act); } [Fact] @@ -181,13 +181,13 @@ public void GivenMultipleHandlersForSameEvent_WhenRegistersThemIndividually_Then var services = new ServiceCollection(); // When - var fct = () => services.AddPullDeliverySubscription() + var act = () => services.AddPullDeliverySubscription() .AddTopicSubscription() .AddDomainEventHandler() .AddDomainEventHandler(); // Then - fct.Should().Throw(); + Assert.Throws(act); } [Fact] @@ -197,13 +197,13 @@ public void GivenMultipleHandlersForSameEvent_WhenIndividuallyRegisteredAndFromA var services = new ServiceCollection(); // When - var fct = () => services.AddPullDeliverySubscription() + var act = () => services.AddPullDeliverySubscription() .AddTopicSubscription() .AddDomainEventHandler() .AddDomainEventHandlers(Assembly.GetAssembly(typeof(ServiceCollectionEventSubscriptionExtensionsTests))!); // Then - fct.Should().Throw(); + Assert.Throws(act); } [Fact] @@ -217,7 +217,7 @@ public void GivenMultipleHandlersForSameEventInAssembly_WhenRegisterHandlers_The .AddDomainEventHandlers(Assembly.GetAssembly(typeof(MisconfiguredTestAssembly.SampleEvent))!); // Then - act.Should().Throw(); + Assert.Throws(act); } private static void GivenConfigurations(IServiceCollection services, params string[] sections) diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests.csproj b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests.csproj index e22d0b9b..2d16b094 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests.csproj +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests.csproj @@ -11,7 +11,6 @@ - From f9711af9d902a90510e095f5ac0b4e9b48f79f90 Mon Sep 17 00:00:00 2001 From: Felix Pelletier Date: Tue, 27 Feb 2024 11:32:42 -0800 Subject: [PATCH 5/7] test(refactor): remove warning by introducing a "fire & forget" mechanism for tasks, in a test context --- .../EventPullerTests.cs | 9 ++--- .../TestExtensions/TaskExtensions.cs | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/TestExtensions/TaskExtensions.cs diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs index 1bc688d2..2b85eb2c 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs @@ -1,4 +1,4 @@ -using AutoBogus; +using AutoBogus; using Azure.Messaging; using FakeItEasy; using Microsoft.Extensions.DependencyInjection; @@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Workleap.DomainEventPropagation.EventGridClientAdapter; +using Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests.TestExtensions; namespace Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests; @@ -49,11 +50,9 @@ private static void RegisterCloudEventsInClient(IEventGridClientAdapter client, private static async Task StartWaitAndStop(IHostedService puller) { -// We need this to start on the thread pool otherwise it will just block the test -#pragma warning disable CS4014 - Task.Run(() => puller.StartAsync(CancellationToken.None)); + // We need this to start on the thread pool otherwise it will just block the test + Task.Run(() => puller.StartAsync(CancellationToken.None)).Forget(); await Task.Delay(50); -#pragma warning restore CS4014 await puller.StopAsync(CancellationToken.None); } diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/TestExtensions/TaskExtensions.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/TestExtensions/TaskExtensions.cs new file mode 100644 index 00000000..7f3acca2 --- /dev/null +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/TestExtensions/TaskExtensions.cs @@ -0,0 +1,38 @@ +namespace Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests.TestExtensions; + +internal static class TaskExtensions +{ + /// + /// Observes the task to avoid the UnobservedTaskException event to be raised. + /// + /// + /// Inspired from this blog post + /// + public static void Forget(this Task task) + { + // note: this code is inspired by a tweet from Ben Adams: https://twitter.com/ben_a_adams/status/1045060828700037125 + // Only care about tasks that may fault (not completed) or are faulted, + // so fast-path for SuccessfullyCompleted and Canceled tasks. + if (!task.IsCompleted || task.IsFaulted) + { + // use "_" (Discard operation) to remove the warning IDE0058: Because this call is not awaited, execution of the current method continues before the call is completed + // https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards?WT.mc_id=DT-MVP-5003978#a-standalone-discard + _ = ForgetAwaited(task); + } + + // Allocate the async/await state machine only when needed for performance reasons. + // More info about the state machine: https://blogs.msdn.microsoft.com/seteplia/2017/11/30/dissecting-the-async-methods-in-c/?WT.mc_id=DT-MVP-5003978 + static async Task ForgetAwaited(Task task) + { + try + { + // No need to resume on the original SynchronizationContext, so use ConfigureAwait(false) + await task.ConfigureAwait(false); + } + catch + { + // Nothing to do here + } + } + } +} \ No newline at end of file From 54dc56302a0129b56a54f6857873841e70f2b560 Mon Sep 17 00:00:00 2001 From: Felix Pelletier Date: Tue, 27 Feb 2024 11:44:54 -0800 Subject: [PATCH 6/7] test(refactor): remove pragma warning disable for EventPullerTests by transforming it into an abstract class --- .../EventPullerTests.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs index 2b85eb2c..d4e9642d 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs @@ -10,10 +10,7 @@ namespace Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests; -// Test class cannot be made static -#pragma warning disable CA1052 -public class EventPullerTests -#pragma warning restore CA1052 +public abstract class EventPullerTests { private static IEventGridClientAdapter GivenFakeClient(IEventGridClientWrapperFactory clientWrapperFactory, string subName) { From 96f1086f5b6707b6e49c9a5bb18366c8120a2252 Mon Sep 17 00:00:00 2001 From: Felix Pelletier Date: Tue, 27 Feb 2024 11:48:55 -0800 Subject: [PATCH 7/7] feat(refactor): rename the background service responsible of pulling Cloud Events from Azure Event Grid --- ...lerTests.cs => EventPullerServiceTests.cs} | 34 +++++++++---------- ...lectionEventSubscriptionExtensionsTests.cs | 2 +- .../EventPropagationSubscriberBuilder.cs | 2 +- .../{EventPuller.cs => EventPullerService.cs} | 10 +++--- 4 files changed, 24 insertions(+), 24 deletions(-) rename src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/{EventPullerTests.cs => EventPullerServiceTests.cs} (89%) rename src/Workleap.DomainEventPropagation.Subscription.PullDelivery/{EventPuller.cs => EventPullerService.cs} (93%) diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerServiceTests.cs similarity index 89% rename from src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs rename to src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerServiceTests.cs index d4e9642d..18801351 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerTests.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/EventPullerServiceTests.cs @@ -10,7 +10,7 @@ namespace Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests; -public abstract class EventPullerTests +public abstract class EventPullerServiceTests { private static IEventGridClientAdapter GivenFakeClient(IEventGridClientWrapperFactory clientWrapperFactory, string subName) { @@ -45,15 +45,15 @@ private static void RegisterCloudEventsInClient(IEventGridClientAdapter client, A._)).Returns(events); } - private static async Task StartWaitAndStop(IHostedService puller) + private static async Task StartWaitAndStop(IHostedService pullerService) { // We need this to start on the thread pool otherwise it will just block the test - Task.Run(() => puller.StartAsync(CancellationToken.None)).Forget(); + Task.Run(() => pullerService.StartAsync(CancellationToken.None)).Forget(); await Task.Delay(50); - await puller.StopAsync(CancellationToken.None); + await pullerService.StopAsync(CancellationToken.None); } - public class TwoSubscribers : EventPullerTests + public class TwoSubscribers : EventPullerServiceTests { [Fact] public async Task GivenPuller_WhenStarted_ThenEveryRegisteredClientWasCalled() @@ -73,8 +73,8 @@ public async Task GivenPuller_WhenStarted_ThenEveryRegisteredClientWasCalled() var options2 = GivenEventPropagationSubscriptionOptions(optionMonitor, clientName2); // When - using var puller = new EventPuller(scopeFactory, eventGridClientDescriptors, clientFactory, optionMonitor, new NullLogger()); - await StartWaitAndStop(puller); + using var pullerService = new EventPullerService(scopeFactory, eventGridClientDescriptors, clientFactory, optionMonitor, new NullLogger()); + await StartWaitAndStop(pullerService); // Then A.CallTo(() => fakeClient1 @@ -117,8 +117,8 @@ public async Task GivenFailingClient_WhenErrorOccured_ThenKeepsPollingAndDoesNot RegisterCloudEventsInClient(functionalClient, options2, eventBundle); // When - using var puller = new EventPuller(GivenScopeFactory(eventHandler), eventGridClientDescriptors, clientFactory, optionMonitor, new NullLogger()); - await StartWaitAndStop(puller); + using var pullerService = new EventPullerService(GivenScopeFactory(eventHandler), eventGridClientDescriptors, clientFactory, optionMonitor, new NullLogger()); + await StartWaitAndStop(pullerService); // Then A.CallTo(() => failingClient @@ -131,9 +131,9 @@ public async Task GivenFailingClient_WhenErrorOccured_ThenKeepsPollingAndDoesNot } } - public class OneSubscriber : EventPullerTests, IDisposable + public class OneSubscriber : EventPullerServiceTests, IDisposable { - private readonly EventPuller _puller; + private readonly EventPullerService _pullerService; private readonly IEventGridClientAdapter _client; private readonly EventPropagationSubscriptionOptions _option; private readonly ICloudEventHandler _eventHandler; @@ -157,7 +157,7 @@ public OneSubscriber() this._eventHandler = A.Fake(opt => opt.Strict()); - this._puller = new EventPuller(GivenScopeFactory(this._eventHandler), eventGridClientDescriptors, clientFactory, optionMonitor, new NullLogger()); + this._pullerService = new EventPullerService(GivenScopeFactory(this._eventHandler), eventGridClientDescriptors, clientFactory, optionMonitor, new NullLogger()); } public void Dispose() @@ -170,7 +170,7 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - this._puller.Dispose(); + this._pullerService.Dispose(); } } @@ -184,7 +184,7 @@ public async Task GivenPuller_WhenMultipleEventsAreReceived_ThenEveryEventsAreHa call2.Returns(EventProcessingStatus.Handled); // When - await StartWaitAndStop(this._puller); + await StartWaitAndStop(this._pullerService); // Then call1.MustHaveHappenedOnceOrMore(); @@ -199,7 +199,7 @@ public async Task GivenPuller_WhenHandlerReturnsHandledStatus_ThenEventAreAcknow .Returns(EventProcessingStatus.Handled); // When - await StartWaitAndStop(this._puller); + await StartWaitAndStop(this._pullerService); // Then A.CallTo(() => this._client.AcknowledgeCloudEventAsync( @@ -223,7 +223,7 @@ public async Task GivenPullerWithOneSub_WhenHandlerReturnsReleaseStatus_ThenEven .Returns(EventProcessingStatus.Released); // When - await StartWaitAndStop(this._puller); + await StartWaitAndStop(this._pullerService); // Then A.CallTo(() => this._client.ReleaseCloudEventAsync( @@ -247,7 +247,7 @@ public async Task GivenPullerWithOneSub_WhenHandlerReturnsRejectedStatus_ThenEve .Returns(EventProcessingStatus.Rejected); // When - await StartWaitAndStop(this._puller); + await StartWaitAndStop(this._pullerService); // Then A.CallTo(() => this._client.RejectCloudEventAsync( diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs index b4d82d71..7431b457 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery.Tests/ServiceCollectionEventSubscriptionExtensionsTests.cs @@ -131,7 +131,7 @@ public void GivenNamedConfigurations_WhenResolveEventPuller_ThenCreatesEveryClie GivenConfigurations(services, sectionName1, sectionName2); var fakeClientFactory = A.Fake>(); services.Replace(new ServiceDescriptor(typeof(IAzureClientFactory), fakeClientFactory)); - services.AddTransient, NullLogger>(); + services.AddTransient, NullLogger>(); services.AddTransient, NullLogger>(); // When diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPropagationSubscriberBuilder.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPropagationSubscriberBuilder.cs index b01c828a..c2e9cfd3 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPropagationSubscriberBuilder.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPropagationSubscriberBuilder.cs @@ -27,7 +27,7 @@ public EventPropagationSubscriberBuilder(IServiceCollection services) this.Services.AddTransient(); this.Services.AddTransient(); - this.Services.AddHostedService(); + this.Services.AddHostedService(); } public IServiceCollection Services { get; } diff --git a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPuller.cs b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPullerService.cs similarity index 93% rename from src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPuller.cs rename to src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPullerService.cs index aaca3eac..2e0bd157 100644 --- a/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPuller.cs +++ b/src/Workleap.DomainEventPropagation.Subscription.PullDelivery/EventPullerService.cs @@ -6,18 +6,18 @@ namespace Workleap.DomainEventPropagation; -internal class EventPuller : BackgroundService +internal class EventPullerService : BackgroundService { private readonly IServiceScopeFactory _serviceScopeFactory; - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly EventGridTopicSubscription[] _eventGridTopicSubscriptions; - public EventPuller( + public EventPullerService( IServiceScopeFactory serviceScopeFactory, IEnumerable clientDescriptors, IEventGridClientWrapperFactory eventGridClientWrapperFactory, IOptionsMonitor optionsMonitor, - ILogger logger) + ILogger logger) { this._serviceScopeFactory = serviceScopeFactory; this._logger = logger; @@ -33,7 +33,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) return Task.WhenAll(this._eventGridTopicSubscriptions.Select(sub => Task.Run(() => this.StartReceivingEventsAsync(sub, this._logger, stoppingToken), stoppingToken))); } - private async Task StartReceivingEventsAsync(EventGridTopicSubscription eventGridTopicSubscription, ILogger logger, CancellationToken stoppingToken) + private async Task StartReceivingEventsAsync(EventGridTopicSubscription eventGridTopicSubscription, ILogger logger, CancellationToken stoppingToken) { using var scope = this._serviceScopeFactory.CreateScope(); var cloudEventHandler = scope.ServiceProvider.GetRequiredService();