Ejemplo n.º 1
0
        public void AppendTo_sets_message_type_and_alternatives_for_a_versioned_message()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var property = MessageTypeProperty.CreateForMessageType( typeof( MyMessageV2 ), typeNameSerialiser );
            var properties = new MessageProperties();

            property.AppendTo( properties );

            Assert.That( properties.Type, Is.EqualTo( typeNameSerialiser.Serialize( typeof( MyMessageV2 ) ) ) );
            Assert.That( properties.Headers[ AlternativeMessageTypesHeaderKey ], Is.EqualTo( typeNameSerialiser.Serialize( typeof( MyMessage ) ) ) );
        }
        public void AppendTo_sets_message_type_and_alternatives_for_a_versioned_message()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var property           = MessageTypeProperty.CreateForMessageType(typeof(MyMessageV2), typeNameSerialiser);
            var properties         = new MessageProperties();

            property.AppendTo(properties);

            Assert.Equal(properties.Type, typeNameSerialiser.Serialize(typeof(MyMessageV2)));
            Assert.Equal(properties.Headers[AlternativeMessageTypesHeaderKey], typeNameSerialiser.Serialize(typeof(MyMessage)));
        }
Ejemplo n.º 3
0
        public void MessageTypeProperty_is_created_correctly_from_message_properties_for_versioned_message()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var properties = new MessageProperties {Type = typeNameSerialiser.Serialize( typeof( MyMessageV2 ) )};
            var encodedAlternativeMessageTypes = Encoding.UTF8.GetBytes( typeNameSerialiser.Serialize( typeof( MyMessage ) ) );
            properties.Headers.Add( AlternativeMessageTypesHeaderKey, encodedAlternativeMessageTypes );

            var property = MessageTypeProperty.ExtractFromProperties( properties, typeNameSerialiser );
            var messageType = property.GetMessageType();

            Assert.That( messageType.Type, Is.EqualTo( typeof( MyMessageV2 ) ) );
        }
Ejemplo n.º 4
0
        public void MessageTypeProperty_is_created_correctly_from_message_properties_for_versioned_message()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var properties         = new MessageProperties {
                Type = typeNameSerialiser.Serialize(typeof(MyMessageV2))
            };
            var encodedAlternativeMessageTypes = Encoding.UTF8.GetBytes(typeNameSerialiser.Serialize(typeof(MyMessage)));

            properties.Headers.Add(AlternativeMessageTypesHeaderKey, encodedAlternativeMessageTypes);

            var property    = MessageTypeProperty.ExtractFromProperties(properties, typeNameSerialiser);
            var messageType = property.GetMessageType();

            Assert.That(messageType.Type, Is.EqualTo(typeof(MyMessageV2)));
        }
Ejemplo n.º 5
0
        public void The_default_queue_naming_convention_should_use_the_TypeNameSerializers_Serialize_method_then_an_underscore_then_the_subscription_id()
        {
            const string subscriptionId = "test";
            var          result         = conventions.QueueNamingConvention(typeof(TestMessage), subscriptionId);

            result.ShouldEqual(TypeNameSerializer.Serialize(typeof(TestMessage)) + "_" + subscriptionId);
        }
        public IMessagesMove AsType(Type messageType)
        {
            var typeNameSerializer = new TypeNameSerializer();

            MessageType = typeNameSerializer.Serialize(messageType);
            return(this);
        }
Ejemplo n.º 7
0
        public void Should_throw_exception_if_type_is_null()
        {
            // Arrange
            var serializer = new TypeNameSerializer();

            // Action
            serializer.Serialize(null);
        }
Ejemplo n.º 8
0
        public void MessageTypeProperty_is_created_correctly_from_message_properties_for_unversioned_message()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var properties = new MessageProperties {Type = typeNameSerialiser.Serialize( typeof( MyMessage ) )};

            var property = MessageTypeProperty.ExtractFromProperties( properties, typeNameSerialiser );
            var messageType = property.GetMessageType();

            Assert.That( messageType.Type, Is.EqualTo( typeof( MyMessage ) ) );
        }
Ejemplo n.º 9
0
        public void AppendTo_sets_message_type_and_no_alternatives_for_an_unversioned_message()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var property           = MessageTypeProperty.CreateForMessageType(typeof(MyMessage), typeNameSerialiser);
            var properties         = new MessageProperties();

            property.AppendTo(properties);

            Assert.That(properties.Type, Is.EqualTo(typeNameSerialiser.Serialize(typeof(MyMessage))));
            Assert.That(properties.Headers.ContainsKey(AlternativeMessageTypesHeaderKey), Is.False);
        }
Ejemplo n.º 10
0
        public void GetType_returns_first_available_alternative_if_message_type_unavailable()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var v1                      = typeNameSerialiser.Serialize(typeof(MyMessage));
            var v2                      = typeNameSerialiser.Serialize(typeof(MyMessageV2));
            var vUnknown                = v2.Replace("MyMessageV2", "MyUnknownMessage");
            var alternativeTypes        = string.Concat(v2, ";", v1);
            var encodedAlternativeTypes = Encoding.UTF8.GetBytes(alternativeTypes);

            var properties = new MessageProperties {
                Type = vUnknown
            };

            properties.Headers.Add(AlternativeMessageTypesHeaderKey, encodedAlternativeTypes);

            var property    = MessageTypeProperty.ExtractFromProperties(properties, typeNameSerialiser);
            var messageType = property.GetMessageType();

            Assert.That(messageType.Type, Is.EqualTo(typeof(MyMessageV2)));
        }
Ejemplo n.º 11
0
        public void Should_throw_exception_if_type_fullName_is_null()
        {
            // Arrange
            var type = Substitute.For <Type>();

            type.FullName.Returns((string)null);
            var serializer = new TypeNameSerializer();

            // Action
            serializer.Serialize(type);
        }
Ejemplo n.º 12
0
        public void MessageTypeProperty_is_created_correctly_from_message_properties_for_unversioned_message()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var properties         = new MessageProperties {
                Type = typeNameSerialiser.Serialize(typeof(MyMessage))
            };

            var property    = MessageTypeProperty.ExtractFromProperties(properties, typeNameSerialiser);
            var messageType = property.GetMessageType();

            Assert.That(messageType.Type, Is.EqualTo(typeof(MyMessage)));
        }
Ejemplo n.º 13
0
 private void CreateBus(Conventions conventions, IModel model)
 {
     bus = new RabbitBus(
         x => TypeNameSerializer.Serialize(x.GetType()),
         new JsonSerializer(),
         new MockConsumerFactory(),
         new MockConnectionFactory(new MockConnection(model)),
         new MockLogger(),
         CorrelationIdGenerator.GetCorrelationId,
         conventions
         );
 }
Ejemplo n.º 14
0
        public void Publish <T>(T message) where T : class
        {
            var properties = new MessageProperties();

            properties.Type      = typeNameSerializer.Serialize(message.GetType());
            properties.MessageId = Guid.NewGuid().ToString();
            //TODO: set correlation Id

            messages.Add(new MessageWithProperties
            {
                Message    = message,
                Properties = properties
            });
        }
Ejemplo n.º 15
0
        private void CreateBus(IConventions conventions, IModel model)
        {
            var advancedBus = new RabbitAdvancedBus(
                new ConnectionConfiguration(),
                new MockConnectionFactory(new MockConnection(model)),
                TypeNameSerializer.Serialize,
                new JsonSerializer(),
                new MockConsumerFactory(),
                new MockLogger(),
                CorrelationIdGenerator.GetCorrelationId,
                new Conventions()
                );

            bus = new RabbitBus(
                x => TypeNameSerializer.Serialize(x.GetType()),
                new MockLogger(),
                conventions,
                advancedBus
                );
        }
Ejemplo n.º 16
0
        public void GetType_throws_exception_if_all_types_unavailable()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var v2                      = typeNameSerialiser.Serialize(typeof(MyMessageV2));
            var vUnknown1               = v2.Replace("MyMessageV2", "MyUnknownMessage");
            var vUnknown2               = v2.Replace("MyMessageV2", "MyUnknownMessageV2");
            var vUnknown3               = v2.Replace("MyMessageV2", "MyUnknownMessageV3");
            var alternativeTypes        = string.Concat(vUnknown2, ";", vUnknown1);
            var encodedAlternativeTypes = Encoding.UTF8.GetBytes(alternativeTypes);

            var properties = new MessageProperties {
                Type = vUnknown3
            };

            properties.Headers.Add(AlternativeMessageTypesHeaderKey, encodedAlternativeTypes);

            var property = MessageTypeProperty.ExtractFromProperties(properties, typeNameSerialiser);

            Assert.Throws <EasyNetQException>(() => property.GetMessageType());
        }
        public virtual void HandleConsumerError(ConsumerExecutionContext context, Exception exception)
        {
            Preconditions.CheckNotNull(context, "context");
            Preconditions.CheckNotNull(exception, "exception");

            try
            {
                Connect();

                using (var model = connection.CreateModel())
                {
                    var errorExchange = DeclareErrorExchangeQueueStructure(model, context.Info.RoutingKey);

                    var messageBody = CreateErrorMessage(context, exception);
                    var properties  = model.CreateBasicProperties();
                    properties.SetPersistent(true);
                    properties.Type = TypeNameSerializer.Serialize(typeof(Error));

                    model.BasicPublish(errorExchange, context.Info.RoutingKey, properties, messageBody);
                }
            }
            catch (BrokerUnreachableException)
            {
                // thrown if the broker is unreachable during initial creation.
                logger.ErrorWrite("EasyNetQ Consumer Error Handler cannot connect to Broker\n" +
                                  CreateConnectionCheckMessage());
            }
            catch (OperationInterruptedException interruptedException)
            {
                // thrown if the broker connection is broken during declare or publish.
                logger.ErrorWrite("EasyNetQ Consumer Error Handler: Broker connection was closed while attempting to publish Error message.\n" +
                                  string.Format("Message was: '{0}'\n", interruptedException.Message) +
                                  CreateConnectionCheckMessage());
            }
            catch (Exception unexpecctedException)
            {
                // Something else unexpected has gone wrong :(
                logger.ErrorWrite("EasyNetQ Consumer Error Handler: Failed to publish error message\nException is:\n"
                                  + unexpecctedException);
            }
        }
Ejemplo n.º 18
0
        public void Should_serialize_a_type_name()
        {
            var typeName = TypeNameSerializer.Serialize(typeof(string));

            typeName.ShouldEqual(expectedTypeName);
        }
Ejemplo n.º 19
0
        public void GetType_throws_exception_if_all_types_unavailable()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var v2 = typeNameSerialiser.Serialize( typeof( MyMessageV2 ) );
            var vUnknown1 = v2.Replace( "MyMessageV2", "MyUnknownMessage" );
            var vUnknown2 = v2.Replace( "MyMessageV2", "MyUnknownMessageV2" );
            var vUnknown3 = v2.Replace( "MyMessageV2", "MyUnknownMessageV3" );
            var alternativeTypes = string.Concat( vUnknown2, ";", vUnknown1 );
            var encodedAlternativeTypes = Encoding.UTF8.GetBytes( alternativeTypes );

            var properties = new MessageProperties {Type = vUnknown3 };
            properties.Headers.Add( AlternativeMessageTypesHeaderKey, encodedAlternativeTypes );

            var property = MessageTypeProperty.ExtractFromProperties( properties, typeNameSerialiser );
            Assert.Throws<EasyNetQException>( () => property.GetMessageType() );
        }
Ejemplo n.º 20
0
        public void GetType_returns_first_available_alternative_if_message_type_unavailable()
        {
            var typeNameSerialiser = new TypeNameSerializer();
            var v1 = typeNameSerialiser.Serialize( typeof( MyMessage ) );
            var v2 = typeNameSerialiser.Serialize( typeof( MyMessageV2 ) );
            var vUnknown = v2.Replace( "MyMessageV2", "MyUnknownMessage" );
            var alternativeTypes = string.Concat( v2, ";", v1 );
            var encodedAlternativeTypes = Encoding.UTF8.GetBytes( alternativeTypes );

            var properties = new MessageProperties {Type = vUnknown };
            properties.Headers.Add( AlternativeMessageTypesHeaderKey, encodedAlternativeTypes );

            var property = MessageTypeProperty.ExtractFromProperties( properties, typeNameSerialiser );
            var messageType = property.GetMessageType();

            Assert.That( messageType.Type, Is.EqualTo( typeof( MyMessageV2 ) ) );
        }
Ejemplo n.º 21
0
        public void Should_serialize_a_custom_type()
        {
            var typeName = TypeNameSerializer.Serialize(typeof(TypeNameSerializer));

            typeName.ShouldEqual(expectedCustomTypeName);
        }
Ejemplo n.º 22
0
        public void The_default_topic_naming_convention_should_use_the_TypeNameSerializers_Serialize_method()
        {
            var result = conventions.TopicNamingConvention(typeof(TestMessage));

            result.ShouldEqual(TypeNameSerializer.Serialize(typeof(TestMessage)));
        }
Ejemplo n.º 23
0
        public void The_default_rpc_routingkey_naming_convention_should_use_the_TypeNameSerializers_Serialize_method()
        {
            var result = conventions.RpcRoutingKeyNamingConvention(typeof(TestMessage));

            result.ShouldEqual(TypeNameSerializer.Serialize(typeof(TestMessage)));
        }