Esempio n. 1
0
        public async Task Hosts_can_exchange_commands(FirstTestCommand testCommand, CommandReceivedAsync <FirstTestCommand> commandReceived)
        {
            var sender = CreateNybusHost(nybus =>
            {
                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(configuration => configuration.ConnectionFactory = new ConnectionFactory());
                });
            });

            var receiver = CreateNybusHost(nybus =>
            {
                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(configuration => configuration.ConnectionFactory = new ConnectionFactory());
                });

                nybus.SubscribeToCommand(commandReceived);
            });

            await sender.StartAsync();

            await receiver.StartAsync();

            await sender.Bus.InvokeCommandAsync(testCommand);

            await Task.Delay(TimeSpan.FromMilliseconds(50));

            await receiver.StopAsync();

            await sender.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()));
        }
Esempio n. 2
0
        public async Task Host_can_loopback_commands(FirstTestCommand testCommand)
        {
            var settings = new Dictionary <string, string>
            {
                ["Nybus:ErrorPolicy:ProviderName"] = "retry",
                ["Nybus:ErrorPolicy:MaxRetries"]   = "5",
            };

            var configurationBuilder = new ConfigurationBuilder().AddInMemoryCollection(settings);
            var configuration        = configurationBuilder.Build();

            var handler = Mock.Of <FirstTestCommandHandler>();

            var host = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand <FirstTestCommand, FirstTestCommandHandler>(handler);

                nybus.UseConfiguration(configuration);

                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(c => c.ConnectionFactory = new ConnectionFactory());
                });
            });

            await host.StartAsync();

            await host.Bus.InvokeCommandAsync(testCommand);

            await Task.Delay(TimeSpan.FromMilliseconds(50));

            await host.StopAsync();

            Mock.Get(handler).Verify(p => p.HandleAsync(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()), Times.Once);
        }
Esempio n. 3
0
        public async Task Host_can_loopback_commands(FirstTestCommand testCommand)
        {
            var handler = Mock.Of <FirstTestCommandHandler>();

            var host = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand <FirstTestCommand, FirstTestCommandHandler>(handler);

                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(c => c.ConnectionFactory = new ConnectionFactory());
                });
            });

            await host.StartAsync();

            await host.Bus.InvokeCommandAsync(testCommand);

            await Task.Delay(TimeSpan.FromMilliseconds(50));

            await host.StopAsync();

            Mock.Get(handler).Verify(p => p.HandleAsync(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()), Times.Once);
        }
Esempio n. 4
0
        public void DeserializeObject_returns_object_from_byte_representation(JsonSerializer sut, FirstTestCommand testObject)
        {
            var json  = JsonConvert.SerializeObject(testObject);
            var bytes = Encoding.UTF8.GetBytes(json);

            var result = sut.DeserializeObject(bytes, typeof(FirstTestCommand), Encoding.UTF8) as FirstTestCommand;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Message, Is.EqualTo(testObject.Message));
        }
Esempio n. 5
0
        public void SerializeObject_returns_byte_representation_of_object(JsonSerializer sut, FirstTestCommand testObject)
        {
            var json  = JsonConvert.SerializeObject(testObject);
            var bytes = Encoding.UTF8.GetBytes(json);

            var result = sut.SerializeObject(testObject, Encoding.UTF8);

            CollectionAssert.AreEqual(bytes, result);
        }
Esempio n. 6
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));
        }
Esempio n. 7
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>()));
        }
Esempio n. 8
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>()));
        }
Esempio n. 9
0
        public async Task Issue90(CommandReceivedAsync <FirstTestCommand> commandReceived, Exception exception, FirstTestCommand testCommand)
        {
            const int maxRetries = 5;

            Mock.Get(commandReceived)
            .Setup(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()))
            .Throws(exception);

            var builder = new ConfigurationBuilder();

            builder.AddInMemoryCollection(new Dictionary <string, string>
            {
                ["Nybus:CommandErrorFilters:0:type"]       = "retry",
                ["Nybus:CommandErrorFilters:0:maxRetries"] = maxRetries.Stringfy()
            });

            var configuration = builder.Build();

            var host = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand(commandReceived);

                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(c => c.ConnectionFactory = new ConnectionFactory());
                });

                nybus.UseConfiguration(configuration);
            });

            await host.StartAsync();

            await host.Bus.InvokeCommandAsync(testCommand);

            await Task.Delay(TimeSpan.FromMilliseconds(50));

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()), Times.Exactly(maxRetries));
        }
Esempio n. 10
0
        public void CreateCommandMessage_returns_message_from_Envelope([Frozen] ISerializer serializer, EnvelopeService sut, Envelope envelope, FirstTestCommand testCommand)
        {
            envelope.MessageType = MessageType.Command;

            Mock.Get(serializer).Setup(p => p.DeserializeObject(It.IsAny <string>(), typeof(FirstTestCommand))).Returns(testCommand);

            var commandMessage = sut.CreateCommandMessage(envelope, typeof(FirstTestCommand)) as CommandMessage <FirstTestCommand>;

            Assert.That(commandMessage, Is.Not.Null);
            Assert.That(commandMessage.Command, Is.SameAs(testCommand));
            Assert.That(commandMessage.Headers, Is.SameAs(envelope.Headers));
            Assert.That(commandMessage.MessageId, Is.EqualTo(envelope.MessageId));
            Assert.That(commandMessage.MessageType, Is.EqualTo(envelope.MessageType));
        }
Esempio n. 11
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()));
        }