Skip to content
Open
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
24 changes: 12 additions & 12 deletions src/MerQure.RbMQ/MessagingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

public void DeclareExchange(string exchangeName, string exchangeType)
{
if (string.IsNullOrWhiteSpace(exchangeName)) throw new ArgumentNullException(nameof(exchangeName));

Check warning on line 43 in src/MerQure.RbMQ/MessagingService.cs

View workflow job for this annotation

GitHub Actions / build

Reformat the code to have only one statement per line. (https://rules.sonarsource.com/csharp/RSPEC-122)

using (var channel = CurrentConnection.CreateModel())
{
Expand All @@ -48,23 +48,23 @@
}
}

public string DeclareQueue(string queueName, byte maxPriority, bool isQuorum)
public string DeclareQueue(string proposedQueueName, byte maxPriority, bool isQuorum)
{
var queueArgs = new Dictionary<string, object> {
{ Constants.QueueMaxPriority, maxPriority }
};
return DeclareQueue(queueName, queueArgs, isQuorum);
return DeclareQueue(proposedQueueName, queueArgs, isQuorum);
}

public string DeclareQueue(string queueName, bool isQuorum)
public string DeclareQueue(string proposedQueueName, bool isQuorum)
{
return DeclareQueue(queueName, new Dictionary<string, object>(), isQuorum);
return DeclareQueue(proposedQueueName, new Dictionary<string, object>(), isQuorum);
}

public string DeclareQueueWithDeadLetterPolicy(string queueName, string deadLetterExchange, int messageTimeToLive, string deadLetterRoutingKey, bool isQuorum)
public string DeclareQueueWithDeadLetterPolicy(string proposedQueueName, string deadLetterExchange, int messageTimeToLive, string deadLetterRoutingKey, bool isQuorum)
{
if (string.IsNullOrWhiteSpace(deadLetterExchange)) throw new ArgumentNullException(nameof(deadLetterExchange));

Check warning on line 66 in src/MerQure.RbMQ/MessagingService.cs

View workflow job for this annotation

GitHub Actions / build

Reformat the code to have only one statement per line. (https://rules.sonarsource.com/csharp/RSPEC-122)
if (messageTimeToLive <= 0) throw new ArgumentOutOfRangeException(nameof(messageTimeToLive));

Check warning on line 67 in src/MerQure.RbMQ/MessagingService.cs

View workflow job for this annotation

GitHub Actions / build

Reformat the code to have only one statement per line. (https://rules.sonarsource.com/csharp/RSPEC-122)

var queueArgs = new Dictionary<string, object> {
{ Constants.QueueDeadLetterExchange, deadLetterExchange },
Expand All @@ -74,17 +74,19 @@
{
queueArgs.Add(Constants.QueueDeadLetterRoutingKey, deadLetterRoutingKey);
}
return DeclareQueue(queueName, queueArgs, isQuorum);
return DeclareQueue(proposedQueueName, queueArgs, isQuorum);
}
private const string QuorumQueueNameSuffix ="-q";

public string DeclareQueue(string queueName, Dictionary<string, object> queueArgs, bool isQuorum)
public const string QuorumQueueNameSuffix ="-q";
public string QueueNameTransformationUsedByDeclareQueue(string proposedQueueName, bool isQuorum) => isQuorum ? $"{proposedQueueName}{QuorumQueueNameSuffix}" : proposedQueueName;

public string DeclareQueue(string proposedQueueName, Dictionary<string, object> queueArgs, bool isQuorum)
{
if (string.IsNullOrWhiteSpace(queueName)) throw new ArgumentNullException(nameof(queueName));
if (string.IsNullOrWhiteSpace(proposedQueueName)) throw new ArgumentNullException(nameof(proposedQueueName));

Check warning on line 85 in src/MerQure.RbMQ/MessagingService.cs

View workflow job for this annotation

GitHub Actions / build

Reformat the code to have only one statement per line. (https://rules.sonarsource.com/csharp/RSPEC-122)
if (queueArgs == null) throw new ArgumentNullException(nameof(queueArgs));

Check warning on line 86 in src/MerQure.RbMQ/MessagingService.cs

View workflow job for this annotation

GitHub Actions / build

Reformat the code to have only one statement per line. (https://rules.sonarsource.com/csharp/RSPEC-122)

string effectiveQueueName = QueueNameTransformationUsedByDeclareQueue(proposedQueueName, isQuorum);;
Dictionary<string, object> effectiveQueueArgs;
string effectiveQueueName;
if(isQuorum)
{
if(!Durable || AutoDeleteQueue)
Expand All @@ -95,12 +97,10 @@
{
{ Constants.HeaderQueueType, Constants.HeaderQueueTypeQuorumValue }
};
effectiveQueueName = $"{queueName}{QuorumQueueNameSuffix}";
}
else
{
effectiveQueueArgs = queueArgs;
effectiveQueueName = queueName;
}

using (var channel = CurrentConnection.CreateModel())
Expand Down
25 changes: 18 additions & 7 deletions src/MerQure/IMessagingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,44 @@ public interface IMessagingService
/// <param name="exchangeType">fanout, direct, topic, headers</param>
void DeclareExchange(string exchangeName, string exchangeType);

/// <summary>
/// Implementation of name transformation between proposedQueueName and queue name effectively used made by each DeclareQueueXxxx method
/// </summary>
/// <param name="proposedQueueName"></param>
/// <param name="isQuorum"></param>
/// <returns>Effective queue name</returns>
string QueueNameTransformationUsedByDeclareQueue(string proposedQueueName, bool isQuorum);

/// <summary>
/// Declare a queue with dead letter policy
/// </summary>
/// <param name="queueName"></param>
/// <param name="proposedQueueName">Proposed queue name (will be transformetransformed by <see cref="QueueNameTransformationUsedByDeclareQueue"/>)</param>
/// <param name="deadLetterExchange"></param>
/// <param name="messageTimeToLive"></param>
/// <param name="deadLetterRoutingKey"></param>
/// <param name="isQuorum">Whether created queue should be quorum (when true, quorum arguments will be added to arguments used for queue creation)</param>
/// <see cref="https://www.rabbitmq.com/dlx.html"/>
string DeclareQueueWithDeadLetterPolicy(string queueName, string deadLetterExchange, int messageTimeToLive, string deadLetterRoutingKey, bool isQuorum);
/// <returns>Effective queue name used by created queue</returns>
string DeclareQueueWithDeadLetterPolicy(string proposedQueueName, string deadLetterExchange, int messageTimeToLive, string deadLetterRoutingKey, bool isQuorum);

string DeclareQueue(string queueName, byte maxPriority, bool isQuorum);
string DeclareQueue(string proposedQueueName, byte maxPriority, bool isQuorum);

/// <summary>
/// Declare a queue (if it doesn't exists) with specific arguments
/// </summary>
/// <param name="queueName"></param>
/// <param name="proposedQueueName">Proposed queue name (transformed by <see cref="QueueNameTransformationUsedByDeclareQueue"/>)</param>
/// <param name="queueArgs">Queue's arguments</param>
/// <param name="isQuorum">Whether created queue should be quorum (when true, quorum arguments will be added to arguments used for queue creation)</param>
string DeclareQueue(string queueName, Dictionary<string, object> queueArgs, bool isQuorum);
/// <returns>Effective queue name used by created queue</returns>
string DeclareQueue(string proposedQueueName, Dictionary<string, object> queueArgs, bool isQuorum);

/// <summary>
/// Declare a queue (if it doesn't exists)
/// </summary>
/// <param name="queueName"></param>
/// <param name="proposedQueueName">Proposed queue name (transformed by <see cref="QueueNameTransformationUsedByDeclareQueue"/>)</param>
/// <param name="isQuorum">Whether created queue should be quorum (when true, quorum arguments will be added to arguments used for queue creation)</param>
string DeclareQueue(string queueName, bool isQuorum);
/// <returns>Effective queue name used by created queue</returns>
string DeclareQueue(string proposedQueueName, bool isQuorum);

/// <summary>
/// Declare an Binding (if it doesn't exists)
Expand Down
10 changes: 7 additions & 3 deletions tests/MerQure.Tools.Tests/RetryBusServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Moq;
using System;
using System.Collections.Generic;
using MerQure.RbMQ;
using Xunit;

namespace MerQure.Tools.Tests
Expand All @@ -21,9 +22,12 @@ public class RetryBusServiceTests : IDisposable
public RetryBusServiceTests()
{
_mockMessagingService = new Mock<IMessagingService>();
_mockMessagingService
.Setup(service => service.QueueNameTransformationUsedByDeclareQueue(It.IsAny<string>(), It.IsAny<bool>()))
.Returns((string proposedQueueName, bool isQuorum) => isQuorum ? $"{proposedQueueName}{MessagingService.QuorumQueueNameSuffix}" : proposedQueueName);
_mockMessagingService
.Setup(service => service.DeclareQueue(It.IsAny<string>(), It.IsAny<bool>()))
.Returns((string queueName, bool isQuorum) => isQuorum ? queueName + "-q" : queueName);
.Returns((string proposedQueueName, bool isQuorum) => isQuorum ? $"{proposedQueueName}{MessagingService.QuorumQueueNameSuffix}" : proposedQueueName);

_retryBusService = new RetryBusService(_mockMessagingService.Object);
}
Expand Down Expand Up @@ -111,8 +115,8 @@ public void RetryBusService_CreateNewBus_quorum_ShouldDeclareBindingForeachChann
_retryBusService.CreateNewBus<TestMessage>(retryStrategy, isQuorum: true);

//Assert
_mockMessagingService.Verify(m => m.DeclareBinding(retryStrategy.BusName, $"{_channelNames[0]}-q", It.IsAny<string>()));
_mockMessagingService.Verify(m => m.DeclareBinding(retryStrategy.BusName, $"{_channelNames[1]}-q", It.IsAny<string>()));
_mockMessagingService.Verify(m => m.DeclareBinding(retryStrategy.BusName, $"{_channelNames[0]}{MessagingService.QuorumQueueNameSuffix}", It.IsAny<string>()));
_mockMessagingService.Verify(m => m.DeclareBinding(retryStrategy.BusName, $"{_channelNames[1]}{MessagingService.QuorumQueueNameSuffix}", It.IsAny<string>()));
}

[Theory]
Expand Down
Loading