public async Task ExecuteDeferredEvents_WhenCalledWithInterface_ShouldCallAllEventHandlers()
        {
            // Arrange
            var @event = new FakeEvent {
                Id = 1
            };
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <IFakeEvent> >
            {
                _autoSubstitute.SubstituteFor <HandlerForInterfaceDeferringMultipleEvents>(mediator),
                _autoSubstitute.SubstituteFor <HandlerForInterfaceDeferringSingleEvent>(mediator),
                _autoSubstitute.SubstituteFor <HandlerForInterfaceWithoutAdditionalEvents>()
            });

            mediator.DeferEvent <IFakeEvent>(@event);

            // Act
            await mediator.ExecuteDeferredEvents();

            // Assert
            foreach (var handler in handlerFactory.GetHandlersFor <IFakeEvent>())
            {
                handler.Received().Handle(Arg.Any <FakeEvent>()).FireAndForget();
            }
        }
        public async Task ExecuteDeferredEvents_CanAddFromMultipleThreads()
        {
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEvent> >
            {
                _autoSubstitute.SubstituteFor <HandlerWithoutAdditionalEvents>()
            });

            var options = new ParallelOptions {
                MaxDegreeOfParallelism = 10
            };

            //Make sure the mediator can handle events being added from multiple threads (should never lose events)
            Parallel.For(0, 10, options, i =>
            {
                for (var j = 0; j < 1000; ++j)
                {
                    mediator.DeferEvent(new FakeEvent {
                        Id = (i * 1000) + j
                    });
                }
            });

            await mediator.ExecuteDeferredEvents();

            foreach (var handler in handlerFactory.GetHandlersFor <FakeEvent>())
            {
                handler.Received(10000).Handle(Arg.Any <FakeEvent>()).FireAndForget();
            }
        }
        public async Task DeferEvents_CanDeferMultipleEvents()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEvent> >
            {
                Substitute.For <HandlerDeferringMultipleEvents>(mediator),
                Substitute.For <HandlerDeferringSingleEvent>(mediator),
                Substitute.For <HandlerWithoutAdditionalEvents>()
            });

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEventFromHandler> >
            {
                Substitute.For <DependentEventHandler>()
            });

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommand> >(new TestCommandHandler(mediator));

            // Act
            var result = await mediator.Send(new TestCommand { Id = 1 });

            Assert.IsFalse(result.ValidationResults.Any());

            // Assert
            foreach (var handler in handlerFactory.GetHandlersFor <FakeEvent>())
            {
                handler.Received().Handle(Arg.Any <FakeEvent>()).FireAndForget();
            }

            foreach (var handler in handlerFactory.GetHandlersFor <FakeEventFromHandler>())
            {
                handler.Received().Handle(Arg.Any <FakeEventFromHandler>()).FireAndForget();
            }
        }
        public async Task ExecuteDeferredEvents_WhenCalledWithoutRegisteredHandlers_ShouldNotCallAnyHandlers()
        {
            // Arrange
            var @event = new FakeEvent {
                Id = 1
            };
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            mediator.DeferEvent(@event);

            // Act
            await mediator.ExecuteDeferredEvents();

            // Assert
            foreach (var handler in handlerFactory.GetHandlersFor <FakeEvent>())
            {
                handler.DidNotReceive().Handle(Arg.Any <FakeEvent>()).FireAndForget();
            }
        }
        public async Task ExecuteDeferredEvents_WhenCalled_ShouldExecuteEventHandlersForEventsFiredInHandlers()
        {
            // Arrange
            var triggerEvent = new FakeEvent {
                Id = 1
            };

            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEvent> >
            {
                new HandlerDeferringMultipleEvents(mediator),
                new HandlerDeferringSingleEvent(mediator),
                new HandlerWithoutAdditionalEvents()
            });

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEventFromHandler> >
            {
                new DependentEventHandler()
            });

            handlerFactory.AddHandlersForEvent(new List <IEventHandler <FakeEventTwoFromHandler> >
            {
                _autoSubstitute.SubstituteFor <ChainedEventHandler>()
            });

            // Act
            mediator.DeferEvent(triggerEvent);
            await mediator.ExecuteDeferredEvents();

            // Assert
            foreach (var handler in handlerFactory.GetHandlersFor <FakeEventTwoFromHandler>())
            {
                handler.Received(1).Handle(Arg.Any <FakeEventTwoFromHandler>()).FireAndForget();
            }
        }