Esempio n. 1
0
        public async Task ShouldIndicateWhetherAHandlerIsATimeoutHandler()
        {
            var registry = new MessageHandlerRegistry();

            registry.RegisterHandler(typeof(SagaWithTimeoutOfMessage));

            var handlers = registry.GetHandlersFor(typeof(MyMessage));

            Assert.AreEqual(2, handlers.Count);

            var timeoutHandler = handlers.SingleOrDefault(h => h.IsTimeoutHandler);

            Assert.NotNull(timeoutHandler, "Timeout handler should be marked as such");

            var timeoutInstance = new SagaWithTimeoutOfMessage();

            timeoutHandler.Instance = timeoutInstance;
            await timeoutHandler.Invoke(new MyMessage(), new TestableInvokeHandlerContext());

            Assert.True(timeoutInstance.TimeoutCalled);
            Assert.False(timeoutInstance.HandlerCalled);

            var regularHandler = handlers.SingleOrDefault(h => !h.IsTimeoutHandler);

            Assert.NotNull(regularHandler, "Regular handler should be marked as timeout handler");

            var regularInstance = new SagaWithTimeoutOfMessage();

            regularHandler.Instance = regularInstance;
            await regularHandler.Invoke(new MyMessage(), new TestableInvokeHandlerContext());

            Assert.False(regularInstance.TimeoutCalled);
            Assert.True(regularInstance.HandlerCalled);
        }
Esempio n. 2
0
        public async Task ExecuteDeferredEvents_WhenCalled_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<FakeEvent>>
            {
                _autoSubstitute.SubstituteFor<HandlerDeferringMultipleEvents>(mediator),
                _autoSubstitute.SubstituteFor<HandlerDeferringSingleEvent>(mediator),
                _autoSubstitute.SubstituteFor<HandlerWithoutAdditionalEvents>()
            });

            mediator.DeferEvent(@event);

            // Act
            await mediator.ExecuteDeferredEvents();

            // Assert
            foreach (var handler in handlerFactory.GetHandlersFor<FakeEvent>())
            {
                handler.Received().Handle(Arg.Any<FakeEvent>()).FireAndForget();
            }
        }
Esempio n. 3
0
        public static MessageHandler GetCachedHandlerForMessage <TMessage>(this MessageHandlerRegistry cache)
        {
            var handler = cache.GetHandlersFor(typeof(TMessage)).Single();

            handler.Instance = Activator.CreateInstance(handler.HandlerType);
            return(handler);
        }
        static List <Type> GetMessageTypesHandledByThisEndpoint(MessageHandlerRegistry handlerRegistry, Conventions conventions, SubscribeSettings settings)
        {
            var messageTypesHandled = handlerRegistry.GetMessageTypes()                                                                                                                 //get all potential messages
                                      .Where(t => !conventions.IsInSystemConventionList(t))                                                                                             //never auto-subscribe system messages
                                      .Where(t => !conventions.IsCommandType(t))                                                                                                        //commands should never be subscribed to
                                      .Where(t => conventions.IsEventType(t))                                                                                                           //only events unless the user asked for all messages
                                      .Where(t => settings.AutoSubscribeSagas || handlerRegistry.GetHandlersFor(t).Any(handler => !typeof(Saga).IsAssignableFrom(handler.HandlerType))) //get messages with other handlers than sagas if needed
                                      .ToList();

            return(messageTypesHandled);
        }
Esempio n. 5
0
        static List<Type> GetMessageTypesHandledByThisEndpoint(MessageHandlerRegistry handlerRegistry, Conventions conventions, SubscribeSettings settings)
        {
            var messageTypesHandled = handlerRegistry.GetMessageTypes() //get all potential messages
                .Where(t => !conventions.IsInSystemConventionList(t)) //never auto-subscribe system messages
                .Where(t => !conventions.IsCommandType(t)) //commands should never be subscribed to
                .Where(conventions.IsEventType) //only events unless the user asked for all messages
                .Where(t => settings.AutoSubscribeSagas || handlerRegistry.GetHandlersFor(t).Any(handler => !typeof(Saga).IsAssignableFrom(handler.HandlerType))) //get messages with other handlers than sagas if needed
                .ToList();

            return messageTypesHandled;
        }
Esempio n. 6
0
        public override Task Invoke(IIncomingLogicalMessageContext context, Func <Task> next)
        {
            var type             = context.Message.MessageType;
            var numberOfHandlers = messageHandlerRegistry.GetHandlersFor(type).Count;

            if (numberOfHandlers > 1)
            {
                logger.WarnFormat("Message of type '{0}' is handled by more than a single handler ({1}). Ensure transport supports transactions or capable of retries to have more than one handler. Otherwise break down into multiple message types each handled by a separate handler."
                                  , type, numberOfHandlers);
            }

            return(next());
        }
Esempio n. 7
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();
            }
        }
Esempio n. 8
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();
            }
        }
Esempio n. 9
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();
            }
        }
Esempio n. 10
-1
        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();
            }
        }