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

            bus.Subscribe(hndl);
            Assert.IsOrBecomesTrue(() => bus.HasSubscriberFor <TestCommands.TestCommand>());

            bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null));
            bus.Unsubscribe(hndl);
            Assert.IsOrBecomesFalse(() => bus.HasSubscriberFor <TestCommands.TestCommand>());

            Assert.Throws <CommandNotHandledException>(
                () => bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));
            bus.Subscribe(hndl);
            Assert.IsOrBecomesTrue(() => bus.HasSubscriberFor <TestCommands.TestCommand>());

            bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref proccessedCmd) == 2,
                msg: "Expected command handled twice, got" + proccessedCmd);
        }
        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.IsOrBecomesTrue(() => bus.HasSubscriberFor <TestCommands.TestCommand>());
            Assert.Throws <CommandException>(
                () => bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null)));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotFail) == 1, null, "Expected Fail");
        }
        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);
            }
                              ));
            Assert.IsOrBecomesTrue(() => bus.HasSubscriberFor <TestCommands.TypedTestCommand>());

            bus.Fire(new TestCommands.TypedTestCommand(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotResponse) == 1, null, "Expected Success");
            Assert.IsOrBecomesTrue(() => data == responseData);
        }