public void DeserializeThrows()
 {
     const String type = "fake-type";
     const String contentType = "application/null";
     var body = new Byte[] { };
     var args = new BasicDeliverEventArgs
                    {
                        Body = body,
                        BasicProperties = new BasicProperties
                        {
                            ContentType = contentType,
                            Type = type
                        }
                    };
     var context = ConsumedMessageContext.FromBasicDeliverEventArgs(args);
     var runtimeType = typeof(Foo);
     var serializer = new Mock<ISerializer>();
     serializer.Setup(_ => _.Deserialize(body, runtimeType, new UTF8Encoding(true)))
               .Throws(new Exception("boom"));
     var serializationConfiguration = new SerializationConfigurationWrapper(serializer.Object);
     var resolver = new Mock<IMessageTypeResolver>();
     resolver.Setup(_ => _.Resolve(context)).Returns(new MessageBinding(type, runtimeType));
     var builder = new ConsumedMessageBuilder(serializationConfiguration, resolver.Object);
     var message = builder.Build(args);
     Assert.IsType<CorruptedMessage>(message);
 }
 public void MissingContentType()
 {
     const String contentType = "application/null";
     var serializationConfiguration = new SerializationConfigurationWrapper(NullSerializer.Instance);
     var resolver = new Mock<IMessageTypeResolver>();
     var builder = new ConsumedMessageBuilder(serializationConfiguration, resolver.Object);
     var message = builder.Build(new BasicDeliverEventArgs
                                     {
                                         BasicProperties = new BasicProperties { ContentType = contentType }
                                     });
     Assert.IsType<UnsupportedMessage>(message);
 }
 public void MissingContentType()
 {
     const String contentType = "application/null";
     var serializerFactory = new Mock<ISerializerFactory>();
     serializerFactory.Setup(_ => _.Create(contentType)).Returns(NullSerializer.Instance);
     var resolver = new Mock<IMessageTypeResolver>();
     var builder = new ConsumedMessageBuilder(serializerFactory.Object, resolver.Object);
     var message = builder.Build(new BasicDeliverEventArgs
                                     {
                                         BasicProperties = new BasicProperties { ContentType = contentType }
                                     });
     Assert.IsType<UnsupportedMessage>(message);
 }
 public void CannotResolve()
 {
     const String type = "fake-type";
     var serializerFactory = new Mock<ISerializerFactory>();
     var resolver = new Mock<IMessageTypeResolver>();
     resolver.Setup(_ => _.Resolve(type)).Returns(EmptyMessageType.Instance);
     var builder = new ConsumedMessageBuilder(serializerFactory.Object, resolver.Object);
     var message = builder.Build(new BasicDeliverEventArgs
                                     {
                                         BasicProperties = new BasicProperties { Type = type }
                                     });
     Assert.IsType<UnresolvedMessage>(message);
 }
 public void ExceptionOnResolve()
 {
     const String type = "fake-type";
     var serializationConfiguration = new SerializationConfiguration();
     var resolver = new Mock<IMessageTypeResolver>();
     resolver.Setup(_ => _.Resolve(It.Is<ConsumedMessageContext>(__ => __.MessageType == type)))
             .Throws<Exception>();
     var builder = new ConsumedMessageBuilder(serializationConfiguration, resolver.Object);
     var message = builder.Build(new BasicDeliverEventArgs
                                     {
                                         BasicProperties = new BasicProperties { Type = type }
                                     });
     Assert.IsType<UnresolvedMessage>(message);
 }
Exemple #6
0
        private IConnection CreateConnection(RabbitMQ.Client.IConnection connection,
                                             IModel outboundModel)
        {
            var builder = new ConsumedMessageBuilder(_configuration.SerializationConfiguration,
                                                     _configuration.MessageTypeResolver);
            var outboundChannel = _configuration.OutboundChannelBuilder(outboundModel,
                                                                        _configuration);
            var consumers = _promises.Select(_ =>
                                                {
                                                    var model = CreateInboundModel(connection,
                                                                                   _configuration.PrefetchSize,
                                                                                   _configuration.PrefetchCount);
                                                    var consumer = _(builder).BuildConsumer(new InboundChannel(model),
                                                                                            outboundChannel);
                                                    return new { Model = model, Consumer = consumer };
                                                })
                                     .ToList();

            foreach (var consumer in consumers)
                consumer.Consumer.Declare(consumer.Model);

            return new Connection(connection,
                                  consumers.Select(_ => _.Consumer),
                                  outboundChannel,
                                  _configuration);
        }
Exemple #7
0
        protected virtual void ConnectToBroker()
        {
            if (_outboundModel != null && !_outboundModel.IsClosed) return;
            _connection = _connectionBuilder.CreateConnection(_configuration.EndpointUri);
            //Logger.DebugFormat("RMQMessagingGateway: Opening channel to Rabbit MQ on connection {0}", Configuration.AMPQUri.GetSanitizedUri());
            _outboundModel = _connection.CreateModel();
            //When AutoClose is true, the last channel to close will also cause the connection to close1. If it is set to
            //true before any channel is created, the connection will close then and there.
            if (_connection.AutoClose == false)
                _connection.AutoClose = true;

            foreach (var exchange in _exchanges)
                exchange.Declare(_outboundModel);

            foreach (var queue in _queues)
                queue.Declare(_outboundModel);

            foreach (var binding in _bindings)
                binding.Declare(_outboundModel);

            var builder = new ConsumedMessageBuilder(_configuration.SerializationConfiguration, _configuration.MessageTypeResolver);
            _outboundChannel = _configuration.OutboundChannelBuilder(_outboundModel, _configuration);
            var consumers = _promises.Select(_ =>
            {
                var model = CreateInboundModel(_connection, _configuration.PrefetchSize, _configuration.PrefetchCount);
                var consumer = _(builder).BuildConsumer(new InboundChannel(model), _outboundChannel);
                return new {Model = model, Consumer = consumer};
            })
                .ToList();

            foreach (var consumer in consumers)
                consumer.Consumer.Declare(consumer.Model);
            _consumers = consumers.Select(_ => _.Consumer);
        }
Exemple #8
0
        private void Subscribe(Action<SubscriptionConfiguration> configure,
                               Func<IConsumedMessageBuilder, SubscriptionConfiguration, ConsumerBase> func,
                               IFallbackStrategy fallbackStrategy)
        {
            var configuration = new SubscriptionConfiguration(fallbackStrategy);
            configure(configuration);
            var builder = new ConsumedMessageBuilder(new SerializerFactory(), _resolver);

            _model.BasicConsume(_name, false, func(builder, configuration));
        }
Exemple #9
0
        public IConnection Connect()
        {
            var connection = _connectionBuilder.CreateConnection(_configuration.EndpointUri);
            var outboundModel = connection.CreateModel();

            foreach (var exchange in _exchanges)
                exchange.Declare(outboundModel);

            foreach (var queue in _queues)
                queue.Declare(outboundModel);

            foreach (var binding in _bindings)
                binding.Declare(outboundModel);

            var builder = new ConsumedMessageBuilder(_configuration.SerializationConfiguration,
                                                     _configuration.MessageTypeResolver);
            var outboundChannel = _configuration.OutboundChannelBuilder(outboundModel, _configuration);
            var consumers = _promises.Select(_ =>
                                             {
                                                 var model = CreateInboundModel(connection,
                                                                                _configuration.PrefetchSize,
                                                                                _configuration.PrefetchCount);
                                                 var consumer = _(builder).BuildConsumer(new InboundChannel(model),
                                                                                         outboundChannel);
                                                 return new { Model = model, Consumer = consumer };
                                             })
                                     .ToList();

            foreach (var consumer in consumers)
                consumer.Consumer.Declare(consumer.Model);

            return new Connection(connection,
                                  consumers.Select(_ => _.Consumer),
                                  outboundChannel,
                                  _configuration);
        }