From 76bf3f8e3074dc044f0c1c3e47cee4074ab81979 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 18 Dec 2023 20:20:39 +0100 Subject: [PATCH] Refactor code structure for ServiceBus Integration tests Refactored the ServiceBus Integration tests for improved readability and maintenance. The changes included reducing the number of lines by removing unnecessary curly braces, removing unnecessary use of 'this' qualifier, replacing null checks with null-conditional operators, and organizing various code blocks for better understanding. --- .../Contracts/IServiceBusProcessorManager.cs | 11 +-- .../Contracts/ITestResetEventManager.cs | 13 ++- .../Helpers/ServiceBusProcessorManager.cs | 67 ++++++------- .../Helpers/ServiceBusProcessorTest.cs | 57 +++++------ .../Helpers/TestResetEventManager.cs | 55 ++++++----- .../Scenarios/ServiceBus/ServiceBusTests.cs | 8 +- .../Workflows/ReceiveMessageWorkflow.cs | 34 +++---- .../Workflows/ReceiveOneMessageWorkflow.cs | 28 ++---- .../Workflows/SendOneMessageWorkflow.cs | 95 ++++++++----------- 9 files changed, 157 insertions(+), 211 deletions(-) diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/IServiceBusProcessorManager.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/IServiceBusProcessorManager.cs index 7664f5e60..557c6500d 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/IServiceBusProcessorManager.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/IServiceBusProcessorManager.cs @@ -1,10 +1,9 @@ using Elsa.ServiceBus.IntegrationTests.Helpers; -namespace Elsa.ServiceBus.IntegrationTests.Contracts +namespace Elsa.ServiceBus.IntegrationTests.Contracts; + +public interface IServiceBusProcessorManager { - public interface IServiceBusProcessorManager - { - public ServiceBusProcessorTest Get(string topic, string subscription); - public ServiceBusProcessorTest Init(string topic, string subscription); - } + public ServiceBusProcessorTest Get(string topic, string subscription); + public ServiceBusProcessorTest Init(string topic, string subscription); } \ No newline at end of file diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/ITestResetEventManager.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/ITestResetEventManager.cs index ee9d41d7c..6b3226e01 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/ITestResetEventManager.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Contracts/ITestResetEventManager.cs @@ -1,9 +1,8 @@ -namespace Elsa.ServiceBus.IntegrationTests.Contracts +namespace Elsa.ServiceBus.IntegrationTests.Contracts; + +public interface ITestResetEventManager { - public interface ITestResetEventManager - { - public void Set(string resetEvent); - public AutoResetEvent Get(string resetEvent); - public AutoResetEvent Init(string resetEvent); - } + public void Set(string resetEvent); + public AutoResetEvent? Get(string resetEvent); + public AutoResetEvent Init(string resetEvent); } \ No newline at end of file diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorManager.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorManager.cs index 1c2e43517..dcdd2f6c6 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorManager.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorManager.cs @@ -3,49 +3,40 @@ using Elsa.ServiceBus.IntegrationTests.Contracts; using NSubstitute; using Xunit.Abstractions; -namespace Elsa.ServiceBus.IntegrationTests.Helpers +namespace Elsa.ServiceBus.IntegrationTests.Helpers; + +public class ServiceBusProcessorManager(ServiceBusClient serviceBusClient, ITestOutputHelper testOutputHelper) : IServiceBusProcessorManager { - public class ServiceBusProcessorManager : IServiceBusProcessorManager + private readonly IDictionary<(string topicName, string subscriptionName), ServiceBusProcessorTest> _serviceBusProcessors = new Dictionary<(string topicName, string subscriptionName), ServiceBusProcessorTest>(); + + public ServiceBusProcessorTest Init(string topic, string subscription) { - private readonly ServiceBusClient _serviceBusClient; - private readonly ITestOutputHelper _testOutputHelper; - private readonly IDictionary<(string topicName, string subscriptionName), ServiceBusProcessorTest> _serviceBusProcessors = new Dictionary<(string topicName, string subscriptionName), ServiceBusProcessorTest>(); + _serviceBusProcessors.TryGetValue((topic, subscription), out var processor); + if (processor != null) + throw new ArgumentException($"ServiceBusProcessor with topic:{topic}, subscription:{subscription} already initialized"); - public ServiceBusProcessorManager(ServiceBusClient serviceBusClient, ITestOutputHelper testOutputHelper) - { - _serviceBusClient = serviceBusClient; - _testOutputHelper = testOutputHelper; - } + processor = new ServiceBusProcessorTest(testOutputHelper); + serviceBusClient + .CreateProcessor(Arg.Is(s => s == topic), + Arg.Any(), + Arg.Any() + ) + .Returns((c) => + { + testOutputHelper.WriteLine("ServiceBusClient"); + return processor; + }); - public ServiceBusProcessorTest Init(string topic, string subscription) - { - _serviceBusProcessors.TryGetValue((topic, subscription), out var processor); - if (processor != null) - throw new ArgumentException($"ServiceBusProcessor with topic:{topic}, subscription:{subscription} already initialized"); + _serviceBusProcessors.Add((topic, subscription), processor); + return processor; + } - processor = new ServiceBusProcessorTest(_testOutputHelper); - _serviceBusClient - .CreateProcessor(Arg.Is(s => s == topic), - Arg.Any(), - Arg.Any() - ) - .Returns((c) => - { - _testOutputHelper.WriteLine("ServiceBusClient"); - return processor; - }); + public ServiceBusProcessorTest Get(string topic, string subscription) + { + _serviceBusProcessors.TryGetValue((topic, subscription), out var processor); + if (processor == null) + throw new ArgumentException($"ServiceBusProcessor with topic:{topic}, subscription:{subscription} not exist"); - _serviceBusProcessors.Add((topic, subscription), processor); - return processor; - } - - public ServiceBusProcessorTest Get(string topic, string subscription) - { - _serviceBusProcessors.TryGetValue((topic, subscription), out var processor); - if (processor == null) - throw new ArgumentException($"ServiceBusProcessor with topic:{topic}, subscription:{subscription} not exist"); - - return processor; - } + return processor; } } \ No newline at end of file diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorTest.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorTest.cs index 35efe3233..d7956627f 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorTest.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/ServiceBusProcessorTest.cs @@ -2,45 +2,34 @@ using System.Text.Json; using Xunit.Abstractions; -namespace Elsa.ServiceBus.IntegrationTests.Helpers +namespace Elsa.ServiceBus.IntegrationTests.Helpers; + +public class ServiceBusProcessorTest(ITestOutputHelper testOutputHelper) : ServiceBusProcessor { - public class ServiceBusProcessorTest : ServiceBusProcessor + public Task SendMessage(T payload, string correlationId, int attempt = 1) { - private readonly ITestOutputHelper _testOutputHelper; - - public ServiceBusProcessorTest(ITestOutputHelper testOutputHelper) - { - _testOutputHelper = testOutputHelper; - } - public async Task SendMessage(T payload, string correlationId, int attempt = 1) - { - var args = CreateMessageArgs(payload, correlationId, attempt); - await base.OnProcessMessageAsync(args); - } + var args = CreateMessageArgs(payload, correlationId, attempt); + return base.OnProcessMessageAsync(args); + } - public override Task StartProcessingAsync(CancellationToken cancellationToken = default) - { - _testOutputHelper.WriteLine("Receiving Service Bus Message"); - return Task.CompletedTask; - } + public override Task StartProcessingAsync(CancellationToken cancellationToken = default) + { + testOutputHelper.WriteLine("Receiving Service Bus Message"); + return Task.CompletedTask; + } - private ProcessMessageEventArgs CreateMessageArgs(T payload, string correlationId, int deliveryCount = 1) - { - var payloadJson = JsonSerializer.Serialize(payload); - var props = new Dictionary() { }; + private ProcessMessageEventArgs CreateMessageArgs(T payload, string correlationId, int deliveryCount = 1) + { + var payloadJson = JsonSerializer.Serialize(payload); + var props = new Dictionary() { }; - var message = ServiceBusModelFactory.ServiceBusReceivedMessage( - body: BinaryData.FromString(payloadJson), - deliveryCount: deliveryCount, - correlationId: correlationId, - properties: props - ); + var message = ServiceBusModelFactory.ServiceBusReceivedMessage( + body: BinaryData.FromString(payloadJson), + deliveryCount: deliveryCount, + correlationId: correlationId, + properties: props + ); - - var args = new ProcessMessageEventArgs(message, null, new CancellationToken()); - - return args; - } - + return new ProcessMessageEventArgs(message, null, new CancellationToken()); } } \ No newline at end of file diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/TestResetEventManager.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/TestResetEventManager.cs index 833569264..0fdef578b 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/TestResetEventManager.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Helpers/TestResetEventManager.cs @@ -1,37 +1,36 @@ using Elsa.ServiceBus.IntegrationTests.Contracts; -namespace Elsa.ServiceBus.IntegrationTests.Helpers +namespace Elsa.ServiceBus.IntegrationTests.Helpers; + +public class TestResetEventManager : ITestResetEventManager { - public class TestResetEventManager : ITestResetEventManager + public AutoResetEvent WaitHandleTest { get; } = new(false); + private readonly Dictionary _events = new(); + + public AutoResetEvent Get() { - public AutoResetEvent WaitHandleTest { get; } = new AutoResetEvent(false); - private IDictionary _events = new Dictionary(); - public AutoResetEvent Get() - { - return WaitHandleTest; - } + return WaitHandleTest; + } - public AutoResetEvent Get(string resetEvent) - { - _events.TryGetValue(resetEvent, out var result); - return result; - } + public AutoResetEvent? Get(string resetEvent) + { + _events.TryGetValue(resetEvent, out var result); + return result; + } - public AutoResetEvent Init(string resetEvent) - { - var rEvent = Get(resetEvent); - if (rEvent == null) - { - var newEvent = new AutoResetEvent(false); - _events.Add(resetEvent, newEvent); - return newEvent; - } - return rEvent; - } + public AutoResetEvent Init(string resetEvent) + { + var autoResetEvent = Get(resetEvent); + if (autoResetEvent != null) + return autoResetEvent; + + var newEvent = new AutoResetEvent(false); + _events.Add(resetEvent, newEvent); + return newEvent; + } - public void Set(string resetEvent) - { - Get(resetEvent)?.Set(); - } + public void Set(string resetEvent) + { + Get(resetEvent)?.Set(); } } \ No newline at end of file diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/ServiceBus/ServiceBusTests.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/ServiceBus/ServiceBusTests.cs index 9a2cfca6e..d35881653 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/ServiceBus/ServiceBusTests.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/ServiceBus/ServiceBusTests.cs @@ -96,21 +96,21 @@ public class ServiceBusTest : IDisposable Assert.Equal(WorkflowStatus.Running, workflowState.Status); Assert.Equal(WorkflowSubStatus.Suspended, workflowState.SubStatus); - //Start Worker to send Message on topicName/subscriptionName + // Start Worker to send Message on topicName/subscriptionName await _worker.StartWorkerAsync("topicName", "subscriptionName"); await _sbProcessorManager .Get("topicName", "subscriptionName") .SendMessage(new { hello = "world" }, null!); - //Wait for receiving first message + // Wait for receiving first message var wait1 = _resetEventManager.Get("receive1").WaitOne(TimeSpan.FromSeconds(5)); _testOutputHelper.WriteLine($"wait1 : {wait1}"); - //Wait for receiving second message + // Wait for receiving second message var wait2 = _resetEventManager.Get("receive2").WaitOne(TimeSpan.FromSeconds(5)); _testOutputHelper.WriteLine($"wait2 : {wait2}"); - await Task.Delay(500); //Todo find how to remove delay + await Task.Delay(500); // Todo find how to remove delay var lastWorkflowState = await workflowRuntime.ExportWorkflowStateAsync(workflowState.WorkflowInstanceId); /* * We don't send 2 messages so Workflow must be diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveMessageWorkflow.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveMessageWorkflow.cs index 596de96e1..c80225554 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveMessageWorkflow.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveMessageWorkflow.cs @@ -4,30 +4,22 @@ using Elsa.Workflows.Core; using Elsa.Workflows.Core.Activities; using Elsa.Workflows.Core.Contracts; -namespace Elsa.ServiceBus.IntegrationTests.Scenarios.Workflows +namespace Elsa.ServiceBus.IntegrationTests.Scenarios.Workflows; + +public class ReceiveMessageWorkflow(ITestResetEventManager waitHandleTestManager) : WorkflowBase { - public class ReceiveMessageWorkflow : WorkflowBase + protected override void Build(IWorkflowBuilder builder) { - private readonly ITestResetEventManager _waitHandleTestManager; - - public ReceiveMessageWorkflow(ITestResetEventManager waitHandleTestManager) + builder.Root = new Sequence() { - _waitHandleTestManager = waitHandleTestManager; - } - - protected override void Build(IWorkflowBuilder builder) - { - builder.Root = new Sequence() + Activities = { - Activities = - { - new MessageReceived("topicName","subscriptionName"), - new WriteLine(context=> {_waitHandleTestManager.Set("receive1"); return "first receive ok"; }), - new MessageReceived("topicName1","subscriptionName1"), - new WriteLine(context=> {_waitHandleTestManager.Set("receive2"); return "Ok"; }), - } - }; - } - + new MessageReceived("topicName","subscriptionName"), + new WriteLine(context=> {waitHandleTestManager.Set("receive1"); return "first receive ok"; }), + new MessageReceived("topicName1","subscriptionName1"), + new WriteLine(context=> {waitHandleTestManager.Set("receive2"); return "Ok"; }), + } + }; } + } \ No newline at end of file diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveOneMessageWorkflow.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveOneMessageWorkflow.cs index 5b2214602..9c6ede100 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveOneMessageWorkflow.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/ReceiveOneMessageWorkflow.cs @@ -4,27 +4,19 @@ using Elsa.Workflows.Core; using Elsa.Workflows.Core.Activities; using Elsa.Workflows.Core.Contracts; -namespace Elsa.ServiceBus.IntegrationTests.Scenarios.Workflows +namespace Elsa.ServiceBus.IntegrationTests.Scenarios.Workflows; + +public class ReceiveOneMessageWorkflow(ITestResetEventManager waitHandleTestManager) : WorkflowBase { - public class ReceiveOneMessageWorkflow : WorkflowBase + protected override void Build(IWorkflowBuilder builder) { - private readonly ITestResetEventManager _waitHandleTestManager; - - public ReceiveOneMessageWorkflow(ITestResetEventManager waitHandleTestManager) + builder.Root = new Sequence() { - _waitHandleTestManager = waitHandleTestManager; - } - - protected override void Build(IWorkflowBuilder builder) - { - builder.Root = new Sequence() + Activities = { - Activities = - { - new MessageReceived("topicName","subscriptionName"), - new WriteLine(context=> {_waitHandleTestManager.Set("receive1"); return "first receive ok"; }), - } - }; - } + new MessageReceived("topicName","subscriptionName"), + new WriteLine(context=> {waitHandleTestManager.Set("receive1"); return "first receive ok"; }), + } + }; } } \ No newline at end of file diff --git a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/SendOneMessageWorkflow.cs b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/SendOneMessageWorkflow.cs index 191df9b42..95cffb8d4 100644 --- a/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/SendOneMessageWorkflow.cs +++ b/test/integration/Elsa.ServiceBus.IntegrationTests/Scenarios/Workflows/SendOneMessageWorkflow.cs @@ -4,71 +4,56 @@ using Elsa.Workflows.Core; using Elsa.Workflows.Core.Activities; using Elsa.Workflows.Core.Contracts; -namespace Elsa.ServiceBus.IntegrationTests.Scenarios.Workflows +namespace Elsa.ServiceBus.IntegrationTests.Scenarios.Workflows; + +public class SendOneMessageWorkflow(ITestResetEventManager waitHandleTestManager) : WorkflowBase { - public class SendOneMessageWorkflow : WorkflowBase + protected override void Build(IWorkflowBuilder builder) { - private readonly ITestResetEventManager _waitHandleTestManager; - - public SendOneMessageWorkflow(ITestResetEventManager waitHandleTestManager) + builder.Root = new Sequence { - _waitHandleTestManager = waitHandleTestManager; - } - - protected override void Build(IWorkflowBuilder builder) - { - builder.Root = new Sequence + Activities = { - Activities = + new SendMessage { - new SendMessage - { - QueueOrTopic = new("sendTopic1"), - MessageBody = new ("Hello World"), - }, - new MessageReceived("topicName", "subscriptionName"), - new WriteLine(_ => - { - _waitHandleTestManager.Set("receive1"); - return "first receive ok"; - }), - } - }; - } + QueueOrTopic = new("sendTopic1"), + MessageBody = new ("Hello World"), + }, + new MessageReceived("topicName", "subscriptionName"), + new WriteLine(_ => + { + waitHandleTestManager.Set("receive1"); + return "first receive ok"; + }), + } + }; } +} - public class SendOneMessageWithCorrelationIdWorkflow : WorkflowBase +public class SendOneMessageWithCorrelationIdWorkflow(ITestResetEventManager waitHandleTestManager) : WorkflowBase +{ + protected override void Build(IWorkflowBuilder builder) { - private readonly ITestResetEventManager _waitHandleTestManager; - - public SendOneMessageWithCorrelationIdWorkflow(ITestResetEventManager waitHandleTestManager) + builder.Root = new Sequence { - _waitHandleTestManager = waitHandleTestManager; - } - - protected override void Build(IWorkflowBuilder builder) - { - builder.Root = new Sequence + Activities = { - Activities = + new SendMessage { - new SendMessage - { - QueueOrTopic = new("sendTopic1"), - MessageBody = new("Hello World"), - }, - new Correlate - { - CorrelationId = new("EEE3D9CC-2279-4CE5-8F4F-FC6C65BF8814") - }, - new MessageReceived("topicName", "subscriptionName"), - new WriteLine(context => - { - _waitHandleTestManager.Set("receive1"); - return "first receive ok"; - }), - } - }; - } + QueueOrTopic = new("sendTopic1"), + MessageBody = new("Hello World"), + }, + new Correlate + { + CorrelationId = new("EEE3D9CC-2279-4CE5-8F4F-FC6C65BF8814") + }, + new MessageReceived("topicName", "subscriptionName"), + new WriteLine(context => + { + waitHandleTestManager.Set("receive1"); + return "first receive ok"; + }), + } + }; } } \ No newline at end of file