示例#1
0
        public async Task Invalid_commands_are_discarded([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut, string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, string messageId, Guid correlationId, FirstTestCommand command)
        {
            // At least one subscription is needed to inject invalid messages
            sut.SubscribeToCommand <SecondTestCommand>();

            var sequence = await sut.StartAsync();

            var encoding = Encoding.UTF8;

            IBasicProperties properties = new BasicProperties
            {
                MessageId       = messageId,
                ContentEncoding = encoding.WebName,
                Headers         = new Dictionary <string, object>
                {
                    ["Nybus:MessageId"]     = encoding.GetBytes(messageId),
                    ["Nybus:MessageType"]   = encoding.GetBytes(DescriptorName(command.GetType())),
                    ["Nybus:CorrelationId"] = correlationId.ToByteArray()
                }
            };

            var body = configuration.Serializer.SerializeObject(command, encoding);

            var incomingMessages = sequence.DumpInList();

            sut.Consumers.First().Value.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            Assert.That(incomingMessages, Is.Empty);

            Mock.Get(configuration.ConnectionFactory.CreateConnection().CreateModel()).Verify(p => p.BasicNack(deliveryTag, It.IsAny <bool>(), It.IsAny <bool>()));
        }
示例#2
0
        public async Task NotifyFail_can_handle_closed_connections([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut, string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, string messageId, Guid correlationId, FirstTestCommand command, ShutdownEventArgs shutdownEventArgs)
        {
            sut.SubscribeToCommand <FirstTestCommand>();

            var sequence = await sut.StartAsync().ConfigureAwait(false);

            var encoding = Encoding.UTF8;

            IBasicProperties properties = new BasicProperties
            {
                MessageId       = messageId,
                ContentEncoding = encoding.WebName,
                Headers         = new Dictionary <string, object>
                {
                    ["Nybus:MessageId"]     = encoding.GetBytes(messageId),
                    ["Nybus:MessageType"]   = encoding.GetBytes(DescriptorName(command.GetType())),
                    ["Nybus:CorrelationId"] = correlationId.ToByteArray()
                }
            };

            var body = configuration.Serializer.SerializeObject(command, encoding);

            var incomingMessages = sequence.DumpInList();

            sut.Consumers.First().Value.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            Mock.Get(configuration.ConnectionFactory.CreateConnection().CreateModel()).Setup(p => p.BasicNack(It.IsAny <ulong>(), It.IsAny <bool>(), It.IsAny <bool>())).Throws(new AlreadyClosedException(shutdownEventArgs));

            await sut.NotifyFailAsync(incomingMessages.First());

            Mock.Get(configuration.ConnectionFactory.CreateConnection().CreateModel()).Verify(p => p.BasicNack(deliveryTag, It.IsAny <bool>(), It.IsAny <bool>()));
        }
示例#3
0
        public async Task RabbitMq_headers_are_read_from_incoming_message([Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut, string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, string messageId, Guid correlationId, FirstTestCommand Command, string headerKey, string headerValue)
        {
            sut.SubscribeToCommand <FirstTestCommand>();

            var sequence = await sut.StartAsync();

            var encoding = Encoding.UTF8;

            IBasicProperties properties = new BasicProperties
            {
                MessageId       = messageId,
                ContentEncoding = encoding.WebName,
                Headers         = new Dictionary <string, object>
                {
                    ["Nybus:MessageId"]       = encoding.GetBytes(messageId),
                    ["Nybus:MessageType"]     = encoding.GetBytes(DescriptorName(Command.GetType())),
                    ["Nybus:CorrelationId"]   = correlationId.ToByteArray(),
                    [$"RabbitMq:{headerKey}"] = headerValue
                }
            };

            var body = configuration.Serializer.SerializeObject(Command, encoding);

            var incomingMessages = sequence.DumpInList();

            sut.Consumers.First().Value.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            Assert.That(incomingMessages, Has.Exactly(1).InstanceOf <CommandMessage <FirstTestCommand> >());

            var message = incomingMessages[0] as CommandMessage <FirstTestCommand>;

            Assert.That(message, Is.Not.Null);
            Assert.That(message.MessageId, Is.EqualTo(messageId));
            Assert.That(message.MessageType, Is.EqualTo(MessageType.Command));
            Assert.That(message.Type, Is.EqualTo(Command.GetType()));
            Assert.That(message.Command, Is.Not.Null);

            Assert.That(message.Headers, Contains.Key($"RabbitMq:{headerKey}"));
            Assert.That(message.Headers[$"RabbitMq:{headerKey}"], Is.EqualTo(headerValue));
        }
示例#4
0
        public async Task Issue90([Frozen] ISerializer serializer, [Frozen] IRabbitMqConfiguration configuration, RabbitMqBusEngine sut, string consumerTag, ulong headerDeliveryTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, string messageId, Guid correlationId, DateTimeOffset sentOn, FirstTestCommand testCommand)
        {
            Mock.Get(serializer).Setup(p => p.DeserializeObject(It.IsAny <byte[]>(), It.IsAny <Type>(), It.IsAny <Encoding>())).Returns(testCommand);

            sut.SubscribeToCommand <FirstTestCommand>();

            var sequence = await sut.StartAsync();

            var encoding = Encoding.UTF8;

            IBasicProperties properties = new BasicProperties
            {
                MessageId       = messageId,
                ContentEncoding = encoding.WebName,
                Headers         = new Dictionary <string, object>
                {
                    ["Nybus:MessageId"]      = encoding.GetBytes(messageId),
                    ["Nybus:MessageType"]    = encoding.GetBytes(DescriptorName(testCommand.GetType())),
                    ["Nybus:CorrelationId"]  = correlationId.ToByteArray(),
                    ["RabbitMq:DeliveryTag"] = headerDeliveryTag
                }
            };

            var body = configuration.Serializer.SerializeObject(testCommand, encoding);

            var incomingMessages = sequence.DumpInList();

            sut.Consumers.First().Value.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);

            Assert.That(incomingMessages, Has.Exactly(1).InstanceOf <CommandMessage <FirstTestCommand> >());

            var message = incomingMessages[0] as CommandMessage <FirstTestCommand>;

            Assert.That(message.Headers, Contains.Key("RabbitMq:DeliveryTag"));
            Assert.That(message.Headers["RabbitMq:DeliveryTag"], Is.EqualTo(deliveryTag.ToString()));
        }