Skip to content

Commit

Permalink
Merge pull request #116 from Quogu/CreateAndBindToDefaultExchange
Browse files Browse the repository at this point in the history
Add support for default exchange.
  • Loading branch information
odalet authored Nov 24, 2022
2 parents 22df4c8 + 5c0ed0a commit 81d21b7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
18 changes: 18 additions & 0 deletions src/AddUp.FakeRabbitMQ.Tests/FakeModelBasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ public void BasicPublish_publishes_message()
}
}

[Fact]
public void BasicPublish_to_default_exchange_publishes_message()
{
var server = new RabbitServer();
using (var model = new FakeModel(server))
{
model.QueueDeclare("my_queue");

var message = "hello world!";
var encodedMessage = Encoding.ASCII.GetBytes(message);

model.BasicPublish("", "my_queue", model.CreateBasicProperties(), encodedMessage);

Assert.Single(server.Queues["my_queue"].Messages);
Assert.Equal(encodedMessage, server.Queues["my_queue"].Messages.First().Body);
}
}

[Fact]
public void BasicPublishBatch_publishes_messages()
{
Expand Down
33 changes: 19 additions & 14 deletions src/AddUp.FakeRabbitMQ.Tests/FakeModelExchangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public void ExchangeDeclare_with_all_arguments_creates_exchange()
var arguments = new Dictionary<string, object>();

model.ExchangeDeclare(exchange: exchangeName, type: exchangeType, durable: isDurable, autoDelete: isAutoDelete, arguments: arguments);
Assert.Single(server.Exchanges);
Assert.Equal(2, server.Exchanges.Count);
Assert.Single(server.Exchanges.Where(x => x.Key == exchangeName));

var exchange = server.Exchanges.First();
var exchange = server.Exchanges.Single(x => x.Key == exchangeName);
AssertEx.AssertExchangeDetails(exchange, exchangeName, isAutoDelete, arguments, isDurable, exchangeType);
}
}
Expand All @@ -41,9 +42,10 @@ public void ExchangeDeclare_with_name_type_and_durable_creates_exchange()
const bool isDurable = true;

model.ExchangeDeclare(exchange: exchangeName, type: exchangeType, durable: isDurable);
Assert.Single(server.Exchanges);
Assert.Equal(2, server.Exchanges.Count);
Assert.Single(server.Exchanges.Where(x => x.Key == exchangeName));

var exchange = server.Exchanges.First();
var exchange = server.Exchanges.Single(x => x.Key == exchangeName);
AssertEx.AssertExchangeDetails(exchange, exchangeName, false, null, isDurable, exchangeType);
}
}
Expand All @@ -58,9 +60,10 @@ public void ExchangeDeclare_with_name_and_type_creates_exchange()
const string exchangeType = "someType";

model.ExchangeDeclare(exchange: exchangeName, type: exchangeType);
Assert.Single(server.Exchanges);
Assert.Equal(2, server.Exchanges.Count);
Assert.Single(server.Exchanges.Where(x => x.Key == exchangeName));

var exchange = server.Exchanges.First();
var exchange = server.Exchanges.Single(x => x.Key == exchangeName);
AssertEx.AssertExchangeDetails(exchange, exchangeName, false, null, false, exchangeType);
}
}
Expand Down Expand Up @@ -103,9 +106,10 @@ public void ExchangeDeclareNoWait_creates_exchange()
var arguments = new Dictionary<string, object>();

model.ExchangeDeclareNoWait(exchange: exchangeName, type: exchangeType, durable: isDurable, autoDelete: isAutoDelete, arguments: arguments);
Assert.Single(server.Exchanges);
Assert.Equal(2, server.Exchanges.Count);
Assert.Single(server.Exchanges.Where(x => x.Key == exchangeName));

var exchange = server.Exchanges.First();
var exchange = server.Exchanges.Single(x => x.Key == exchangeName);
AssertEx.AssertExchangeDetails(exchange, exchangeName, isAutoDelete, arguments, isDurable, exchangeType);
}
}
Expand All @@ -119,7 +123,7 @@ public void ExchangeDelete_with_only_name_argument_removes_the_exchange_if_it_ex
const string exchangeName = "someExchange";
model.ExchangeDeclare(exchangeName, "someType");
model.ExchangeDelete(exchange: exchangeName);
Assert.Empty(server.Exchanges);
Assert.Single(server.Exchanges);
}
}

Expand All @@ -134,7 +138,7 @@ public void ExchangeDelete_removes_the_exchange_if_it_exists(bool ifUnused)
const string exchangeName = "someExchange";
model.ExchangeDeclare(exchangeName, "someType");
model.ExchangeDelete(exchange: exchangeName, ifUnused: ifUnused);
Assert.Empty(server.Exchanges);
Assert.Single(server.Exchanges);
}
}

Expand All @@ -149,7 +153,7 @@ public void ExchangeDeleteNoWait_removes_the_exchange_if_it_exists(bool ifUnused
const string exchangeName = "someExchange";
model.ExchangeDeclare(exchangeName, "someType");
model.ExchangeDeleteNoWait(exchange: exchangeName, ifUnused: ifUnused);
Assert.Empty(server.Exchanges);
Assert.Single(server.Exchanges);
}
}

Expand All @@ -162,7 +166,8 @@ public void ExchangeDelete_does_nothing_if_exchange_does_not_exist()
const string exchangeName = "someExchange";
model.ExchangeDeclare(exchangeName, "someType");
model.ExchangeDelete(exchange: "someOtherExchange");
Assert.Single(server.Exchanges);
Assert.Equal(2, server.Exchanges.Count);
Assert.Single(server.Exchanges.Where(s => s.Key == exchangeName));
}
}

Expand Down Expand Up @@ -219,7 +224,7 @@ public void ExchangeUnbind_removes_binding()
model.ExchangeUnbind(queueName, exchangeName, routingKey, arguments);

Assert.True(server.Exchanges[exchangeName].Bindings.IsEmpty);
Assert.True(server.Queues[queueName].Bindings.IsEmpty);
Assert.Single(server.Queues[queueName].Bindings);
}
}

Expand All @@ -240,7 +245,7 @@ public void ExchangeUnbindNoWait_removes_binding()
model.ExchangeUnbindNoWait(queueName, exchangeName, routingKey, arguments);

Assert.True(server.Exchanges[exchangeName].Bindings.IsEmpty);
Assert.True(server.Queues[queueName].Bindings.IsEmpty);
Assert.Single(server.Queues[queueName].Bindings);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/AddUp.FakeRabbitMQ.Tests/FakeModelQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void QueueUnbind_removes_binding()
model.QueueUnbind(queueName, exchangeName, routingKey, arguments);

Assert.True(server.Exchanges[exchangeName].Bindings.IsEmpty);
Assert.True(server.Queues[queueName].Bindings.IsEmpty);
Assert.Single(server.Queues[queueName].Bindings);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/AddUp.FakeRabbitMQ/FakeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ public QueueDeclareOk QueueDeclare(string queue, bool durable, bool exclusive, b
RabbitQueue updateFunction(string name, RabbitQueue existing) => existing;
_ = server.Queues.AddOrUpdate(q, queueInstance, updateFunction);

// RabbitMQ automatically binds queues to the default exchange.
// https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-default
QueueBind(q, "", q, null);

return new QueueDeclareOk(q, 0, 0);
}

Expand Down
10 changes: 10 additions & 0 deletions src/AddUp.FakeRabbitMQ/RabbitServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using RabbitMQ.Client;

namespace AddUp.RabbitMQ.Fakes
{
Expand All @@ -7,6 +8,15 @@ public sealed class RabbitServer
public RabbitServer()
{
Exchanges = new ConcurrentDictionary<string, RabbitExchange>();
// Pre-declare the default exchange.
// https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-default
Exchanges[""] = new RabbitExchange(ExchangeType.Direct, this)
{
Name = "",
Arguments = null,
AutoDelete = false,
IsDurable = true,
};
Queues = new ConcurrentDictionary<string, RabbitQueue>();
}

Expand Down

0 comments on commit 81d21b7

Please sign in to comment.