Пример #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);
        }
Пример #2
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;
                }
            });
        }
Пример #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);
        }
Пример #4
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);
        }
Пример #5
0
        public void AddShouldAllowMultipleCommandRegistration()
        {
            // Arrange.
            var map = new MessageHandlerMap();

            // Act.
            map.Add(typeof(CancelOrderCommand), typeof(RemoveOrderCommandHandler));
            map.Add(typeof(DeleteOrderCommand), typeof(RemoveOrderCommandHandler));
            map.Add(typeof(DeleteOrderCommand), typeof(CreateOrderCommand));

            // Assert.
            var registrations = map.GetHandlerTypes();

            Assert.Equal(2, registrations.Count);
            Assert.Equal(typeof(CreateOrderCommand), registrations[typeof(DeleteOrderCommand)][1]);
        }
Пример #6
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;
                }
            });
        }