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 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 cancelable_commands_can_be_canceled()
        {
            var bus     = new CommandBus("local");
            var handler = new CancelableTestCommandHandler();

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

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

            Assert.Throws <CommandCanceledException>(
                () =>
            {
                try
                {
                    bus.Fire(cmd);
                }
                catch (Exception ex)
                {
                    throw ex.InnerException;
                }
            });
        }