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.
This commit is contained in:
Sipke Schoorstra 2023-12-18 20:20:39 +01:00
parent 5e95daaa7c
commit 76bf3f8e30
9 changed files with 157 additions and 211 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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<string>(s => s == topic),
Arg.Any<string>(),
Arg.Any<ServiceBusProcessorOptions>()
)
.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<string>(s => s == topic),
Arg.Any<string>(),
Arg.Any<ServiceBusProcessorOptions>()
)
.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;
}
}

View file

@ -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>(T payload, string correlationId, int attempt = 1)
{
private readonly ITestOutputHelper _testOutputHelper;
public ServiceBusProcessorTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
public async Task SendMessage<T>(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>(T payload, string correlationId, int deliveryCount = 1)
{
var payloadJson = JsonSerializer.Serialize(payload);
var props = new Dictionary<string, object>() { };
private ProcessMessageEventArgs CreateMessageArgs<T>(T payload, string correlationId, int deliveryCount = 1)
{
var payloadJson = JsonSerializer.Serialize(payload);
var props = new Dictionary<string, object>() { };
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());
}
}

View file

@ -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<string, AutoResetEvent> _events = new();
public AutoResetEvent Get()
{
public AutoResetEvent WaitHandleTest { get; } = new AutoResetEvent(false);
private IDictionary<string, AutoResetEvent> _events = new Dictionary<string, AutoResetEvent>();
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();
}
}

View file

@ -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<dynamic>(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

View file

@ -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"; }),
}
};
}
}

View file

@ -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"; }),
}
};
}
}

View file

@ -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";
}),
}
};
}
}