Example #1
0
        public async void When_a_single_Command_is_published_to_the_bus_containing_multiple_CommandHandlers()
        {
            var handler1 = new FirstTestCommandHandler();
            var handler2 = new SecondTestCommandHandler();

            List <Action <TestCommand> > handlers = new List <Action <TestCommand> >
            {
                command => handler1.ExecuteAsync(command),
                command => handler2.ExecuteAsync(command)
            };

            Mock <ICommandDispatcher> commandDispatcherMock = new Mock <ICommandDispatcher>();

            commandDispatcherMock.Setup(e => e.DispatchAsync(It.IsAny <TestCommand>())).Callback((ICommand command) =>
            {
                handlers.ForEach(action =>
                {
                    action((TestCommand)command);
                });
            }).Returns(Task.CompletedTask);

            var testCommand = new TestCommand(Guid.NewGuid());

            ICommandDispatcher commandDispatcher = commandDispatcherMock.Object;

            await commandDispatcher.DispatchAsync(testCommand);

            handler1.Ids.First().Should().Be(testCommand.AggregateId);
            handler2.Ids.First().Should().Be(testCommand.AggregateId);
        }
Example #2
0
        public async Task Host_can_loopback_commands(SecondTestCommand testCommand, [Frozen] CommandReceivedAsync <SecondTestCommand> commandReceived, SecondTestCommandHandler handler)
        {
            var host = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.SubscribeToCommand(commandReceived);

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

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

            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> >()));
        }
        public async Task Host_can_loopback_commands(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 = TestUtils.CreateNybusHost(nybus =>
            {
                nybus.UseRabbitMqBusEngine(rabbitMq =>
                {
                    rabbitMq.Configure(c => c.ConnectionFactory = new ConnectionFactory());
                });

                nybus.UseConfiguration(configuration);

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

            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> >()));
        }