Ejemplo n.º 1
0
        public void DispatchShouldSupportMultipleHandlers()
        {
            // Arrange.
            var evt = new OrderCancelledEvent();

            var executed = 0;
            var handler1 = new OrderCancelledEventHandler1(() => { executed++; });
            var handler2 = new OrderCancelledEventHandler2(() => { executed++; });

            var map = new MessageHandlerMap();
            map.Add(typeof(OrderCancelledEvent), handler1.GetType());
            map.Add(typeof(OrderCancelledEvent), handler2.GetType());

            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(handler1.GetType())).Returns(handler1);
            A.CallTo(() => container.Resolve(handler2.GetType())).Returns(handler2);

            var dispatcher = new MessageDispatcher(container, map);

            // Act.
            dispatcher.Dispatch("CreateOrder", new Envelope<OrderCancelledEvent>(evt)).Wait();

            // Assert.
            Assert.Equal(2, executed);
        }
Ejemplo n.º 2
0
        public void DispatchShouldThrowExceptionIfHandlerNotFound()
        {
            // Arrange.
            var map = new MessageHandlerMap();
            map.Add(typeof(CancelOrderCommand), typeof(RemoveOrderCommandHandler));
            var container = A.Fake<IContainer>();
            var dispatcher = new MessageDispatcher(container, map);

            // Act.
            Assert.Throws<MessageDispatcherException>(() =>
            {
                try
                {
                    dispatcher.Dispatch("CreateOrder", new Envelope<CreateOrderCommand>(new CreateOrderCommand())).Wait();
                }
                catch (AggregateException ex)
                {
                    throw ex.InnerException;
                }
            });
        }
Ejemplo n.º 3
0
        public void DispatchShouldExecuteHandler()
        {
            // Arrange.
            var command = new CancelOrderCommand();

            var executed = false;
            var handler = A.Fake<ICommandHandler<CancelOrderCommand>>();
            A.CallTo(() => handler.Handle(command)).Invokes(c => { executed = true; }).Returns(Task.FromResult(0));
            
            var map = new MessageHandlerMap();
            map.Add(typeof(CancelOrderCommand), handler.GetType());
            
            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(handler.GetType())).Returns(handler);

            var dispatcher = new MessageDispatcher(container, map);

            // Act.
            dispatcher.Dispatch("CreateOrder", new Envelope<CancelOrderCommand>(command)).Wait();

            // Assert.
            A.CallTo(() => handler.Handle(command)).MustHaveHappened(Repeated.Exactly.Once);
            Assert.True(executed);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handle the message.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="brokeredMessage"></param>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        /// <returns></returns>
        private async Task OnHandleMessage(IMessageReceiver receiver, MessageSession session, BrokeredMessage brokeredMessage, MessageHandlerMap handlerMap)
        {
            MessagingEventSource.Log.MessageReceived(brokeredMessage.ContentType, receiver, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

            // Create new isolated scope.
            using (var scope = Configuration.Container.BeginScope())
            {
                var envelope = await BuildMessage(brokeredMessage);

                // Dispatch the message.
                var dispatcher = new MessageDispatcher(scope, handlerMap);
                await dispatcher.Dispatch(brokeredMessage.ContentType, envelope, session != null ? new Session(session, Configuration.Serializer) : null);

                // Cleanup the message.
                var messageReceiver = receiver as IMessagePump;
                if (messageReceiver != null && messageReceiver.Mode == ReceiveMode.PeekLock)
                {
                    // Log.
                    MessagingEventSource.Log.MessageCompleting(brokeredMessage.ContentType, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

                    // Complete the message.
                    brokeredMessage.TryComplete();
                    return;
                }

                // Cleanup the message.
                var sessionMessageReceiver = receiver as ISessionMessagePump;
                if (sessionMessageReceiver != null && sessionMessageReceiver.Mode == ReceiveMode.PeekLock)
                {
                    // Log.
                    MessagingEventSource.Log.MessageCompleting(brokeredMessage.ContentType, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

                    // Complete the message.
                    brokeredMessage.TryComplete();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Handle the message.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="brokeredMessage"></param>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        /// <returns></returns>
        private async Task OnHandleMessage(IMessageReceiver receiver, MessageSession session, BrokeredMessage brokeredMessage, MessageHandlerMap handlerMap)
        {
            MessengerEventSource.Log.MessageReceived(brokeredMessage.ContentType, receiver, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

            // Create new isolated scope.
            using (var scope = Configuration.Container.BeginScope())
            {
                var envelope = await BuildMessage(brokeredMessage)
                    .ConfigureAwait(false);

                // Dispatch the message.
                var dispatcher = new MessageDispatcher(scope, handlerMap);
                await dispatcher.Dispatch(brokeredMessage.ContentType, envelope, session != null ? new Session(session, Configuration.Serializer) : null)
                    .ConfigureAwait(false);
            }
        }
Ejemplo n.º 6
0
        public void DispatchShouldThrowIfHandlerNull()
        {
            // Arrange.
            var evt = new OrderCancelledEvent();
            var map = new MessageHandlerMap();
            map.Add(typeof(OrderCancelledEvent), typeof(OrderCancelledEventHandler1));

            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(typeof(OrderCancelledEventHandler1))).Returns(null);

            // Act.
            Assert.Throws<NullReferenceException>(() =>
            {
                try
                {
                    var dispatcher = new MessageDispatcher(container, map);
                    dispatcher.Dispatch("CreateOrder", new Envelope<OrderCancelledEvent>(evt)).Wait();
                }
                catch (AggregateException ex)
                {
                    throw ex.InnerException;
                }
            });
        }
Ejemplo n.º 7
0
        public void DispatchShouldSupportGiveAccessToSession()
        {
            // Arrange.
            var evt = new OrderCancelledEvent();
            var handler = new SessionOrderCancelledEventHandler();

            var map = new MessageHandlerMap();
            map.Add(typeof(OrderCancelledEvent), handler.GetType());

            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(handler.GetType())).Returns(handler);

            var dispatcher = new MessageDispatcher(container, map);
            var session = A.Fake<ISession>();

            // Act.
            var envelope = new Envelope<OrderCancelledEvent>(evt);
            dispatcher.Dispatch("CreateOrder", envelope, session).Wait();

            // Assert.
            Assert.Equal(session, handler.Session);
        }