public void unsubscribe_should_not_remove_other_handlers()
        {
            var  bus           = new CommandBus("local");
            long proccessedCmd = 0;
            var  hndl          = new AdHocCommandHandler <TestCommands.TestCommand>(
                cmd =>
            {
                Interlocked.Increment(ref proccessedCmd);
                return(true);
            });

            bus.Subscribe(hndl);
            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand2>(
                              cmd =>
            {
                Interlocked.Increment(ref proccessedCmd);
                return(true);
            }));
            bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null));
            bus.Unsubscribe(hndl);
            Assert.Throws <CommandNotHandledException>(
                () => bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));

            bus.Fire(new TestCommands.TestCommand2(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref proccessedCmd) == 2,
                msg: "Expected command handled twice, got" + proccessedCmd);
        }
        public void tryfire_commands_should_not_call_other_commands()
        {
            var  bus    = new CommandBus("local");
            long gotCmd = 0;


            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Increment(ref gotCmd);
                return(true);
            }));
            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand2>(
                              cmd =>
            {
                Interlocked.Increment(ref gotCmd);
                return(true);
            }));
            CommandResponse response;

            var passed = bus.TryFire(
                new TestCommands.TestCommand(Guid.NewGuid(), null), out response, TimeSpan.FromMilliseconds(1500));

            Assert.True(passed, "Expected false return");
            Thread.Sleep(100); //let other threads complete
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd) == 1,
                                   msg: "Expected command received once, got " + gotCmd);
            Assert.IsType(typeof(Success), response);
        }
        public void handlers_that_throw_exceptions_rethrow_on_fire()
        {
            var bus = new CommandBus("local");

            long gotCmd      = 0;
            long gotResponse = 0;
            long gotAck      = 0;
            long msgCount    = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd, 1);
                throw new CommandException("I knew this would happen.", cmd);
            },
                              wrapExceptions: false));

            bus.Subscribe(new AdHocHandler <CommandResponse>(
                              cmd => Interlocked.Exchange(ref gotResponse, 1)));

            bus.Subscribe(new AdHocHandler <AckCommand>(
                              cmd => Interlocked.Exchange(ref gotAck, 1)));

            bus.Subscribe(new AdHocHandler <Message>(
                              cmd => Interlocked.Increment(ref msgCount)));

            Assert.Throws <CommandException>(() =>
                                             bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));

            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref msgCount) == 3, null, "Expected 3 Messages");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotAck) == 1, null, "Expected Ack");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd) == 1, null, "Expected Command was handled");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotResponse) == 1, null, "Expected Response");
        }
        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 typed_tryfire_passing_command_should_pass()
        {
            var       bus  = new CommandBus("local");
            const int data = 12356;

            long gotResponse  = 0;
            long responseData = 0;

            bus.Subscribe(new AdHocTypedCommandHandler <TestCommands.TypedTestCommand, TestCommands.TestCommandResponse>(
                              cmd => new TestCommands.TestCommandResponse(cmd, data)));
            bus.Subscribe(new AdHocHandler <TestCommands.TestCommandResponse>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotResponse, 1);
                Interlocked.Exchange(ref responseData, cmd.Data);
            }
                              ));

            CommandResponse response;
            var             pass = bus.TryFire(new TestCommands.TypedTestCommand(Guid.NewGuid(), null), out response);

            Assert.True(pass, "Expected true return value");
            Assert.IsType(typeof(TestCommands.TestCommandResponse), response);
            Assert.Equal(data, ((TestCommands.TestCommandResponse)response).Data);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotResponse) == 1, null, "Expected Success");
            Assert.IsOrBecomesTrue(() => data == responseData);
        }
        public void cancel_should_not_cancel_others()
        {
            var bus     = new CommandBus("local");
            var handler = new CancelableTestCommandHandler();

            bus.Subscribe((IHandleCommand <TestCommands.TestCommand>)handler);
            bus.Subscribe((IHandleCommand <TestCommands.TestCommand2>)handler);
            var handler2 = new TestCommandHandler();

            bus.Subscribe((IHandleCommand <TestCommands.TestCommand3>)handler2);
            var cmd1 = new TestCommands.TestCommand(Guid.NewGuid(), null);
            var cmd2 = new TestCommands.TestCommand2(Guid.NewGuid(), null);
            var cmd3 = new TestCommands.TestCommand3(Guid.NewGuid(), null);

            Task.Delay(100).ContinueWith(t => bus.RequestCancel(cmd1));
            Task.Run(() => bus.Fire(cmd2));
            Task.Run(() => bus.Fire(cmd3));
            Assert.Throws <CommandCanceledException>(
                () =>
            {
                try
                {
                    bus.Fire(cmd1);
                }
                catch (Exception ex)
                {
                    throw ex.InnerException;
                }
            });
        }
        public void slow_commands_should_throw_timeout()
        {
            var  bus             = new CommandBus("local");
            long gotCmd1         = 0;
            long cancelRecieved  = 0;
            long cancelPublished = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd1, 1);
                Task.Delay(1000).Wait();
                return(true);
            },
                              (cancel, currentCmdId) =>
            {
                if (cancel.CommandType == typeof(TestCommands.TestCommand) &&
                    (currentCmdId == cancel.CommandId))
                {
                    Interlocked.Increment(ref cancelRecieved);
                }
                return(Unit.Default);
            }));
            bus.Subscribe(new AdHocHandler <CancelCommand>(c => Interlocked.Increment(ref cancelPublished)));

            Assert.Throws <CommandTimedOutException>(() =>
                                                     bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd1) == 1, msg: "Expected Cmd1 handled");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref cancelPublished) == 1, msg: "Expected cancel published");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref cancelRecieved) == 1, msg: "Expected cancel received");
        }
        public void typed_failing_tryfire_command_should_fail()
        {
            var          bus          = new CommandBus("local");
            const int    data         = 12356;
            const string errText      = @"I knew this would happen.";
            long         gotResponse  = 0;
            long         responseData = 0;

            bus.Subscribe(new AdHocTypedCommandHandler <TestCommands.TypedTestCommand, TestCommands.TestFailedCommandResponse>(
                              cmd => new TestCommands.TestFailedCommandResponse(cmd, new CommandException(errText, cmd), data)));
            bus.Subscribe(new AdHocHandler <TestCommands.TestFailedCommandResponse>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotResponse, 1);
                Interlocked.Exchange(ref responseData, cmd.Data);
            }
                              ));

            CommandResponse response;
            var             command = new TestCommands.TypedTestCommand(Guid.NewGuid(), null);
            var             passed  = bus.TryFire(command, out response);

            Assert.False(passed, "Expected false return");
            Assert.IsType(typeof(TestCommands.TestFailedCommandResponse), response);
            Assert.IsType(typeof(CommandException), ((Fail)response).Exception);
            Assert.Equal($"{command.GetType().Name}: {errText}", ((Fail)response).Exception.Message);
            Assert.Equal(data, ((TestCommands.TestFailedCommandResponse)response).Data);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotResponse) == 1, null, "Expected Fail");
            Assert.IsOrBecomesTrue(() => data == responseData);
        }
        public void multiple_commands_can_register()
        {
            var  bus     = new CommandBus("local");
            long gotCmd1 = 0;
            long gotCmd2 = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd1, 1);
                return(true);
            }));
            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand2>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd2, 1);
                return(true);
            }));
            CommandResponse result;

            bus.TryFire(new TestCommands.TestCommand(Guid.NewGuid(), null), out result);
            Assert.True(result is Success);
            bus.TryFire(new TestCommands.TestCommand2(Guid.NewGuid(), null), out result);
            Assert.True(result is Success);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd1) == 1, msg: "Expected Cmd1 handled");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd2) == 1, msg: "Expected Cmd2 handled");
        }
        public void commands_should_not_deadlock()
        {
            var  bus     = new CommandBus("local");
            long gotCmd1 = 0;
            long gotCmd2 = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd1, 1);
                bus.TryFire(new TestCommands.TestCommand2(Guid.NewGuid(), null));
                return(true);
            }));
            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand2>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd2, 1);
                return(true);
            }));
            CommandResponse result;

            bus.TryFire(new TestCommands.TestCommand(Guid.NewGuid(), null), out result);

            Assert.True(result is Success, $"Got Fail: {(result as Fail)?.Exception.Message}");

            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd1) == 1, msg: "Expected Cmd1 handled");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd2) == 1, msg: "Expected Cmd2 handled");
        }
        public void concurrent_commands_should_pass()
        {
            var  bus        = new CommandBus("local", slowCmdThreshold: TimeSpan.FromMilliseconds(50));
            long gotSuccess = 0;
            long gotFail    = 0;
            long gotTimeout = 0;
            Func <Command, bool> testAction        = cmd => { Interlocked.Increment(ref gotSuccess); return(true); };
            Func <Command, bool> testFailAction    = cmd => { Interlocked.Increment(ref gotFail); return(true); };
            Func <Command, bool> testTimeoutAction = cmd =>
            {
                Interlocked.Increment(ref gotTimeout);
                Thread.Sleep(55); //timeout == 50 ms
                return(true);
            };

            Parallel.Invoke(
                () => bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(testAction)),
                () => bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand2>(testTimeoutAction)),
                () => bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand3>(testFailAction))
                );
            Assert.True(gotSuccess == 0, $"gotSuccess should be 0, found { gotSuccess}");
            for (int i = 0; i < 50; i++)
            {
                Parallel.Invoke(
                    () => bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)),
                    () => bus.TryFire(new TestCommands.TestCommand2(Guid.NewGuid(), null)),
                    () => bus.TryFire(new TestCommands.TestCommand3(Guid.NewGuid(), null)));
            }

            Assert.True(gotSuccess == 50, $"gotSuccess should be 50, found { gotSuccess}");
            Assert.True(gotFail == 50, $"gotFail should be 50, found { gotFail}");
            Assert.True(gotTimeout == 50, $"gotTimeout should be 50, found { gotTimeout}");
        }
        public void command_handler_respondes_to_command_message()
        {
            var  bus         = new CommandBus("temp");
            long gotCmd      = 0;
            long gotResponse = 0;
            long gotAck      = 0;
            long msgCount    = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd, 1);
                return(true);
            }));

            bus.Subscribe(new AdHocHandler <CommandResponse>(
                              cmd => Interlocked.Exchange(ref gotResponse, 1)));

            bus.Subscribe(new AdHocHandler <AckCommand>(
                              cmd => Interlocked.Exchange(ref gotAck, 1)));

            bus.Subscribe(new AdHocHandler <Message>(
                              cmd => Interlocked.Increment(ref msgCount)));

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

            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref msgCount) == 3, null, "Expected 3 Messages");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotAck) == 1, null, "Expected Ack");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd) == 1, null, "Expected Command was handled");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotResponse) == 1, null, "Expected Response");
        }
Esempio n. 13
0
        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 cannot_subscribe_twice_on_same_bus()
        {
            var bus = new CommandBus("local");

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(cmd => true));
            Assert.True(bus.HasSubscriberFor <TestCommands.TestCommand>());
            Assert.Throws <ExistingHandlerException>(
                () => bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(cmd => true)));
        }
        public void fire_failing_command_should_fail()
        {
            var  bus     = new CommandBus("local");
            long gotFail = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(cmd => false));
            bus.Subscribe(new AdHocHandler <Fail>(cmd => Interlocked.Exchange(ref gotFail, 1)));
            Assert.Throws <CommandException>(
                () => bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotFail) == 1, null, "Expected Fail");
        }
        public void tryfire_failing_command_should_fail()
        {
            var  bus     = new CommandBus("local");
            long gotFail = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(cmd => false));
            bus.Subscribe(new AdHocHandler <Fail>(cmd => Interlocked.Exchange(ref gotFail, 1)));
            CommandResponse response;
            var             pass = bus.TryFire(new TestCommands.TestCommand(Guid.NewGuid(), null), out response);

            Assert.False(pass, "Expected false return value");
            Assert.IsType(typeof(Fail), response);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotFail) == 1, null, "Expected Fail");
        }
        public void tryfire_passing_command_should_pass()
        {
            var  bus        = new CommandBus("local");
            long gotSuccess = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(cmd => true));
            bus.Subscribe(new AdHocHandler <Success>(cmd => Interlocked.Exchange(ref gotSuccess, 1)));
            CommandResponse response;
            var             pass = bus.TryFire(new TestCommands.TestCommand(Guid.NewGuid(), null), out response);

            Assert.True(pass, "Expected true return value");
            Assert.IsType(typeof(Success), response);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotSuccess) == 1, null, "Expected Success");
        }
        public void fire_publishes_command_as_message()
        {
            var  bus   = new CommandBus("temp");
            long gotIt = 0;

            bus.Subscribe(new AdHocHandler <Command>(
                              cmd => { if (cmd is TestCommands.TestCommand)
                                       {
                                           Interlocked.Exchange(ref gotIt, 1);
                                       }
                              }));
            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(cmd => true));
            bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotIt) == 1);
        }
        public void tryfire_slow_commands_should_return_timeout()
        {
            var  bus            = new CommandBus("local");
            long gotCmd1        = 0;
            long cancelRecieved = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd1, 1);
                Task.Delay(1000).Wait();
                return(true);
            },
                              (cancel, currentCmdId) =>
            {
                if (cancel.CommandType == typeof(TestCommands.TestCommand) &&
                    (currentCmdId == cancel.CommandId))
                {
                    Interlocked.Increment(ref cancelRecieved);
                }
                return(Unit.Default);
            }));

            CommandResponse response;
            var             passed = bus.TryFire(new TestCommands.TestCommand(Guid.NewGuid(), null), out response);

            Assert.False(passed, "Expected false return");
            Assert.IsType(typeof(Fail), response);
            Assert.IsType(typeof(CommandTimedOutException), ((Fail)response).Exception);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd1) == 1, msg: "Expected Cmd1 handled");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref cancelRecieved) == 1, msg: "Expected cancel received");
        }
Esempio n. 20
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 fire_passing_command_should_pass()
        {
            var bus = new CommandBus("local");

            long gotSuccess = 0;
            long gotFail    = 0;
            long gotCancel  = 0;

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(cmd => true));
            bus.Subscribe(new AdHocHandler <Success>(cmd => Interlocked.Exchange(ref gotSuccess, 1)));
            bus.Subscribe(new AdHocHandler <Fail>(cmd => Interlocked.Exchange(ref gotFail, 1)));
            bus.Subscribe(new AdHocHandler <Canceled>(cmd => Interlocked.Exchange(ref gotCancel, 1)));
            bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotSuccess) == 1, null, "Expected Success");
            Assert.True(gotFail == 0, "Unexpected fail received.");
            Assert.True(gotCancel == 0, "Unexpected Cancel received.");
        }
        public void handlers_that_wrap_exceptions_return_on_tryfire()
        {
            var bus = new CommandBus("local");

            long         gotCmd      = 0;
            long         gotResponse = 0;
            long         gotAck      = 0;
            long         msgCount    = 0;
            const string errText     = @"I knew this would happen.";

            bus.Subscribe(new AdHocCommandHandler <TestCommands.TestCommand>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotCmd, 1);
                throw new CommandException(errText, cmd);
            },
                              // ReSharper disable once RedundantArgumentDefaultValue
                              wrapExceptions: true));

            bus.Subscribe(new AdHocHandler <CommandResponse>(
                              cmd => Interlocked.Exchange(ref gotResponse, 1)));

            bus.Subscribe(new AdHocHandler <AckCommand>(
                              cmd => Interlocked.Exchange(ref gotAck, 1)));

            bus.Subscribe(new AdHocHandler <Message>(
                              cmd => Interlocked.Increment(ref msgCount)));

            CommandResponse response;
            var             command = new TestCommands.TestCommand(Guid.NewGuid(), null);
            var             passed  = bus.TryFire(command, out response);

            Assert.False(passed, "Expected false return");
            Assert.IsType(typeof(Fail), response);
            Assert.IsType(typeof(CommandException), ((Fail)response).Exception);

            Assert.True(string.Equals($"{command.GetType().Name}: {errText}", ((Fail)response).Exception.Message, StringComparison.Ordinal));


            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref msgCount) == 3, null, "Expected 3 Messages");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotAck) == 1, null, "Expected Ack");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd) == 1, null, "Expected Command was handled");
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotResponse) == 1, null, "Expected Response");
        }
        public void unsubscribed_commands_should_throw_ack_timeout()
        {
            var  bus             = new CommandBus("local");
            long cancelPublished = 0;

            bus.Subscribe(new AdHocHandler <CancelCommand>(c => Interlocked.Increment(ref cancelPublished)));
            Assert.Throws <CommandNotHandledException>(() =>
                                                       bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref cancelPublished) == 1, msg: "Expected cancel published");
        }
        public void command_handler_acks_command_message()
        {
            var  bus    = new CommandBus("temp");
            long gotCmd = 0;
            long gotAck = 0;
            var  hndl   =
                new AdHocCommandHandler <TestCommands.TestCommand>(
                    cmd => Interlocked.Exchange(ref gotCmd, 1) == 0);

            bus.Subscribe(hndl);
            var ackHndl =
                new AdHocHandler <AckCommand>(
                    cmd => Interlocked.Exchange(ref gotAck, 1));

            bus.Subscribe(ackHndl);
            bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotAck) == 1);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd) == 1);
        }
        public void unsubscribed_commands_should_throw_ack_timeout()
        {
            var  bus = new CommandBus("local");
            long publishedMessageCount = 0;

            bus.Subscribe(new AdHocHandler <Message>(c => Interlocked.Increment(ref publishedMessageCount)));
            Assert.Throws <CommandNotHandledException>(() =>
                                                       bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));

            Assert.True(Interlocked.Read(ref publishedMessageCount) == 0, userMessage: "Expected no messages published");
        }
        public void noncancelable_commands_will_ignore_cancel()
        {
            var bus     = new CommandBus("local");
            var handler = new TestCommandHandler();

            bus.Subscribe((IHandleCommand <TestCommands.TestCommand3>)handler);
            var cmd = new TestCommands.TestCommand3(Guid.NewGuid(), null);

            Task.Delay(100).ContinueWith(t => bus.RequestCancel(cmd));

            bus.Fire(cmd);
        }
Esempio n. 27
0
        public void CommandBus_Throws_On_Invalid_Command_String()
        {
            Action test = () => {
                var bus = new CommandBus();
                bus.Subscribe("TestCommand", (cmd) => null);
            };

            var expectedMessage = "Command TestCommand does not exist.";

            Assert.That(new TestDelegate(test),
                        Throws.Exception.With.Message.EqualTo(expectedMessage));
        }
        public static void Start(IServiceCollection services)
        {
            var eventBus = new EventBus();
            var requestCleaningHandler          = Decorate(new RequestCleaningHandler(eventBus));
            var eventBusCleaningRequestedBridge = new EventBusCleaningRequestedBridge(Settings.ConfirmUrl);
            var commandBus = new CommandBus();

            eventBus.Subscribe(eventBusCleaningRequestedBridge);
            commandBus.Subscribe(requestCleaningHandler);

            services.AddSingleton <ICommandBus>(commandBus);
            services.AddSingleton <IEventBus>(eventBus);
        }
        public void typed_fire_passing_command_should_pass()
        {
            var       bus  = new CommandBus("local");
            const int data = 12356;

            long gotResponse  = 0;
            long responseData = 0;

            bus.Subscribe(new AdHocTypedCommandHandler <TestCommands.TypedTestCommand, TestCommands.TestCommandResponse>(
                              cmd => new TestCommands.TestCommandResponse(cmd, data)));
            bus.Subscribe(new AdHocHandler <TestCommands.TestCommandResponse>(
                              cmd =>
            {
                Interlocked.Exchange(ref gotResponse, 1);
                Interlocked.Exchange(ref responseData, cmd.Data);
            }
                              ));

            bus.Fire(new TestCommands.TypedTestCommand(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotResponse) == 1, null, "Expected Success");
            Assert.IsOrBecomesTrue(() => data == responseData);
        }
        protected override void Assert()
        {
            var eventBus   = Substitute.For <IEventBus>();
            var commandBus = new CommandBus();
            var sut        = new RequestCleaningHandler(eventBus);

            commandBus.Subscribe(sut);
            commandBus.Handle(when);
            foreach (var evt in then)
            {
                eventBus.Received().Publish(evt);
            }
        }
        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);
        }
Esempio n. 32
0
        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;
        }
Esempio n. 33
0
        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;
        }
Esempio n. 34
0
        public void CommandBus_Throws_On_Invalid_Command_String() {
            Action test = () => {
                var bus = new CommandBus();
                bus.Subscribe("TestCommand", (cmd) => null);
            };

            var expectedMessage = "Command TestCommand does not exist.";
            Assert.That(new TestDelegate(test),
                Throws.Exception.With.Message.EqualTo(expectedMessage));
        }