public void WhenSendingCommand_ThenInvokesCompatibleHandler()
        {
            var handler = new Mock<ICommandHandler<TestCommand>>();
            var e = new ManualResetEvent(false);
            handler.Setup(x => x.Handle(It.IsAny<TestCommand>()))
                .Callback(() => e.Set());

            var bus = new MemoryCommandBus(handler.Object);

            bus.Send(new TestCommand());

            e.WaitOne(2000);

            handler.Verify(x => x.Handle(It.IsAny<TestCommand>()));
        }
        public void WhenSendingMultipleCommands_ThenInvokesCompatibleHandlerMultipleTimes()
        {
            var handler = new Mock<ICommandHandler<TestCommand>>();
            var e = new ManualResetEvent(false);

            var called = 0;
            handler.Setup(x => x.Handle(It.IsAny<TestCommand>()))
                .Callback(() => { if (Interlocked.Increment(ref called) == 4) e.Set(); });

            var bus = new MemoryCommandBus(handler.Object);

            bus.Send(new[] { new TestCommand(), new TestCommand(), new TestCommand(), new TestCommand() });

            e.WaitOne(2000);

            Assert.Equal(4, called);
        }
        public void WhenSendingCommand_ThenDoesNotInvokeIncompatibleHandler()
        {
            var compatibleHandler = new Mock<ICommandHandler<TestCommand>>();
            var incompatibleHandler = new Mock<ICommandHandler<FooCommand>>();
            var e = new ManualResetEvent(false);

            compatibleHandler.Setup(x => x.Handle(It.IsAny<TestCommand>()))
                .Callback(() => e.Set());

            var bus = new MemoryCommandBus(incompatibleHandler.Object, compatibleHandler.Object);

            bus.Send(new TestCommand());

            e.WaitOne(2000);

            incompatibleHandler.Verify(x => x.Handle(It.IsAny<FooCommand>()), Times.Never());
        }