public async Task HandleAsync_MultipleHandlers_RunsCorrectHandler()
        {
            //arrange
            var handlerFactory = new DefaultEventHandlerFactory();

            var handled1 = false;
            var handled2 = false;

            var mockHandler1 = new Mock <IEventHandler <CustomEvent1> >();

            mockHandler1
            .Setup(o => o.HandleEventAsync(It.IsAny <CustomEvent1>()))
            .Returns(() => Task.FromResult(0))
            .Callback(() => handled1 = true);

            var mockHandler2 = new Mock <IEventHandler <CustomEvent2> >();

            mockHandler2
            .Setup(o => o.HandleEventAsync(It.IsAny <CustomEvent2>()))
            .Returns(() => Task.FromResult(0))
            .Callback(() => handled2 = true);

            //register
            handlerFactory.RegisterHandler(mockHandler1.Object);
            handlerFactory.RegisterHandler(mockHandler2.Object);

            //act
            await handlerFactory.HandleAsync(new CustomEvent1()).ConfigureAwait(false);

            //assert
            Assert.True(handled1);
            Assert.False(handled2);
        }
Пример #2
0
        public async Task CanPublishUsingInMemoryBus()
        {
            //arrange
            var eventHandlerFactory = new DefaultEventHandlerFactory();
            var eventBus            = new InMemoryEventBus(eventHandlerFactory);
            var eventPublisher      = new EventPublisher(eventBus);
            var eventHandler        = new CustomEventHandler();

            eventHandlerFactory.RegisterHandler(eventHandler);

            var evt = new CustomEvent("Test message");

            //act
            await eventPublisher.PublishAsync(evt).ConfigureAwait(false);

            //Wait for events to propagate/tasks to finish
            await Task.Delay(100).ConfigureAwait(false);

            //assert
            Assert.Single(eventHandler.Events);
            Assert.Equal(evt.Id, eventHandler.Events[0].Id);
            Assert.Equal(evt.EventId, eventHandler.Events[0].EventId);
            Assert.Equal(evt.Message, eventHandler.Events[0].Message);
        }