Ejemplo n.º 1
0
        public void StartConsumingShouldConsumeAllMessageTypes()
        {
            // Arrange
            var bus = new ServiceConnect.Bus(_mockConfiguration.Object);

            var handlerReferences = new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeHandler1),
                    MessageType = typeof(FakeMessage1)
                },
                new HandlerReference
                {
                    HandlerType = typeof(FakeHandler2),
                    MessageType = typeof(FakeMessage2)
                }
            };

            _mockContainer.Setup(x => x.GetHandlerTypes()).Returns(handlerReferences);

            _mockConsumer.Setup(x => x.StartConsuming(It.IsAny <string>(), It.Is <IList <string> >(m => m.Contains(typeof(FakeMessage1).FullName.Replace(".", string.Empty)) && m.Contains(typeof(FakeMessage2).FullName.Replace(".", string.Empty))), It.IsAny <ConsumerEventHandler>(), It.IsAny <IConfiguration>()));

            // Act
            bus.StartConsuming();

            // Assert
            _mockContainer.VerifyAll();
            _mockConsumer.Verify(x => x.StartConsuming(It.IsAny <string>(), It.Is <IList <string> >(m => m.Contains(typeof(FakeMessage1).FullName.Replace(".", string.Empty)) && m.Contains(typeof(FakeMessage2).FullName.Replace(".", string.Empty))), It.IsAny <ConsumerEventHandler>(), It.IsAny <IConfiguration>()), Times.Once);
        }
Ejemplo n.º 2
0
        public void StartConsumingShouldGetAllHandlerTypesFromContainer()
        {
            // Arrange
            var bus = new ServiceConnect.Bus(_mockConfiguration.Object);

            _mockContainer.Setup(x => x.GetHandlerTypes()).Returns(new List <HandlerReference>());

            // Act
            bus.StartConsuming();

            // Assert
            // One time for handlers and one time for aggregators
            _mockContainer.Verify(x => x.GetHandlerTypes(), Times.Exactly(2));
            _mockContainer.VerifyAll();
        }
        public void ShouldSendMessageToAggregatorProcessor()
        {
            // Arrange

            var bus = new ServiceConnect.Bus(_mockConfiguration.Object);

            _mockConsumer.Setup(x => x.StartConsuming(It.IsAny <string>(), It.IsAny <IList <string> >(), It.Is <ConsumerEventHandler>(y => AssignEventHandler(y)), It.IsAny <IConfiguration>()));
            var mockPersistor  = new Mock <IAggregatorPersistor>();
            var mockProcessor  = new Mock <IAggregatorProcessor>();
            var fakeAggregator = new FakeAggregator();

            _mockConfiguration.Setup(x => x.GetAggregatorPersistor()).Returns(mockPersistor.Object);
            _mockConfiguration.Setup(x => x.Threads).Returns(1);
            _mockContainer.Setup(x => x.GetInstance <IMessageHandlerProcessor>(It.IsAny <Dictionary <string, object> >())).Returns(new Mock <IMessageHandlerProcessor>().Object);
            _mockContainer.Setup(x => x.GetInstance <IProcessManagerProcessor>(It.IsAny <Dictionary <string, object> >())).Returns(new Mock <IProcessManagerProcessor>().Object);
            _mockConfiguration.Setup(x => x.GetAggregatorProcessor(It.IsAny <IAggregatorPersistor>(), It.IsAny <IBusContainer>(), It.IsAny <Type>())).Returns(mockProcessor.Object);
            _mockContainer.Setup(x => x.GetHandlerTypes()).Returns(new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeAggregator),
                    MessageType = typeof(FakeMessage1)
                }
            });
            _mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(fakeAggregator);

            var message = new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            };

            mockProcessor.Setup(x => x.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(message)));

            bus.StartConsuming();

            // Act
            _fakeEventHandler(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)), typeof(FakeMessage1).AssemblyQualifiedName, new Dictionary <string, object>
            {
                { "MessageType", Encoding.UTF8.GetBytes("Send") }
            });

            // Assert
            mockProcessor.Verify(x => x.ProcessMessage <FakeMessage1>(It.Is <string>(y => JsonConvert.DeserializeObject <FakeMessage1>(y).Username == "Tim")), Times.Once);
        }
Ejemplo n.º 4
0
        public void CustomExceptionHandlerShouldBeCalledIfConsumeMessageEventThrows()
        {
            // Arrange
            bool actionCalled         = false;
            Action <Exception> action = exception => { actionCalled = true; };

            _mockConfiguration.Setup(x => x.ExceptionHandler).Returns(action);

            var bus = new ServiceConnect.Bus(_mockConfiguration.Object);

            var handlerReferences = new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeHandler1),
                    MessageType = typeof(FakeMessage1)
                },
                new HandlerReference
                {
                    HandlerType = typeof(FakeHandler2),
                    MessageType = typeof(FakeMessage2)
                }
            };

            var headers = new Dictionary <string, object>();

            _mockContainer.Setup(x => x.GetHandlerTypes()).Returns(handlerReferences);

            _mockConsumer.Setup(x => x.StartConsuming(It.IsAny <string>(), It.IsAny <IList <string> >(), It.Is <ConsumerEventHandler>(y => AssignEventHandler(y)), It.IsAny <IConfiguration>()));

            _mockProcessMessagePipeline.Setup(x => x.ExecutePipeline(It.IsAny <IConsumeContext>(), It.IsAny <Type>(), It.IsAny <Envelope>())).Throws(new Exception());

            bus.StartConsuming();

            // Act
            _fakeEventHandler(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            })), typeof(FakeMessage1).FullName, headers);

            // Assert
            Assert.True(actionCalled);
        }
Ejemplo n.º 5
0
        public void ConsumeMessageEventShouldExecuteMessageProcessingPipeline()
        {
            // Arrange
            var bus = new ServiceConnect.Bus(_mockConfiguration.Object);

            var handlerReferences = new List <HandlerReference>
            {
                new HandlerReference
                {
                    HandlerType = typeof(FakeHandler1),
                    MessageType = typeof(FakeMessage1)
                },
                new HandlerReference
                {
                    HandlerType = typeof(FakeHandler2),
                    MessageType = typeof(FakeMessage2)
                }
            };

            var headers = new Dictionary <string, object>
            {
                { "MessageType", Encoding.ASCII.GetBytes("Send") }
            };

            _mockContainer.Setup(x => x.GetHandlerTypes()).Returns(handlerReferences);
            _mockConsumer.Setup(x => x.StartConsuming(It.IsAny <string>(), It.IsAny <IList <string> >(), It.Is <ConsumerEventHandler>(y => AssignEventHandler(y)), It.IsAny <IConfiguration>()));

            bus.StartConsuming();

            var message = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            }));

            // Act
            _fakeEventHandler(message, typeof(FakeMessage1).AssemblyQualifiedName, headers);

            // Assert
            _mockProcessMessagePipeline.Verify(x => x.ExecutePipeline(It.Is <IConsumeContext>(y => y.Headers == headers), It.IsAny <Type>(), It.Is <Envelope>(y => ((FakeMessage1)JsonConvert.DeserializeObject(Encoding.UTF8.GetString(y.Body), typeof(FakeMessage1))).Username == "Tim Watson")), Times.Once);
        }