Ejemplo n.º 1
0
        public async Task Host_can_loopback_commands(SecondTestCommand testCommand, CommandReceivedAsync <SecondTestCommand> commandReceived, string headerKey, string headerValue)
        {
            var host = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand(commandReceived);

                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(c => c.ConnectionFactory = new ConnectionFactory());
                });
            },
                                                 services =>
            {
                services.AddSingleton(commandReceived);
                services.AddSingleton <ICommandHandler <SecondTestCommand>, SecondTestCommandHandler>();
            });

            await host.StartAsync();

            var headers = new Dictionary <string, string>
            {
                [headerKey] = headerValue
            };

            await host.Bus.InvokeCommandAsync(testCommand, headers);

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

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.Is <ICommandContext <SecondTestCommand> >(c => c.Message.Headers.ContainsKey(headerKey) && c.Message.Headers[headerKey] == headerValue)));
        }
Ejemplo n.º 2
0
        public async Task Host_can_loopback_commands(SecondTestCommand testCommand, CommandReceivedAsync <SecondTestCommand> commandReceived)
        {
            var host = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(configuration => configuration.ConnectionFactory = new ConnectionFactory());
                });

                nybus.SubscribeToCommand <SecondTestCommand>();
            },
                                                 services =>
            {
                services.AddSingleton(commandReceived);
                services.AddSingleton <ICommandHandler <SecondTestCommand>, SecondTestCommandHandler>();
            });

            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 <SecondTestCommand> >()));
        }
Ejemplo n.º 3
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> >()));
        }
        public async Task Host_can_loopback_commands(FirstTestCommand testCommand, CommandReceivedAsync <FirstTestCommand> commandReceived)
        {
            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 host = CreateNybusHost(nybus =>
            {
                nybus.UseConfiguration(configuration);

                nybus.SubscribeToCommand(commandReceived);

                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(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()), Times.Once);
        }
Ejemplo n.º 5
0
        public void SubscribeToCommand <TCommand>(CommandReceivedAsync <TCommand> commandReceived)
            where TCommand : class, ICommand
        {
            _engine.SubscribeToCommand <TCommand>();

            _errorHandlers.AddOrUpdate(typeof(TCommand), CreateCommandErrorDelegate <TCommand>(), (key, item) => item);

            _messagePipelines.Add(async message =>
            {
                if (message is CommandMessage <TCommand> commandMessage)
                {
                    var dispatcher = new NybusDispatcher(this, commandMessage);
                    var context    = new NybusCommandContext <TCommand>(commandMessage);
                    await commandReceived(dispatcher, context).ConfigureAwait(false);
                }
            });
        }
Ejemplo n.º 6
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));
        }
        public void SubscribeToCommand_registers_delegate_handler_type(TestNybusConfigurator nybus, IServiceCollection services, CommandReceivedAsync <FirstTestCommand> testHandler)
        {
            NybusConfiguratorExtensions.SubscribeToCommand(nybus, testHandler);

            nybus.ApplyServiceConfigurations(services);

            Mock.Get(services).Verify(p => p.Add(It.Is <ServiceDescriptor>(sd => sd.ServiceType == typeof(DelegateWrapperCommandHandler <FirstTestCommand>))));
        }
        public void SubscribeToCommand_registers_delegate_handler_for_command(TestNybusConfigurator nybus, ISubscriptionBuilder subscriptionBuilder, CommandReceivedAsync <FirstTestCommand> testHandler)
        {
            NybusConfiguratorExtensions.SubscribeToCommand(nybus, testHandler);

            nybus.ApplySubscriptions(subscriptionBuilder);

            Mock.Get(subscriptionBuilder).Verify(p => p.SubscribeToCommand <FirstTestCommand>(typeof(DelegateWrapperCommandHandler <FirstTestCommand>)));
        }
        public async Task Host_can_loopback_commands(FakeServer server, FirstTestCommand testCommand, CommandReceivedAsync <FirstTestCommand> commandReceived)
        {
            var host = CreateNybusHost(nybus =>
            {
                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
                });

                nybus.SubscribeToCommand(commandReceived);
            });

            await host.StartAsync();

            await host.Bus.InvokeCommandAsync(testCommand);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()));
        }
Ejemplo n.º 10
0
        public async Task Handler_is_executed_when_commandMessages_are_processed([Frozen] IBusEngine engine, NybusHost sut, CommandMessage <FirstTestCommand> testMessage, CommandReceivedAsync <FirstTestCommand> receivedMessage)
        {
            var subject = new Subject <Message>();

            Mock.Get(engine).Setup(p => p.StartAsync()).ReturnsAsync(subject);

            sut.SubscribeToCommand(receivedMessage);

            await sut.StartAsync();

            subject.OnNext(testMessage);

            await sut.StopAsync();

            Mock.Get(receivedMessage).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()), Times.Once);
        }
Ejemplo n.º 11
0
        public async Task Commands_are_correctly_converted(ThirdTestCommand testCommand, CommandReceivedAsync <AttributeTestCommand> commandReceived)
        {
            var host = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand(commandReceived);

                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(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.Is <ICommandContext <AttributeTestCommand> >(c => string.Equals(c.Command.Message, testCommand.Message))), Times.Once);
        }
Ejemplo n.º 12
0
        public void Handler_errors_are_not_caught(IDispatcher dispatcher, ICommandContext <FirstTestCommand> context, Exception error, CommandReceivedAsync <FirstTestCommand> handler)
        {
            Mock.Get(handler).Setup(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >())).Throws(error);

            var sut = new DelegateWrapperCommandHandler <FirstTestCommand>(handler);

            Assert.ThrowsAsync(error.GetType(), () => sut.HandleAsync(dispatcher, context));
        }
Ejemplo n.º 13
0
        public async Task Hosts_can_exchange_commands(FakeServer server, NoNamespaceCommand testCommand, CommandReceivedAsync <NoNamespaceCommand> commandReceived)
        {
            var sender = CreateNybusHost(nybus =>
            {
                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(configuration => configuration.ConnectionFactory = server.CreateConnectionFactory());
                });
            });

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

                nybus.SubscribeToCommand(commandReceived);
            });

            await sender.StartAsync();

            await receiver.StartAsync();

            await sender.Bus.InvokeCommandAsync(testCommand);

            await receiver.StopAsync();

            await sender.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <NoNamespaceCommand> >()));
        }
        public async Task Host_can_loopback_commands(FakeServer server, SecondTestCommand testCommand, [Frozen] CommandReceivedAsync <SecondTestCommand> commandReceived, SecondTestCommandHandler handler)
        {
            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 host = CreateNybusHost(nybus =>
            {
                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(c => c.ConnectionFactory = server.CreateConnectionFactory());
                });

                nybus.UseConfiguration(configuration);

                nybus.SubscribeToCommand <SecondTestCommand, SecondTestCommandHandler>();
            },
                                       services =>
            {
                services.AddSingleton(commandReceived);
                services.AddSingleton(handler);
            });

            await host.StartAsync();

            await host.Bus.InvokeCommandAsync(testCommand);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <SecondTestCommand> >()));
        }
Ejemplo n.º 15
0
        public async Task Outgoing_commands_are_marked_via_MessageAttribute(ServiceCollection services, AttributeTestCommand testCommand, CommandReceivedAsync <ThirdTestCommand> commandReceived)
        {
            services.AddLogging(l => l.AddDebug());

            services.AddNybus(nybus =>
            {
                nybus.UseInMemoryBusEngine();

                nybus.SubscribeToCommand(commandReceived);
            });

            var serviceProvider = services.BuildServiceProvider();

            var host = serviceProvider.GetRequiredService <IBusHost>();

            var bus = serviceProvider.GetRequiredService <IBus>();

            await host.StartAsync();

            await bus.InvokeCommandAsync(testCommand);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <ThirdTestCommand> >()), Times.Once);
        }
Ejemplo n.º 16
0
        public async Task Null_messages_delivered_from_engine_are_discarded([Frozen] IBusEngine engine, NybusHost sut, CommandMessage <FirstTestCommand> testMessage, CommandReceivedAsync <FirstTestCommand> receivedMessage)
        {
            var subject = new Subject <Message>();

            Mock.Get(engine).Setup(p => p.StartAsync()).ReturnsAsync(subject);

            sut.SubscribeToCommand(receivedMessage);

            await sut.StartAsync();

            subject.OnNext(null);

            await sut.StopAsync();

            Mock.Get(receivedMessage).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <FirstTestCommand> >()), Times.Never);
        }
Ejemplo n.º 17
0
        public async Task Host_can_loopback_commands(ServiceCollection services, SecondTestCommand testCommand, CommandReceivedAsync <SecondTestCommand> commandReceived)
        {
            var settings = new Dictionary <string, string>
            {
                ["Nybus:ErrorPolicy:ProviderName"] = "retry",
                ["Nybus:ErrorPolicy:MaxRetries"]   = "5",
            };

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

            services.AddLogging(l => l.AddDebug());

            services.AddSingleton(commandReceived);
            services.AddSingleton <ICommandHandler <SecondTestCommand>, SecondTestCommandHandler>();

            services.AddNybus(nybus =>
            {
                nybus.UseInMemoryBusEngine();

                nybus.UseConfiguration(configuration);

                nybus.SubscribeToCommand <SecondTestCommand>();
            });

            var serviceProvider = services.BuildServiceProvider();

            var host = serviceProvider.GetRequiredService <IBusHost>();

            var bus = serviceProvider.GetRequiredService <IBus>();

            await host.StartAsync();

            await bus.InvokeCommandAsync(testCommand);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <SecondTestCommand> >()));
        }
Ejemplo n.º 18
0
        public async Task Handler_is_executed(IDispatcher dispatcher, ICommandContext <FirstTestCommand> context, CommandReceivedAsync <FirstTestCommand> handler)
        {
            var sut = new DelegateWrapperCommandHandler <FirstTestCommand>(handler);

            await sut.HandleAsync(dispatcher, context);

            Mock.Get(handler).Verify(p => p(dispatcher, context), Times.Once);
        }
Ejemplo n.º 19
0
        public async Task Host_can_loopback_commands(ServiceCollection services, NoNamespaceCommand testCommand, CommandReceivedAsync <NoNamespaceCommand> commandReceived)
        {
            services.AddLogging(l => l.AddDebug());

            services.AddNybus(nybus =>
            {
                nybus.UseInMemoryBusEngine();

                nybus.SubscribeToCommand(commandReceived);
            });

            var serviceProvider = services.BuildServiceProvider();

            var host = serviceProvider.GetRequiredService <IBusHost>();

            var bus = serviceProvider.GetRequiredService <IBus>();

            await host.StartAsync();

            await bus.InvokeCommandAsync(testCommand);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <NoNamespaceCommand> >()), Times.Once);
        }
Ejemplo n.º 20
0
        public async Task Host_can_loopback_commands(FakeServer server, SecondTestCommand testCommand, [Frozen] CommandReceivedAsync <SecondTestCommand> commandReceived, SecondTestCommandHandler handler)
        {
            var host = CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand(commandReceived);

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

                nybus.SubscribeToCommand <SecondTestCommand, SecondTestCommandHandler>();
            },
                                       services =>
            {
                services.AddSingleton(commandReceived);
                services.AddSingleton(handler);
            });

            await host.StartAsync();

            await host.Bus.InvokeCommandAsync(testCommand);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <SecondTestCommand> >()));
        }
Ejemplo n.º 21
0
 public DelegateWrapperCommandHandler(CommandReceivedAsync <TCommand> handler)
 {
     _handler = handler ?? throw new ArgumentNullException(nameof(handler));
 }
Ejemplo n.º 22
0
        public async Task Outgoing_commands_are_marked_via_MessageAttribute(FakeServer server, AttributeTestCommand testCommand, CommandReceivedAsync <ThirdTestCommand> commandReceived)
        {
            var host = CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand(commandReceived);

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

            await host.StartAsync();

            await host.Bus.InvokeCommandAsync(testCommand);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.IsAny <ICommandContext <ThirdTestCommand> >()), Times.Once);
        }
Ejemplo n.º 23
0
 public SecondTestCommandHandler(CommandReceivedAsync <SecondTestCommand> commandReceived)
 {
     _commandReceived = commandReceived ?? throw new ArgumentNullException(nameof(commandReceived));
 }
Ejemplo n.º 24
0
        public static void SubscribeToCommand <TCommand>(this INybusConfigurator configurator, CommandReceivedAsync <TCommand> commandReceived)
            where TCommand : class, ICommand
        {
            var handler = new DelegateWrapperCommandHandler <TCommand>(commandReceived);

            SubscribeToCommand <TCommand, DelegateWrapperCommandHandler <TCommand> >(configurator, handler);
        }
Ejemplo n.º 25
0
        public async Task Host_can_loopback_commands(ServiceCollection services, SecondTestCommand testCommand, CommandReceivedAsync <SecondTestCommand> commandReceived, string headerKey, string headerValue)
        {
            services.AddLogging(l => l.AddDebug());

            services.AddSingleton(commandReceived);
            services.AddSingleton <ICommandHandler <SecondTestCommand>, SecondTestCommandHandler>();

            services.AddNybus(nybus =>
            {
                nybus.UseInMemoryBusEngine();

                nybus.SubscribeToCommand <SecondTestCommand>();
            });

            var serviceProvider = services.BuildServiceProvider();

            var host = serviceProvider.GetRequiredService <IBusHost>();

            var bus = serviceProvider.GetRequiredService <IBus>();

            await host.StartAsync();

            var headers = new Dictionary <string, string>
            {
                [headerKey] = headerValue
            };

            await bus.InvokeCommandAsync(testCommand, headers);

            await host.StopAsync();

            Mock.Get(commandReceived).Verify(p => p(It.IsAny <IDispatcher>(), It.Is <ICommandContext <SecondTestCommand> >(c => c.Message.Headers.ContainsKey(headerKey) && c.Message.Headers[headerKey] == headerValue)));
        }