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); }
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(); } }
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); }
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; }
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()); }
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(); } }
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(); } }