public void QueuePurge_removes_all_messages_from_specified_queue() { var node = new RabbitServer(); using (var model = new FakeModel(node)) { model.QueueDeclare("my_other_queue"); node.Queues["my_other_queue"].Messages.Enqueue(new RabbitMessage()); node.Queues["my_other_queue"].Messages.Enqueue(new RabbitMessage()); model.QueueDeclare("my_queue"); node.Queues["my_queue"].Messages.Enqueue(new RabbitMessage()); node.Queues["my_queue"].Messages.Enqueue(new RabbitMessage()); node.Queues["my_queue"].Messages.Enqueue(new RabbitMessage()); node.Queues["my_queue"].Messages.Enqueue(new RabbitMessage()); var count = model.QueuePurge("my_queue"); Assert.Equal(4u, count); Assert.True(node.Queues["my_queue"].Messages.IsEmpty); Assert.False(node.Queues["my_other_queue"].Messages.IsEmpty); } }
public void BasicCancel_removes_a_consumer() { var server = new RabbitServer(); using (var model = new FakeModel(server)) { model.QueueDeclare("my_queue"); var expectedConsumerTag = "foo"; var actualConsumerTag = ""; var consumer = new EventingBasicConsumer(model) { ConsumerTag = expectedConsumerTag }; consumer.Unregistered += (s, e) => actualConsumerTag = e.ConsumerTag; model.BasicConsume("my_queue", false, expectedConsumerTag, consumer); Assert.True(consumer.IsRunning); model.BasicCancel(expectedConsumerTag); Assert.False(consumer.IsRunning); Assert.Equal(expectedConsumerTag, actualConsumerTag); } }
public void BasicAck_removes_message_from_queue() { var server = new RabbitServer(); using (var model = new FakeModel(server)) { model.ExchangeDeclare("my_exchange", ExchangeType.Direct); model.QueueDeclare("my_queue"); model.ExchangeBind("my_queue", "my_exchange", null); var message = "hello world!"; var encodedMessage = Encoding.ASCII.GetBytes(message); model.BasicPublish("my_exchange", null, new BasicProperties(), encodedMessage); var consumer = new EventingBasicConsumer(model); model.BasicConsume("my_queue", false, consumer); Assert.True(consumer.IsRunning); var deliveryTag = model.WorkingMessagesForUnitTests.First().Key; model.BasicAck(deliveryTag, false); Assert.Empty(server.Queues["my_queue"].Messages); } }
[InlineData(false, 0)] // If requeue param to BasicNack is false, the message that is nacked should be removed from Rabbit public void Nacking_Message_Should_Not_Reenqueue_Brand_New_Message(bool requeue, int expectedMessageCount) { // arrange var node = new RabbitServer(); using (var model = new FakeModel(node)) { model.ExchangeDeclare("my_exchange", ExchangeType.Direct); model.QueueDeclare("my_queue"); model.ExchangeBind("my_queue", "my_exchange", null); var encodedMessage = Encoding.ASCII.GetBytes("hello world!"); model.BasicPublish("my_exchange", null, new BasicProperties(), encodedMessage); model.BasicConsume("my_queue", false, new EventingBasicConsumer(model)); // act var deliveryTag = model.WorkingMessages.First().Key; model.BasicNack(deliveryTag, false, requeue); // assert Assert.Equal(expectedMessageCount, node.Queues["my_queue"].Messages.Count); Assert.Equal(expectedMessageCount, model.WorkingMessages.Count); } }
public void QueueUnbind_RemovesBinding() { // Arrange var node = new RabbitServer(); using (var model = new FakeModel(node)) { const string queueName = "someQueue"; const string exchangeName = "someExchange"; const string routingKey = "someRoutingKey"; var arguments = new Dictionary <string, object>(); model.ExchangeDeclare(exchangeName, "direct"); model.QueueDeclarePassive(queueName); model.ExchangeBind(exchangeName, queueName, routingKey, arguments); // Act model.QueueUnbind(queueName, exchangeName, routingKey, arguments); // Assert Assert.True(node.Exchanges[exchangeName].Bindings.IsEmpty); Assert.True(node.Queues[queueName].Bindings.IsEmpty); } }
public void MessageCount_returns_the_number_of_non_consumed_messages_in_the_queue() { var server = new RabbitServer(); using (var model = new FakeModel(server)) { const string queueName = "myQueue"; model.QueueDeclare(queueName); model.ExchangeDeclare("my_exchange", ExchangeType.Direct); model.ExchangeBind(queueName, "my_exchange", null); for (var i = 0; i < 10; i++) { var message = $"hello world: {i}"; var encodedMessage = Encoding.ASCII.GetBytes(message); model.BasicPublish("my_exchange", null, new BasicProperties(), encodedMessage); } // Consume 4 messages var consumer = new EventingBasicConsumer(model); var consumptionCount = 0; consumer.Received += (s, e) => { if (consumptionCount >= 4) { return; } model.BasicAck(e.DeliveryTag, false); consumptionCount++; }; model.BasicConsume(queueName, true, consumer); Assert.Equal(6u, model.MessageCount(queueName)); } }
[InlineData(false, 0)] // If requeue param to BasicNack is false, the message that is nacked should be removed from Rabbit public void Nacking_message_does_not_reenqueue_a_brand_new_message(bool requeue, int expectedMessageCount) { var server = new RabbitServer(); using (var model = new FakeModel(server)) { model.ExchangeDeclare("my_exchange", ExchangeType.Direct); model.QueueDeclare("my_queue"); model.ExchangeBind("my_queue", "my_exchange", null); var encodedMessage = Encoding.ASCII.GetBytes("hello world!"); model.BasicPublish("my_exchange", null, new BasicProperties(), encodedMessage); var consumer = new EventingBasicConsumer(model); model.BasicConsume("my_queue", false, consumer); Assert.True(consumer.IsRunning); var deliveryTag = model.WorkingMessagesForUnitTests.First().Key; model.BasicNack(deliveryTag, false, requeue); Assert.Equal(expectedMessageCount, server.Queues["my_queue"].Messages.Count); Assert.Equal(expectedMessageCount, model.WorkingMessagesForUnitTests.Count); } }
public FakeModel(RabbitServer rabbitServer) => server = rabbitServer;
public FakeConnectionFactory(RabbitServer rabbitServer) => server = rabbitServer ?? throw new ArgumentNullException(nameof(rabbitServer));
public static void AssertBinding(RabbitServer server, string exchangeName, string routingKey, string queueName) { Assert.Single(server.Exchanges[exchangeName].Bindings); Assert.Equal(routingKey, server.Exchanges[exchangeName].Bindings.First().Value.RoutingKey); Assert.Equal(queueName, server.Exchanges[exchangeName].Bindings.First().Value.Queue.Name); }
public FakeConnection(RabbitServer rabbitServer, string name) { server = rabbitServer ?? throw new ArgumentNullException(nameof(rabbitServer)); ClientProvidedName = name ?? string.Empty; Models = new List <IModel>(); }
public FakeConnection(RabbitServer rabbitServer) : this(rabbitServer, null) { }