Esempio n. 1
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));
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
0
        public void BasicAck()
        {
            var node = new RabbitServer();

            using (var model = new FakeModel(node))
            {
                model.ExchangeDeclare("my_exchange", ExchangeType.Direct);
                model.QueueDeclarePassive("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);
                model.BasicConsume("my_queue", false, new EventingBasicConsumer(model));

                // Act
                var deliveryTag = model.WorkingMessages.First().Key;
                model.BasicAck(deliveryTag, false);

                // Assert
                Assert.Empty(node.Queues["my_queue"].Messages);
            }
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
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));
            }
        }
Esempio n. 9
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);
            }
        }