Exemple #1
0
        public void TestAtPublishRethrowsExceptions()
        {
            var commandBus = new CommandBus(CreateContainer());
            var command1   = new TestCommand
            {
                Modified         = false,
                ExceptionToThrow = new NullReferenceException()
            };
            var command2 = new TestCommand
            {
                Modified         = false,
                ExceptionToThrow = new NullReferenceException()
            };

            Assert.Throws <NullReferenceException>(() => commandBus.Publish(command1));

            command1.Modified = false;
            command2.Modified = false;
            var commands = new List <TestCommand>
            {
                command1,
                command2
            };

            Assert.Throws <NullReferenceException>(() => commandBus.Publish <TestCommand>(commands));

            command1.Modified = false;
            command2.Modified = false;
            Assert.Throws <NullReferenceException>(() => commandBus.Publish <TestCommand, Guid>(command1));
        }
Exemple #2
0
        public void TestAtPublishKasterCommandBusExceptionHvisCommandHandlerIkkeErRegistreret()
        {
            var commandBus = new CommandBus(CreateContainer());

            var command = new TestCommandWithoutCommandHandler();

            Assert.Throws <CommandBusException>(() => commandBus.Publish(command));
            Assert.Throws <CommandBusException>(() => commandBus.Publish <TestCommandWithoutCommandHandler, Guid>(command));
        }
Exemple #3
0
        public void TestAtPublishKasterArgumentNullExceptionHvisCommandErNull()
        {
            var commandBus = new CommandBus(CreateContainer());

            TestCommand command = null;

            Assert.Throws <ArgumentNullException>(() => commandBus.Publish(command));

            IList <TestCommand> commands = null;

            Assert.Throws <ArgumentNullException>(() => commandBus.Publish(commands));

            Assert.Throws <ArgumentNullException>(() => commandBus.Publish <TestCommand, Guid>(null));
        }
        public async Task CommandBus_Publishes_To_Multiple_Subscribers()
        {
            var bus = new CommandBus();

            var source1 = new TaskCompletionSource <bool>();
            var source2 = new TaskCompletionSource <bool>();

            bus.Subscribe <SaveCommand>((cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));

                source1.SetResult(true);
            });

            bus.Subscribe <SaveCommand>((cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));

                source2.SetResult(true);
            });

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await Task.WhenAll(source1.Task, source2.Task).ContinueWith((obj) => {
                bus.RemoveAllSubscriptions();
            });
        }
        public async Task CommandBus_Subscribes_With_On()
        {
            var bus    = new CommandBus();
            var source = new TaskCompletionSource <bool>();

            dynamic input = new InteropMock()
            {
                type       = "SaveCommand",
                subscriber = (cmd) => {
                    var saveCommand = (SaveCommand)cmd;

                    Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));
                    bus.RemoveAllSubscriptions();

                    source.SetResult(true);
                    return(null);
                }
            };

            bus.On(input);

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await source.Task;
        }
        public async Task CommandBus_Publishes_To_Multiple_Subscribers() {
            var bus = new CommandBus();

            var source1 = new TaskCompletionSource<bool>();
            var source2 = new TaskCompletionSource<bool>();

            bus.Subscribe<SaveCommand>((cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));

                source1.SetResult(true);
            });

            bus.Subscribe<SaveCommand>((cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));

                source2.SetResult(true);
            });

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await Task.WhenAll(source1.Task, source2.Task).ContinueWith((obj) => {
                bus.RemoveAllSubscriptions();
            });
        }
        public void TransferMoney()
        {
            // Given
            RegisterHandlers(() => new AccountCommandHandler(Repository));
            RegisterHandlers(() => new AccountValueTranferringEventHandler(SagaRepository));
            CommandBus.Publish(new CreateAccount {
                Id = 1.Guid(), DisplayName = "Company", Value = 1000m
            });
            CommandBus.Publish(new CreateAccount {
                Id = 2.Guid(), DisplayName = "My", Value = 100m
            });
            ClearEvents();

            // When
            CommandBus.Publish(new TransferAccountValue {
                From = 1.Guid(), To = 2.Guid(), Value = 40m, SagaId = 123.Guid()
            });

            // Then
            AssertEvent(new AccountValueChanged {
                Id = 1.Guid(), ValueDifference = -40m, SagaId = 123.Guid()
            }, evt => evt.Id == 1.Guid());
            AssertEvent(new AccountValueChanged {
                Id = 2.Guid(), ValueDifference = +40m, SagaId = 123.Guid()
            }, evt => evt.Id == 2.Guid());
        }
        public void NotTransferMoneyIfLackOfMyMoney()
        {
            // Given
            RegisterHandlers(() => new AccountCommandHandler(Repository));
            RegisterHandlers(() => new AccountValueTranferringEventHandler(SagaRepository));
            CommandBus.Publish(new CreateAccount {
                Id = 1.Guid(), DisplayName = "Company", Value = 1000m
            });
            CommandBus.Publish(new CreateAccount {
                Id = 2.Guid(), DisplayName = "My", Value = 100m
            });
            ClearEvents();

            // When
            CommandBus.Publish(new TransferAccountValue {
                From = 1.Guid(), To = 2.Guid(), Value = -200m, SagaId = 123.Guid()
            });

            // Then
            AssertEvent(new AccountValueChanged {
                Id = 1.Guid(), ValueDifference = +200m, SagaId = 123.Guid()
            }, evt => evt.Id == 1.Guid() && evt.Reason == null);
            AssertEvent(new AccountValueChanged {
                Id = 1.Guid(), ValueDifference = -200m, SagaId = 123.Guid(), Reason = "My account does not have enough value to transfer"
            }, evt => evt.Id == 1.Guid() && evt.Reason != null);
        }
        public void FunctionFor0HandleresDoesNotDoAnythig()
        {
            var commanBus = new CommandBus();
            commanBus.RegisterHandlers<FuncCommand>("nonExistentNamespaceWillHave 0 handlers");

            commanBus.Publish(new FuncCommand());
        }
Exemple #10
0
        private void Run()
        {
            using (var store = Wireup.Init().UsingInMemoryPersistence().Build())
            {
                var eventBus = Substitute.For <IEventBus>();

                if (!command.IsFirstCommand)
                {
                    using (var stream = store.CreateStream(command.CommandId))
                    {
                        initialEvents.Select(x => new EventMessage {
                            Body = x
                        }).ToList().ForEach(stream.Add);
                        stream.CommitChanges(Guid.NewGuid());
                    }
                }

                var bus = new CommandBus(new EventStoreCommandHandler(store, eventBus));
                bus.Subscribe(PriceAggregate.Handle);
                bus.Publish(command);

                using (var stream = store.OpenStream(command.CommandId))
                {
                    Check.That(stream.CommittedEvents.Select(x => x.Body).Cast <Event>()).Contains(resultingEvents);
                }
            }
        }
        public void TestMakeAppointment()
        {
            //Setup
//            var connStr = @"Server=192.168.88.79\SQLEXPRESS;Database=appointments;User Id=chinook;Password=pr0t3ct3d";
            var connStr = @"Data Source=.\SQLEXPRESS;Database=appointments;User Id=chinook;Password=pr0t3ct3d";

            IMessageSender  sender     = new SqlMessageSender(connStr, "dbo.messages");
            ITextSerializer serializer = new JsonTextSerializer();
//			IEventStore eventStore = new SqlEventStore(connStr, "dbo.events");
//
//			IEventBus eventBus = new EventBus(sender, serializer);
//			IMetadataProvider metaProvider = new StandardMetadataProvider();
            IReadModelStorage <AppointmentReadModel> readModelStorage = new InMemeoryStorage <AppointmentReadModel>();
//
//			IEventSourcedRepository<AppointmentAggregate> repo = new
//				EventSourcedRepository<AppointmentAggregate>(eventStore, eventBus, serializer, metaProvider);
//			ICommandDispatcher cmdDispatcher = new CommandDispatcher();
//			cmdDispatcher.Register(new AppointmentCommandHandler(repo));
//
//			IEventDispatcher evtDispatcher = new EventDispatcher();
//			evtDispatcher.Register(new AppointmentEventHandler(readModelStorage));
//
//			IMessageReceiver cmdReceiver = new SqlMessageReceiver(connStr, "dbo.commands");
//
//			IMessageReceiver evtReceiver = new SqlMessageReceiver(connStr, "dbo.events");
//
//			CommandProcessor commandProcessor = new CommandProcessor(cmdReceiver, serializer, cmdDispatcher);
//			EventPorcessor eventProcessor = new EventPorcessor(evtReceiver, serializer, evtDispatcher);
//
//
//			commandProcessor.Start();
//
//			eventProcessor.Start();

            ICommandBus commandBus = new CommandBus(sender, serializer);

            //Test
            var command = new MakeAppointment();

            command.Appointment           = new Appointment();
            command.Appointment.Body      = "Dental appointment";
            command.Appointment.Subject   = "Dental";
            command.Appointment.Start     = DateTimeOffset.MinValue;
            command.Appointment.End       = DateTimeOffset.MaxValue;
            command.Appointment.Organizer = "Jeff Jin";
            var cmdTask = commandBus.Publish(new [] { command });

            cmdTask.Wait();

//			var task = readModelStorage.GetAll(0, 10);
//			task.Wait();
//
//			Thread.Sleep(3000);
//
//			//Verify
//			Assert.AreEqual(1, task.Result.Count());
        }
Exemple #12
0
        public void TestAtPublishKalderCommandHandlers()
        {
            var commandBus = new CommandBus(CreateContainer());
            var command1   = new TestCommand
            {
                Modified         = false,
                ExceptionToThrow = null
            };
            var command2 = new TestCommand
            {
                Modified         = false,
                ExceptionToThrow = null
            };

            commandBus.Publish(command1);
            Assert.That(command1, Is.Not.Null);
            Assert.That(command1.Modified, Is.True);

            command1.Modified = false;
            command2.Modified = false;
            var commands = new List <TestCommand>
            {
                command1,
                command2
            };

            commandBus.Publish <TestCommand>(commands);
            Assert.That(command1, Is.Not.Null);
            Assert.That(command1.Modified, Is.True);
            Assert.That(command2, Is.Not.Null);
            Assert.That(command2.Modified, Is.True);

            command1.Modified = false;
            command2.Modified = false;
            var id = commandBus.Publish <TestCommand, Guid>(command1);

            Assert.That(id, Is.Not.Null);
            Assert.That(id.GetType(), Is.EqualTo(typeof(Guid)));
            Assert.That(id, Is.Not.EqualTo(Guid.Empty));
            Assert.That(command1, Is.Not.Null);
            Assert.That(command1.Modified, Is.True);
        }
        public void ActionCallsVoidHandler()
        {
            var commanBus = new CommandBus();
            commanBus.RegisterHandlers<FuncCommand>("Commandos.Specs.Samples");

            var command = new ActionCommand();

            commanBus.Publish(command);

            Assert.IsTrue(command.Called);
        }
        public void FunctionCallsAHandlerAndReturns()
        {
            var commanBus = new CommandBus();
            commanBus.RegisterHandlers<FuncCommand>("Commandos.Specs.Samples");

            var command = new FuncCommand();

            var result = commanBus.Publish<FuncCommand>((object) command);

            Assert.That(command, Is.SameAs(result));
        }
Exemple #15
0
        public void CorrectBookByNotUniqueName()
        {
            // Given
            RegisterHandlers(() => new BookCommandHandler(Repository, new BookUniquenessCheckerMock().MockIsTitleExist(true)));
            CommandBus.Publish(new RegisterBook {
                Id = 1.Guid(), ISBN = "2", Title = "3"
            });

            // When
            CommandBus.Publish(new CorrectBook {
                Id = 1.Guid(), Title = "33"
            });
        }
        public void publish_publishes_command_as_message()
        {
            var  bus   = new CommandBus("temp");
            long gotIt = 0;
            var  hndl  =
                new AdHocHandler <TestCommands.TestCommand>(
                    cmd => Interlocked.Exchange(ref gotIt, 1));

            bus.Subscribe(hndl);

            bus.Publish(new TestCommands.TestCommand(Guid.NewGuid(), null));

            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotIt) == 1);
        }
Exemple #17
0
        public void RegisterBook()
        {
            // Given
            RegisterHandlers(() => new BookCommandHandler(Repository, new BookUniquenessCheckerMock()));

            // When
            CommandBus.Publish(new RegisterBook {
                Id = 1.Guid(), ISBN = "2", Title = "3"
            });

            // Then
            AssertEvent(new BookRegistered {
                Id = 1.Guid(), ISBN = "2", Title = "3"
            });
        }
Exemple #18
0
        public void TestAtPublishKalderExceptionHandlers()
        {
            var commandBus = new CommandBus(CreateContainer());
            var command1   = new TestCommand
            {
                Modified         = false,
                ExceptionToThrow = new NotSupportedException()
            };
            var command2 = new TestCommand
            {
                Modified         = false,
                ExceptionToThrow = new NotSupportedException()
            };

            commandBus.Publish(command1);
            Assert.That(command1, Is.Not.Null);
            Assert.That(command1.Modified, Is.False);

            command1.Modified = false;
            command2.Modified = false;
            var commands = new List <TestCommand>
            {
                command1,
                command2
            };

            commandBus.Publish <TestCommand>(commands);
            Assert.That(command1, Is.Not.Null);
            Assert.That(command1.Modified, Is.False);
            Assert.That(command2, Is.Not.Null);
            Assert.That(command2.Modified, Is.False);

            command1.Modified = false;
            command2.Modified = false;
            Assert.Throws <CommandBusException>(() => commandBus.Publish <TestCommand, Guid>(command1));
        }
Exemple #19
0
        public async Task Delete(UserAggregate user, ProjectId projectId)
        {
            var spec = new IsCanDeleteProjectWithIdSpecification(user);

            if (spec.IsSatisfiedBy(projectId) == false)
            {
                throw new InvalidOperationException(String.Join("\n", spec.WhyIsNotSatisfiedBy(projectId)));
            }

            var result = await CommandBus.Publish(new DeleteCommand(projectId));

            if (result.IsSuccess == false)
            {
                throw new InvalidOperationException(result.ToString());
            }

            await user.DeleteProject(projectId);
        }
        public async Task CommandBus_Publishes_To_Single_Subscriber() {
            var bus = new CommandBus();
            var source = new TaskCompletionSource<bool>();

            bus.Subscribe<SaveCommand>((cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));
                bus.RemoveAllSubscriptions();

                source.SetResult(true);
            });

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await source.Task;
        }
        public async Task CommandBus_Publishes_To_Single_Subscriber()
        {
            var bus    = new CommandBus();
            var source = new TaskCompletionSource <bool>();

            bus.Subscribe <SaveCommand>((cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));
                bus.RemoveAllSubscriptions();

                source.SetResult(true);
            });

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await source.Task;
        }
        public async Task CommandBus_Subscribes_With_Command_String() {
            var bus = new CommandBus();
            var source = new TaskCompletionSource<bool>();

            bus.Subscribe("SaveCommand", (cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));
                bus.RemoveAllSubscriptions();

                source.SetResult(true);

                return null;
            });

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await source.Task;
        }
        public void NotTransferMoneyIfLackOfCompanyMoney()
        {
            // Given
            RegisterHandlers(() => new AccountCommandHandler(Repository));
            RegisterHandlers(() => new AccountValueTranferringEventHandler(SagaRepository));
            CommandBus.Publish(new CreateAccount {
                Id = 1.Guid(), DisplayName = "Company", Value = 1000m
            });
            CommandBus.Publish(new CreateAccount {
                Id = 2.Guid(), DisplayName = "My", Value = 100m
            });
            ClearEvents();

            // When
            CommandBus.Publish(new TransferAccountValue {
                From = 1.Guid(), To = 2.Guid(), Value = 2000m, SagaId = 123.Guid()
            });

            // Then
            AssertEventIsNotRaised <AccountValueChanged>();
        }
        public async Task CommandBus_Subscribes_With_Command_String()
        {
            var bus    = new CommandBus();
            var source = new TaskCompletionSource <bool>();

            bus.Subscribe("SaveCommand", (cmd) => {
                var saveCommand = (SaveCommand)cmd;

                Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));
                bus.RemoveAllSubscriptions();

                source.SetResult(true);

                return(null);
            });

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await source.Task;
        }
        public void NotTransferMoneyIfNotExistedAccount()
        {
            // Given
            RegisterHandlers(() => new AccountCommandHandler(Repository));
            RegisterHandlers(() => new AccountValueTranferringEventHandler(SagaRepository));
            CommandBus.Publish(new CreateAccount {
                Id = 1.Guid(), DisplayName = "Company", Value = 1000m
            });
            ClearEvents();

            // When
            CommandBus.Publish(new TransferAccountValue {
                From = 1.Guid(), To = 2.Guid(), Value = 40m, SagaId = 123.Guid()
            });

            // Then
            AssertEvent(new AccountValueChanged {
                Id = 1.Guid(), ValueDifference = -40m, SagaId = 123.Guid()
            }, evt => evt.Id == 1.Guid() && evt.Reason == null);
            AssertEvent(new AccountValueChanged {
                Id = 1.Guid(), ValueDifference = +40m, SagaId = 123.Guid(), Reason = "Account 00000000-0000-0000-0000-000000000002 is not exist"
            }, evt => evt.Id == 1.Guid() && evt.Reason != null);
        }
Exemple #26
0
 public static void Execute(ICmd command)
 {
     CommandBus.Publish(command);
     CommandBus.Commit();
 }
        public async Task CommandBus_Subscribes_With_On() {
            var bus = new CommandBus();
            var source = new TaskCompletionSource<bool>();

            dynamic input = new InteropMock() {
                type = "SaveCommand",
                subscriber = (cmd) => {
                    var saveCommand = (SaveCommand)cmd;

                    Assert.That(saveCommand.DeviceId, Is.EqualTo("1234"));
                    bus.RemoveAllSubscriptions();

                    source.SetResult(true);
                    return null;
                }
            };

            bus.On(input);

            var command = new SaveCommand("1234");
            await bus.Publish(command);

            await source.Task;
        }
        private void QueChannel(CommandBus bus)
        {
            using (IModel model = conn.CreateModel())
            {
                var subscription = new Subscription(model, "queue", false);

                while (true)
                {
                    BasicDeliverEventArgs basicDeliveryEventArgs =
                        subscription.Next();
                    var @event = StreamExtension.Deserialize<Vaccine.Events.DomainEvent>(basicDeliveryEventArgs.Body);

                    //Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
                    Console.WriteLine(@event.GetType());

                    bus.Publish(@event);

                    subscription.Ack(basicDeliveryEventArgs);
                }
            }
        }