Example #1
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);
            }
        }
Example #2
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 #3
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));
            }
        }