private async Task <CommandBusResult> Execute(ICommandPipeline pipeline, ICommand cmd)
        {
            InMemoryCommandBus sut    = GetSut(pipeline);
            CommandBusResult   result = await sut.EnqueueAsync(cmd);

            return(result);
        }
        public WhenPipelineThrows()
        {
            var fake = A.Fake <ICommandPipeline>();

            A.CallTo(() => fake.ExecuteAsync(A <ICommand> .Ignored)).Throws <InvalidOperationException>();

            sut = GetSut(fake);
        }
Exemple #3
0
        public async Task InMemoryCommandBus_Configuration_MultipleHandlers_ConfigurationOk()
        {
            var c = new InMemoryCommandBusConfigurationBuilder()
                    .AllowMultipleHandlersFor <TestMultipleHandlerFromConfig>();
            var bus = new InMemoryCommandBus(c.Build());

            (await bus.DispatchAsync(new TestMultipleHandlerFromConfig()).ConfigureAwait(false)).IsSuccess.Should().BeTrue();
        }
Exemple #4
0
        public async Task InMemoryCommandBus_DispatchAsync_NoHandlerCanBeCreated()
        {
            var hInvoked = false;
            var c        = new InMemoryCommandBusConfigurationBuilder().AddHandlerWhenHandlerIsNotFound((cmd, ctx) => hInvoked = true).Build();
            var bus      = new InMemoryCommandBus(c);

            (await bus.DispatchAsync(new TestNoCreatableHandler()).ConfigureAwait(false)).IsSuccess.Should().BeFalse();
            hInvoked.Should().BeTrue();
        }
        public async Task DispatchRangeCommands(int nbCommands)
        {
            var bus = new InMemoryCommandBus();

            for (int i = 0; i < nbCommands; i++)
            {
                await bus.DispatchAsync(new TestCommand(i, SimulateWork, JobDuration));
            }
        }
Exemple #6
0
        public async Task Dispatch_Command_SpecificResult_Should_Not_Loose_Value()
        {
            var bus = new InMemoryCommandBus();

            var cmd    = new TestResultData();
            var result = await bus.DispatchAsync(cmd);

            result.Should().BeOfType <Result <string> >();
        }
 public void Should_be_able_Send_the_Command_to_the_Bus()
 {
     AddProductCommand command = new AddProductCommand { Name = "Reebok Shoe", Categories = "Shoes", Id = 1 };
     InMemoryCommandBus Bus = new InMemoryCommandBus();
     InventoryController controller = new InventoryController(Bus);
     ConfigureController(controller);
     HttpResponseMessage Message = controller.Post(command);
     Message.StatusCode.Should().Be(HttpStatusCode.Created);
     Message.Headers.Location.ToString().Should().Be("http://localhost/api/Product/1");
     Bus.Command.Should().Be(command);
 }
Exemple #8
0
        public static IServiceCollection AddConventionBasedInMemoryCommandBus(
            this IServiceCollection services, EventSourceConfiguration config)
        {
            services.AddSingleton <EventSourceConfiguration>(config);
            services.AddSingleton <ICommandBus>(x => InMemoryCommandBus.CreateConventionCommandBus(
                                                    x.GetRequiredService <IDomainObjectRepository>(),
                                                    x.GetRequiredService <ILoggerFactory>(),
                                                    x.GetRequiredService <EventSourceConfiguration>()));

            return(services);
        }
        public static EventSourceConfiguration SetConventionBasedInMemoryCommandBus(this EventSourceConfiguration config)
        {
            Precondition.For(() => config).NotNull();
            Precondition.For(() => config.DomainObjectRepository).NotNull();
            Precondition.For(() => config.DomainObjectAssemblies).NotNull();

            config.CommandBus = InMemoryCommandBus.CreateConventionCommandBus(config.DomainObjectRepository,
                                                                              config.DomainObjectAssemblies);

            return(config);
        }
Exemple #10
0
        private Task <CommandBusResult> Execute(ICommandPipeline pipeline, ICommand command)
        {
            InMemoryCommandBus bus = GetSut(pipeline);

            bus.WithCondition(i =>
            {
                var temp = i as SampleCommand;
                return(temp.Value == 42);
            });

            return(bus.EnqueueAsync(command));
        }
Exemple #11
0
        public async Task Dispatch_Command_CriticalHandlerThrow_Should_NotCallNextHandlers()
        {
            HandlersData = "";
            var cmd = new CriticalCommand();

            var c   = new InMemoryCommandBusConfigurationBuilder().AllowMultipleHandlersFor <CriticalCommand>(true).Build();
            var bus = new InMemoryCommandBus(c);

            await bus.DispatchAsync(cmd);

            HandlersData.Should().Be("A");
        }
Exemple #12
0
        public async Task Dispatch_Command_OneFail_Should_Returns_Failed_Result()
        {
            var c = new InMemoryCommandBusConfigurationBuilder()
                    .AllowMultipleHandlersFor <TestMultilpleCommand>()
                    .Build();
            var bus = new InMemoryCommandBus(c);

            var cmd    = new TestMultilpleCommand();
            var result = await bus.DispatchAsync(cmd);

            result.IsSuccess.Should().BeFalse();
        }
Exemple #13
0
        public async Task InMemoryCommandBus_DispatchAsync_Reflexion()
        {
            CleanRegistrationInDispatcher();
            var bus = new InMemoryCommandBus();

            (await bus.DispatchAsync(new TestCommand {
                Data = "test_ioc"
            }).ConfigureAwait(false)).IsSuccess.Should().BeTrue();

            TestCommandHandler.HandlerData.Should().Be("test_ioc");
            TestCommandHandler.Origin.Should().Be("reflexion");
        }
Exemple #14
0
        public async Task InMemoryCommandBus_DispatchAsync_FromCoreDispatcher()
        {
            CleanRegistrationInDispatcher();
            CoreDispatcher.AddHandlerToDispatcher(new TestCommandHandler("coreDispatcher"));
            var bus = new InMemoryCommandBus();

            (await bus.DispatchAsync(new TestCommand {
                Data = "test_dispatcher"
            }).ConfigureAwait(false)).IsSuccess.Should().BeTrue();

            TestCommandHandler.HandlerData.Should().Be("test_dispatcher");
            TestCommandHandler.Origin.Should().Be("spec_ctor");
        }
Exemple #15
0
 internal static void ConfigureInMemoryCommandBus(Bootstrapper bootstrapper, InMemoryCommandBusConfiguration?configuration, string[] excludedCommandsDLLs, BootstrappingContext ctx)
 {
     InMemoryCommandBus.InitHandlersCollection(excludedCommandsDLLs);
     if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC))
     {
         bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(InMemoryCommandBus), typeof(ICommandBus), typeof(InMemoryCommandBus)));
         if (configuration != null)
         {
             bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(configuration, typeof(InMemoryCommandBusConfiguration)));
         }
     }
     bootstrapper.AddNotifications(PerformCommandChecksAccordingToBootstrapperParameters(ctx, configuration));
 }
Exemple #16
0
        public async Task InMemoryCommandBus_Configuration_MultipleHandlers_ConfigurationOk_ShouldWait()
        {
            s_Order.Clear();
            var c = new InMemoryCommandBusConfigurationBuilder()
                    .AllowMultipleHandlersFor <TestMultipleHandlerFromConfigParallel>(true);
            var bus = new InMemoryCommandBus(c.Build());

            (await bus.DispatchAsync(new TestMultipleHandlerFromConfigParallel()).ConfigureAwait(false)).IsSuccess.Should().BeTrue();

            s_Order.Should().HaveCount(3);
            s_Order.Should().Contain("One");
            s_Order.Should().Contain("Two");
            s_Order.Should().Contain("Three");
        }
Exemple #17
0
        public async Task When_a_command_is_undelivered_it_can_be_retrieved_from_the_bus_before_it_is_due()
        {
            using (var clock = VirtualClock.Start())
            {
                var bus = new InMemoryCommandBus <string>(clock);

                await bus.Schedule("undelivered",
                                   dueTime : clock.Now().AddDays(2));

                bus.Undelivered().Should().HaveCount(1);

                bus.Undelivered().Single().Command.Should().Be("undelivered");
            }
        }
Exemple #18
0
        public async Task Dispatch_Command_With_Result_Should_Go_To_Correct_Path()
        {
            var bus = new InMemoryCommandBus();

            var cmd      = new TestResultOkCommand();
            var okResult = await bus.DispatchAsync(cmd);

            okResult.IsSuccess.Should().BeTrue();

            var cmd2       = new TestResultFailCommand();
            var failResult = await bus.DispatchAsync(cmd2);

            failResult.IsSuccess.Should().BeFalse();
        }
Exemple #19
0
        /// <summary>
        /// Configure the system to use InMemory Command bus for dipsatching commands, with the provided configuration.
        /// </summary>
        /// <param name="bootstrapper">Bootstrapper instance.</param>
        /// <param name="configurationBuilderAction">Action to apply on builder.</param>
        /// <param name="excludedCommandsDLLs">DLLs name to exclude from auto-configuration into IoC
        /// (IAutoRegisterType will be ineffective).</param>
        /// <returns>Bootstrapper Instance.</returns>
        public static Bootstrapper UseInMemoryCommandBus(this Bootstrapper bootstrapper, Action <InMemoryCommandBusConfigurationBuilder> configurationBuilderAction,
                                                         params string[] excludedCommandsDLLs)
        {
            if (configurationBuilderAction == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilderAction));
            }

            InMemoryCommandBus.InitHandlersCollection(excludedCommandsDLLs);
            var builder = new InMemoryCommandBusConfigurationBuilder();

            configurationBuilderAction(builder);

            return(UseInMemoryCommandBus(bootstrapper, builder.Build()));
        }
Exemple #20
0
        public async Task InMemoryCommandBus_DispatchAsync_HandlerFromIoC()
        {
            var factory = new TestScopeFactory();

            factory.Instances.Add(typeof(ICommandHandler <TestCommand>), new TestCommandHandler("tt"));

            CleanRegistrationInDispatcher();
            var bus = new InMemoryCommandBus(null, factory);

            (await bus.DispatchAsync(new TestCommand {
                Data = "test_ioc"
            }).ConfigureAwait(false)).IsSuccess.Should().BeTrue();

            TestCommandHandler.HandlerData.Should().Be("test_ioc");
            TestCommandHandler.Origin.Should().Be("spec_ctor");
        }
Exemple #21
0
        public async Task When_a_command_is_undelivered_it_can_be_retrieved_from_the_bus_after_it_is_due()
        {
            using (var clock = VirtualClock.Start())
            {
                var bus = new InMemoryCommandBus <string>(clock);

                await bus.Schedule("undelivered",
                                   1.Seconds());

                await clock.AdvanceBy(3.Days());

                bus.Undelivered().Should().HaveCount(1);

                bus.Undelivered().Single().Command.Should().Be("undelivered");
            }
        }
Exemple #22
0
        public async Task InMemoryCommandBus_Configuration_DispatchIfClause()
        {
            TestIfCommandHandler.ResetData();
            var cfgBuilder =
                new InMemoryCommandBusConfigurationBuilder()
                .DispatchOnlyIf <TestIfCommand>(e => e.Data > 1);

            var b = new InMemoryCommandBus(cfgBuilder.Build());

            TestIfCommandHandler.Data.Should().Be(0);

            await b.DispatchAsync(new TestIfCommand { Data = 1 }).ConfigureAwait(false);

            TestIfCommandHandler.Data.Should().Be(0);

            await b.DispatchAsync(new TestIfCommand { Data = 10 }).ConfigureAwait(false);

            TestIfCommandHandler.Data.Should().Be(10);
        }
Exemple #23
0
        public void Init(IUnityContainer container)
        {
            container.RegisterType<ITextSerializer, JsonSerializer>();
            var eventBus = new InMemoryEventBus();
            container.RegisterInstance<IEventBus>(eventBus);
            var commandBus = new InMemoryCommandBus();
            container.RegisterInstance<ICommandBus>(commandBus);

            container.RegisterType<IEventStore, SqlEventStore>();

            foreach (var handler in container.ResolveAll<ICommandHandler>())
            {
                commandBus.Register(handler);
            }

            foreach (var handler in container.ResolveAll<IEventHandler>())
            {
                eventBus.Register(handler);
            }
        }
Exemple #24
0
        public async Task A_subscribed_receiver_can_select_a_handler_based_on_message_type()
        {
            var bus = new InMemoryCommandBus <ICommand <CommandTarget> >(Clock);

            var received = new List <ICommand <CommandTarget> >();

            var handler1 = CommandHandler.Create <CreateCommandTarget>(delivery => received.Add(delivery.Command));
            var handler2 = CommandHandler.Create <UpdateCommandTarget>(delivery => received.Add(delivery.Command));

            bus.Subscribe(handler1);
            bus.Subscribe(handler2);

            await bus.Schedule(new CreateCommandTarget(Guid.NewGuid().ToString()));

            await Clock.AdvanceBy(1.Seconds());

            received.Should()
            .OnlyContain(c => c is CreateCommandTarget)
            .And
            .HaveCount(1);
        }
Exemple #25
0
        /// <summary>
        /// Configure the bootstrapper to use InMemory buses for dispatching commands.
        /// </summary>
        /// <param name="bootstrapper">Instance of boostrapper.</param>
        /// <param name="configuration">Configuration to use for in memory command bus.</param>
        /// <param name="excludedCommandsDLLs">DLLs name to exclude from auto-configuration into IoC
        /// (IAutoRegisterType will be ineffective).</param>
        public static Bootstrapper UseInMemoryCommandBus(this Bootstrapper bootstrapper, InMemoryCommandBusConfiguration configuration = null,
                                                         params string[] excludedCommandsDLLs)
        {
            var service = InMemoryBusesBootstrappService.Instance;

            service.BootstrappAction += (ctx) =>
            {
                InMemoryCommandBus.InitHandlersCollection(excludedCommandsDLLs);
                if (ctx.IsServiceRegistered(BootstrapperServiceType.IoC))
                {
                    bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(InMemoryCommandBus), typeof(ICommandBus), typeof(InMemoryCommandBus)));
                    if (configuration != null)
                    {
                        bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(configuration, typeof(InMemoryCommandBusConfiguration)));
                    }
                }
                bootstrapper.AddNotifications(PerformCommandChecksAccordingToBootstrapperParameters(ctx, configuration));
            };
            if (!bootstrapper.RegisteredServices.Any(s => s == service))
            {
                bootstrapper.AddService(service);
            }
            return(bootstrapper);
        }
 public async Task DispatchACommand()
 {
     var bus = new InMemoryCommandBus();
     await bus.DispatchAsync(new TestCommand(0, SimulateWork, JobDuration));
 }
        public InMemoryCommandBus GetSut(ICommandPipeline pipeline)
        {
            var sut = new InMemoryCommandBus(pipeline, new NoopLoggerFactory());

            return(sut);
        }
Exemple #28
0
 public InMemoryCommandBusTests()
 {
     TestCommandHandler.ResetTestData();
     InMemoryCommandBus.InitHandlersCollection(new string[0]);
 }
Exemple #29
0
        public InMemoryCommandBus GetSut(ICommandPipeline pipeline)
        {
            var sut = new InMemoryCommandBus(pipeline);

            return(sut);
        }