Skip to content

Commit

Permalink
PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RikuVirtanen committed Mar 17, 2023
1 parent 798582c commit 6c7cbaa
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 372 deletions.
132 changes: 69 additions & 63 deletions Frends.RabbitMQ.Read/Frends.RabbitMQ.Read.Tests/QuorumQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,48 @@ public class QuorumQueueTests
/// Access UI from http://localhost:15672 username: agent, password: agent123
/// </summary>

private const string TestUri = "amqp://agent:agent123@localhost:5672";
private const string TestHost = "localhost";
private const string _testUri = "amqp://agent:agent123@localhost:5672";
private const string _testHost = "localhost";
private const string _exchange = "exchange";
private const string _queue = "quorumqueue";
private const string _username = "agent";
private const string _pws = "agent123";

[TestInitialize]
public void CreateExchangeAndQueue()
{
var factory = new ConnectionFactory { Uri = new Uri(TestUri) };
var factory = new ConnectionFactory { Uri = new Uri(_testUri) };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.ExchangeDeclare("exchange", type: "fanout", durable: false, autoDelete: false);
channel.ExchangeDeclare(_exchange, type: "fanout", durable: false, autoDelete: false);
var args = new Dictionary<string, object>();
args["x-queue-type"] = "quorum";
channel.QueueDeclare("quorumqueue", durable: true, exclusive: false, autoDelete: false, arguments: args);
channel.QueueBind("quorumqueue", "exchange", routingKey: "");
channel.QueueDeclare(_queue, durable: true, exclusive: false, autoDelete: false, arguments: args);
channel.QueueBind(_queue, _exchange, routingKey: "");
}

[TestCleanup]
public void DeleteExchangeAndQueue()
{
var factory = new ConnectionFactory { Uri = new Uri(TestUri) };
var factory = new ConnectionFactory { Uri = new Uri(_testUri) };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDelete("quorumqueue", false, false);
channel.ExchangeDelete("exchange", ifUnused: false);
channel.QueueDelete(_queue, false, false);
channel.ExchangeDelete(_exchange, ifUnused: false);
}

[TestMethod]
public void TestReadMultipleMessagesWithHostQuorum()
{
Connection connection = new()
{
Host = TestHost,
Username = "agent",
Password = "agent123",
RoutingKey = "quorumqueue",
QueueName = "quorumqueue",
Host = _testHost,
Username = _username,
Password = _pws,
RoutingKey = _queue,
QueueName = _queue,
AuthenticationMethod = AuthenticationMethod.Host,
ExchangeName = "exchange",
ExchangeName = _exchange,

AutoAck = ReadAckType.AutoAck,
ReadMessageCount = 2,
Expand All @@ -62,30 +66,47 @@ public void TestReadMultipleMessagesWithHostQuorum()
Publish(connection, 2);
var result = RabbitMQ.Read(connection);

var test1 = result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-AppId"));
var test2 = result.MessagesBase64.Any(x => x.Headers.ContainsValue("application id"));

Assert.IsTrue(result.MessagesBase64.Count == 2 && result.MessageUTF8.Count == 2 && result.Success);
Assert.IsTrue(result.MessagesBase64.Any(x => x.Data.Equals("VGVzdCBtZXNzYWdlIDA=") && result.MessageUTF8.Any(x => x.Data.Equals("Test message 0")))
&& result.MessagesBase64.Any(x => x.Data.Equals("VGVzdCBtZXNzYWdlIDE=") && result.MessageUTF8.Any(x => x.Data.Equals("Test message 1"))));

Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("application id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("cluster id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Content-Type") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("content type"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Content-Encoding") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("content encoding"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-CorrelationId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("correlation id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-Expiration") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("100"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-MessageId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("message id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Custom-Header") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("custom header"))));

Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("application id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("cluster id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Content-Type") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("content type"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Content-Encoding") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("content encoding"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-CorrelationId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("correlation id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-Expiration") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("100"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-MessageId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("message id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Custom-Header") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("custom header"))));
Assert.AreEqual(2, result.MessagesBase64.Count);
Assert.AreEqual(2, result.MessageUTF8.Count);
Assert.IsTrue(result.Success);
Assert.IsTrue(result.MessagesBase64.Any(x => x.Data.Equals("VGVzdCBtZXNzYWdlIDA=")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Data.Equals("Test message 0")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Data.Equals("VGVzdCBtZXNzYWdlIDE=")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Data.Equals("Test message 1")));

Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-AppId")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("application id")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-AppId")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("cluster id")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Content-Type")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("content type")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Content-Encoding")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("content encoding")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-CorrelationId")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("correlation id")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-Expiration")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("100")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-MessageId")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("message id")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Custom-Header")));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsValue("custom header")));

Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-AppId")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("application id")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-AppId")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("cluster id")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Content-Type")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("content type")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Content-Encoding")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("content encoding")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-CorrelationId")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("correlation id")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-Expiration")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("100")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-MessageId")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("message id")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Custom-Header")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsValue("custom header")));
}

/// <summary>
Expand All @@ -96,9 +117,9 @@ public void TestReadSingleMessageWithURIQuorum()
{
Connection connection = new()
{
Host = TestUri,
RoutingKey = "quorumqueue",
QueueName = "quorumqueue",
Host = _testUri,
RoutingKey = _queue,
QueueName = _queue,
AuthenticationMethod = AuthenticationMethod.URI,
ExchangeName = null,

Expand All @@ -109,26 +130,11 @@ public void TestReadSingleMessageWithURIQuorum()
Publish(connection, 1);
var result = RabbitMQ.Read(connection);

Assert.IsTrue(result.MessagesBase64.Count == 1 && result.MessageUTF8.Count == 1 && result.Success.Equals(true));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Data.Equals("VGVzdCBtZXNzYWdlIDA=") && result.MessageUTF8.Any(x => x.Data.Equals("Test message 0"))));

Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("application id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("cluster id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Content-Type") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("content type"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Content-Encoding") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("content encoding"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-CorrelationId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("correlation id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-Expiration") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("100"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("X-MessageId") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("message id"))));
Assert.IsTrue(result.MessagesBase64.Any(x => x.Headers.ContainsKey("Custom-Header") && result.MessagesBase64.Any(x => x.Headers.ContainsValue("custom header"))));

Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("application id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-AppId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("cluster id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Content-Type") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("content type"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Content-Encoding") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("content encoding"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-CorrelationId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("correlation id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-Expiration") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("100"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("X-MessageId") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("message id"))));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Headers.ContainsKey("Custom-Header") && result.MessageUTF8.Any(x => x.Headers.ContainsValue("custom header"))));
Assert.AreEqual(1, result.MessagesBase64.Count);
Assert.AreEqual(1, result.MessageUTF8.Count);
Assert.IsTrue(result.Success);
Assert.IsTrue(result.MessagesBase64.Any(x => x.Data.Equals("VGVzdCBtZXNzYWdlIDA=")));
Assert.IsTrue(result.MessageUTF8.Any(x => x.Data.Equals("Test message 0")));
}

public static void Publish(Connection connection, int messageCount)
Expand Down Expand Up @@ -164,7 +170,7 @@ public static void Publish(Connection connection, int messageCount)
basicProperties.Headers = headers;

for (var i = 0; i < messageCount; i++)
connectionHelper.AMQPModel.BasicPublish(exchange: "exchange",
connectionHelper.AMQPModel.BasicPublish(exchange: _exchange,
routingKey: connection.RoutingKey,
basicProperties: basicProperties,
body: Encoding.UTF8.GetBytes(message + " " + i));
Expand Down
Loading

0 comments on commit 6c7cbaa

Please sign in to comment.