Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
        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();
            }
        }
Exemplo n.º 3
0
        public async Task ExecuteDeferredEvents_WhenCalledWithoutEvent_ShouldNotThrow()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            // Act
            await mediator.ExecuteDeferredEvents();
        }
Exemplo n.º 4
0
        public async Task CommandMissing_ShouldThrowEx()
        {
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

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

            await mediator.Send(new CommandMissing { Id = 1 });
        }
Exemplo n.º 5
0
        public async Task LookupQuery_CanLookupData()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ILookupQuery <List <FakeResult> > >(new FindResultForLookup());

            // Act
            var result = await mediator.LoadList <List <FakeResult> >();

            // Assert
            Assert.IsTrue(result.Count == FakeDataStore.Results.Count);
        }
Exemplo n.º 6
0
        public async Task Queries_CanReturnPrimitiveTypes()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <SingleNameCriteria, int> >(new FindPrimitiveTypeByCriteria());

            // Act
            var result = await mediator.Query <SingleNameCriteria, int>(new SingleNameCriteria { Name = "Name1" });

            // Assert
            Assert.IsTrue(result == 1);
        }
Exemplo n.º 7
0
        public async Task Queries_WhenCalledWithCriteria_ShouldReturnResult()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <FakeRangeCriteria, List <FakeResult> > >(new FindFakeResultByRangeCriteria());

            // Act
            var result = await mediator.Query <FakeRangeCriteria, List <FakeResult> >(new FakeRangeCriteria { MinValue = 0, MaxValue = 5 });

            // Assert
            Assert.IsTrue(result.Count == 5);
        }
Exemplo n.º 8
0
        public async Task Commands_WhenExecuting_CanSuccessfulCompleteValidCommand()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

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

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

            // Assert
            Assert.IsFalse(result.ValidationResults.Any());
        }
Exemplo n.º 9
0
        public async Task Commands_CanHandleCommandWithAReturnValue()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommandWithResult> >(new TestCommandWithResultHandler(mediator));

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

            // Assert
            Assert.IsTrue(result.Result <TestCommandResult>().ResultingValue == 5);
        }
Exemplo n.º 10
0
        public async Task Queries_WhenCalledWithPrimitiveCriteria_ShouldReturnResult()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <int, List <FakeResult> > >(new FindFakeResultByPrimitiveType());

            // Act
            var result = await mediator.Query <int, List <FakeResult> >(1);

            // Assert
            Assert.IsTrue(result.FirstOrDefault() != null);
            Assert.IsTrue(result.First().Id == 1);
        }
Exemplo n.º 11
0
        public async Task Commands_CanHandleCommandThatFiresOtherCommandsWithANestedError()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommandWithResult> >(new TestCommandWithResultHandler(mediator));
            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestMultipleCommandWithResult> >(new TestMultipleCommandHandlerWithResult(mediator));

            // Act
            var result = await mediator.Send(new TestMultipleCommandWithResult { Name = "bar", Id = 999 });

            // Assert
            Assert.IsTrue(result.Result <TestCommandResult>() == null && result.ValidationResults.Any());
        }
Exemplo n.º 12
0
        public async Task Commands_CanHandleCommandWithAReturnValueWithValidationFailures()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <ICommandHandler <TestCommandWithResult> >(new TestCommandWithResultHandler(mediator));

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

            var returnedValue = result.Result <TestCommandResult>();

            // Assert
            Assert.IsTrue(returnedValue == null && result.ValidationResults.Any());
        }
Exemplo n.º 13
0
        public async Task Queries_ShouldAllowMultipleQueryDefinitionsPerObject()
        {
            // Arrange
            var handlerFactory = new MessageHandlerRegistry();
            var mediator       = new Mediator(handlerFactory.MultiInstanceFactory, handlerFactory.SingleInstanceFactory);

            handlerFactory.AddHandlersForCommandOrQuery <IQuery <SingleIdCriteria, FakeResult> >(new MultipleQueryTypesInOneObject());
            handlerFactory.AddHandlersForCommandOrQuery <IQuery <SingleNameCriteria, FakeResult> >(new MultipleQueryTypesInOneObject());

            // Act
            var resultByName = await mediator.Query <SingleNameCriteria, FakeResult>(new SingleNameCriteria { Name = "Name2" });

            var resultById = await mediator.Query <SingleIdCriteria, FakeResult>(new SingleIdCriteria { Id = 1 });

            // Assert
            Assert.IsTrue(resultByName.Id == 2);
            Assert.IsTrue(resultById.Id == 1);
        }
Exemplo n.º 14
0
        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();
            }
        }
Exemplo n.º 15
0
        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();
            }
        }
Exemplo n.º 16
0
        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();
            }
        }