Example #1
0
        public void QueueDeleteNoWait_WithArguments_DeletesTheQueue()
        {
            // Arrange
            var node = new RabbitServer();

            using (var model = new FakeModel(node))
            {
                const string queueName = "someName";
                model.QueueDeclare(queueName, true, true, true, null);

                // Act
                model.QueueDeleteNoWait(queueName, true, true);

                // Assert
                Assert.True(node.Queues.IsEmpty);
            }
        }
        public void ExchangeBindNoWait_binds_an_exchange_to_a_queue()
        {
            var server = new RabbitServer();

            using (var model = new FakeModel(server))
            {
                const string queueName    = "someQueue";
                const string exchangeName = "someExchange";
                const string routingKey   = "someRoutingKey";
                var          arguments    = new Dictionary <string, object>();

                model.ExchangeDeclare(exchangeName, "direct");
                model.QueueDeclare(queueName);
                model.ExchangeBindNoWait(queueName, exchangeName, routingKey, arguments);
                AssertEx.AssertBinding(server, exchangeName, routingKey, queueName);
            }
        }
Example #3
0
        public void QueuePurge_returns_0_if_queue_does_not_exist()
        {
            var node = new RabbitServer();

            using (var model = new FakeModel(node))
            {
                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_other_queue");
                Assert.Equal(0u, count);

                Assert.False(node.Queues["my_queue"].Messages.IsEmpty);
            }
        }
Example #4
0
        public void MessageCount_returns_the_number_of_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);

                var message        = "hello world!";
                var encodedMessage = Encoding.ASCII.GetBytes(message);
                model.BasicPublish("my_exchange", null, new BasicProperties(), encodedMessage);

                Assert.Equal(1u, model.MessageCount(queueName));
            }
        }
Example #5
0
        public void ConsumerCount_returns_the_number_of_attached_consumers()
        {
            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);

                // Attach 2 consumers
                model.BasicConsume(queueName, true, new DefaultBasicConsumer(model));
                model.BasicConsume(queueName, true, new DefaultBasicConsumer(model));

                Assert.Equal(2u, model.ConsumerCount(queueName));
            }
        }
Example #6
0
        [InlineData(false, 1)] // BasicGet with NO auto-ack should NOT remove the message from the queue
        public void BasicGet_does_not_remove_the_message_from_the_queue_if_not_acked(bool autoAck, 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);

                _ = model.BasicGet("my_queue", autoAck);

                Assert.Equal(expectedMessageCount, server.Queues["my_queue"].Messages.Count);
                Assert.Equal(expectedMessageCount, model.WorkingMessagesForUnitTests.Count);
            }
        }
Example #7
0
        public void BasicPublish_publishes_message()
        {
            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);

                Assert.Single(server.Queues["my_queue"].Messages);
                Assert.Equal(encodedMessage, server.Queues["my_queue"].Messages.First().Body);
            }
        }
Example #8
0
        public void QueueDeclare_creates_a_queue()
        {
            var server = new RabbitServer();

            using (var model = new FakeModel(server))
            {
                const string queueName    = "someQueue";
                const bool   isDurable    = true;
                const bool   isExclusive  = true;
                const bool   isAutoDelete = false;
                var          arguments    = new Dictionary <string, object>();

                model.QueueDeclare(queue: queueName, durable: isDurable, exclusive: isExclusive, autoDelete: isAutoDelete, arguments: arguments);
                Assert.Single(server.Queues);

                var queue = server.Queues.First();
                AssertEx.AssertQueueDetails(queue, queueName, isAutoDelete, arguments, isDurable, isExclusive);
            }
        }
Example #9
0
        public void BasicCancel_works_for_asynchronous_consumers()
        {
            var server = new RabbitServer();

            using (var model = new FakeModel(server))
            {
                model.QueueDeclare("my_queue");
                var expectedConsumerTag = "foo";

                var consumer = new FakeAsyncDefaultBasicConsumer(model);

                model.BasicConsume("my_queue", false, expectedConsumerTag, consumer);
                Assert.True(consumer.IsRunning);
                model.BasicCancel(expectedConsumerTag);
                Assert.False(consumer.IsRunning);

                Assert.Equal(expectedConsumerTag, consumer.LastCancelOkConsumerTag);
            }
        }
Example #10
0
        public void BasicGet_retrieves_message_from_the_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 response = model.BasicGet("my_queue", false);

                Assert.Equal(encodedMessage, response.Body);
                Assert.True(response.DeliveryTag > 0ul);
            }
        }
        public void ExchangeUnbindNoWait_removes_binding()
        {
            var server = new RabbitServer();

            using (var model = new FakeModel(server))
            {
                const string queueName    = "someQueue";
                const string exchangeName = "someExchange";
                const string routingKey   = "someRoutingKey";
                var          arguments    = new Dictionary <string, object>();

                model.ExchangeDeclare(exchangeName, "direct");
                model.QueueDeclare(queueName);
                model.ExchangeBind(exchangeName, queueName, routingKey, arguments);
                model.ExchangeUnbindNoWait(queueName, exchangeName, routingKey, arguments);

                Assert.True(server.Exchanges[exchangeName].Bindings.IsEmpty);
                Assert.True(server.Queues[queueName].Bindings.IsEmpty);
            }
        }
Example #12
0
        public void BasicConsume_works_for_asynchronous_consumers()
        {
            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 FakeAsyncDefaultBasicConsumer(model);
                model.BasicConsume("my_queue", false, consumer);
                Assert.True(consumer.IsRunning);

                Assert.Equal(encodedMessage, consumer.LastDelivery.body);
            }
        }
Example #13
0
        [InlineData(false, 1)] // BasicGet with NO auto-ack should NOT remove the message from the queue
        public void BasicGet_Should_Not_Remove_The_Message_From_Queue_If_Not_Acked(bool autoAck, 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);

                // act
                _ = model.BasicGet("my_queue", autoAck);

                // assert
                Assert.Equal(expectedMessageCount, node.Queues["my_queue"].Messages.Count);
                Assert.Equal(expectedMessageCount, model.WorkingMessages.Count);
            }
        }
Example #14
0
        public void QueueDeclare_CreatesQueue()
        {
            // Arrange
            var node = new RabbitServer();

            using (var model = new FakeModel(node))
            {
                const string queueName    = "someQueue";
                const bool   isDurable    = true;
                const bool   isExclusive  = true;
                const bool   isAutoDelete = false;
                var          arguments    = new Dictionary <string, object>();

                // Act
                model.QueueDeclare(queue: queueName, durable: isDurable, exclusive: isExclusive, autoDelete: isAutoDelete, arguments: arguments);

                // Assert
                Assert.Single(node.Queues);

                var queue = node.Queues.First();
                AssertQueueDetails(queue, queueName, isAutoDelete, arguments, isDurable, isExclusive);
            }
        }
Example #15
0
        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));
            }
        }
Example #16
0
        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);
            }
        }
Example #17
0
        [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);
            }
        }
Example #18
0
        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);
            }
        }
Example #19
0
        [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);
            }
        }