public void TryGetNextMessage_WhenThereIsAMessage_ReturnsValues()
        {
            //Arrange
            ulong deliveryTag = 1;
            string routingKey = "routingKey";
            byte[] byteArray = {0,1,0};
            IBasicProperties basicProperties = new BasicProperties();;
            IBasicConsumer basicConsumer = null;
            var model = Substitute.For<IModel>();
            model.BasicConsume("queueName", false, Arg.Do<IBasicConsumer>(x => basicConsumer = x));

            var sut = new ConfirmingSubscription(model);
            sut.Subscribe("queueName");

            //Act
            basicConsumer.HandleBasicDeliver(string.Empty, deliveryTag, false, string.Empty, routingKey, basicProperties, byteArray);
            DeliveredRabbitMessage message = null;
            bool result = sut.TryGetNextMessage(out message);

            //Assert
            Assert.That(message.DeliveryTag, Is.EqualTo(deliveryTag));
            Assert.That(message.RoutingKey, Is.EqualTo(routingKey));
            Assert.That(message.Body, Is.EqualTo(byteArray));
            Assert.That(message.Properties, Is.EqualTo(basicProperties));
        }
        public void TryGetNextMessage_WhenThereIsAMessage_ReturnsTrue()
        {
            //Arrange
            IBasicConsumer basicConsumer = null;
            var model = Substitute.For<IModel>();
            model.BasicConsume("queueName", false, Arg.Do<IBasicConsumer>(x => basicConsumer = x));

            var sut = new ConfirmingSubscription(model);
            sut.Subscribe("queueName");

            //Act
            basicConsumer.HandleBasicDeliver(string.Empty,1,false,string.Empty,string.Empty,new BasicProperties(), new byte[0]);
            DeliveredRabbitMessage message = null;
            bool result = sut.TryGetNextMessage(out message);

            //Assert
            Assert.That(result, Is.True);
        }
 public static AsyncMessageConsumer HandleMessagesAsync(this IModel source, string queueName)
 {
     var messageSource = new ConfirmingSubscription(source);
     var consumer = new AsyncMessageConsumer(messageSource);
     messageSource.Subscribe(queueName);
     return consumer;
 }
        public async Task RunAction(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var messageSource = new ConfirmingSubscription(_model);
            messageSource.Subscribe(_queueName);
            var consumer = new AsyncMessageConsumer(new LoggingMessageSourceDecorator(messageSource))
                               {
                                   ConcurrencyLevel = this.ConcurrencyLevel
                               };

            _log.Debug(string.Format("Consumer started for queue '{0}'", _queueName));
            while (_model.IsOpen && !cancellationToken.IsCancellationRequested)
            {
                await consumer.ConsumeMessages(this, cancellationToken);
                await Task.Delay(RestTime, cancellationToken);
            }
            _log.Debug(string.Format("Consumer stopped for queue '{0}'", _queueName));
        }
        public void TryGetNextMessage_WhenThereIsNoMessage_ReturnsFalse()
        {
            //Arrange
            IBasicConsumer basicConsumer = null;
            var model = Substitute.For<IModel>();
            model.BasicConsume("queueName", false, Arg.Do<IBasicConsumer>(x => basicConsumer = x));

            var sut = new ConfirmingSubscription(model);
            sut.Subscribe("queueName");

            //Act
            DeliveredRabbitMessage message = null;
            bool result = sut.TryGetNextMessage(out message);

            //Assert
            Assert.That(result, Is.False);
        }