Exemplo n.º 1
0
        public void IfTimerIsNotSetAndBatchSizeIsNotSetThenBatchSizeIsSetTo10()
        {
            // Arrange
            var mockContainer           = new Mock <IBusContainer>();
            var mockAggregatorPersistor = new Mock <IAggregatorPersistor>();
            var mockLogger = new Mock <ILogger>();

            var handlerRef = new HandlerReference()
            {
                HandlerType = typeof(FakeAggregator),
                MessageType = typeof(FakeMessage1)
            };

            mockContainer.Setup(x => x.GetHandlerTypes(typeof(Aggregator <FakeMessage1>))).Returns(new List <HandlerReference> {
                handlerRef
            });
            var aggregator = new FakeAggregator();

            mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(aggregator);
            mockAggregatorPersistor.Setup(x => x.InsertData(It.IsAny <Message>(), It.IsAny <string>()));

            mockAggregatorPersistor.Setup(x => x.Count(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(10);
            mockAggregatorPersistor.Setup(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(new List <object>());

            var aggregatorProcessor = new AggregatorProcessor(mockAggregatorPersistor.Object, mockContainer.Object, typeof(FakeAggregator), mockLogger.Object);

            // Act
            aggregatorProcessor.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())));

            // Assert
            // Only calls this if batchsize is equal to number of messages in persistance store.
            mockAggregatorPersistor.Verify(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName), Times.Once);
        }
Exemplo n.º 2
0
        public void ShouldFindTheAggregatorForTheMessageType()
        {
            // Arrange
            var mockContainer           = new Mock <IBusContainer>();
            var mockAggregatorPersistor = new Mock <IAggregatorPersistor>();
            var mockLogger = new Mock <ILogger>();

            var handlerRef = new HandlerReference()
            {
                HandlerType = typeof(FakeAggregator),
                MessageType = typeof(FakeMessage1)
            };

            mockContainer.Setup(x => x.GetHandlerTypes(typeof(Aggregator <FakeMessage1>))).Returns(new List <HandlerReference> {
                handlerRef
            });
            mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(new FakeAggregator
            {
                Time = new TimeSpan(0, 0, 0, 1)
            });
            mockAggregatorPersistor.Setup(x => x.InsertData(It.IsAny <Message>(), It.IsAny <string>()));


            var aggregator = new AggregatorProcessor(mockAggregatorPersistor.Object, mockContainer.Object, typeof(FakeAggregator), mockLogger.Object);

            // Act
            aggregator.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())));

            // Assert
            mockContainer.Verify(x => x.GetInstance(typeof(FakeAggregator)), Times.Once());
        }
Exemplo n.º 3
0
        public void ShouldNotExecuteAggregatorIfBatchSizeHasntBeenSet()
        {
            // Arrange
            var mockContainer           = new Mock <IBusContainer>();
            var mockAggregatorPersistor = new Mock <IAggregatorPersistor>();
            var mockLogger = new Mock <ILogger>();

            var handlerRef = new HandlerReference()
            {
                HandlerType = typeof(FakeAggregator),
                MessageType = typeof(FakeMessage1)
            };

            mockContainer.Setup(x => x.GetHandlerTypes(typeof(Aggregator <FakeMessage1>))).Returns(new List <HandlerReference> {
                handlerRef
            });
            var fakeAggregator = new FakeAggregator
            {
                Time = new TimeSpan(0, 0, 0, 1)
            };

            mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(fakeAggregator);
            mockAggregatorPersistor.Setup(x => x.InsertData(It.IsAny <Message>(), It.IsAny <string>()));


            var aggregator = new AggregatorProcessor(mockAggregatorPersistor.Object, mockContainer.Object, typeof(FakeAggregator), mockLogger.Object);

            // Act
            aggregator.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())));

            // Assert
            mockAggregatorPersistor.Verify(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName), Times.Never);
            Assert.False(fakeAggregator.Executed);
        }
Exemplo n.º 4
0
        public void ShouldUseBatchSizeFromAggregator()
        {
            // Arrange
            var mockContainer           = new Mock <IBusContainer>();
            var mockAggregatorPersistor = new Mock <IAggregatorPersistor>();
            var mockLogger = new Mock <ILogger>();

            var handlerRef = new HandlerReference()
            {
                HandlerType = typeof(FakeAggregator),
                MessageType = typeof(FakeMessage1)
            };

            mockContainer.Setup(x => x.GetHandlerTypes(typeof(Aggregator <FakeMessage1>))).Returns(new List <HandlerReference> {
                handlerRef
            });
            var aggregator = new FakeAggregator
            {
                Batch = 20
            };

            mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(aggregator);
            mockAggregatorPersistor.Setup(x => x.InsertData(It.IsAny <Message>(), It.IsAny <string>()));

            mockAggregatorPersistor.Setup(x => x.Count(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(20);
            mockAggregatorPersistor.Setup(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(new List <object>());

            var aggregatorProcessor = new AggregatorProcessor(mockAggregatorPersistor.Object, mockContainer.Object, typeof(FakeAggregator), mockLogger.Object);

            // Act
            aggregatorProcessor.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())));

            // Assert
            mockAggregatorPersistor.Verify(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName), Times.Once);
        }
        public void ShouldExecuteHandlerWithMessagesIfMessageCountIsEqualToOrGreaterThanBatchSize()
        {
            // Arrange
            var mockContainer           = new Mock <IBusContainer>();
            var mockAggregatorPersistor = new Mock <IAggregatorPersistor>();

            var handlerRef = new HandlerReference()
            {
                HandlerType = typeof(FakeAggregator),
                MessageType = typeof(FakeMessage1)
            };

            mockContainer.Setup(x => x.GetHandlerTypes(typeof(Aggregator <FakeMessage1>))).Returns(new List <HandlerReference> {
                handlerRef
            });
            var aggregator = new FakeAggregator();

            mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(aggregator);
            mockAggregatorPersistor.Setup(x => x.InsertData(It.IsAny <object>(), It.IsAny <string>()));

            mockAggregatorPersistor.Setup(x => x.Count(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(10);
            mockAggregatorPersistor.Setup(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(new List <object>()
            {
                new FakeMessage1(Guid.NewGuid())
            });

            var aggregatorProcessor = new AggregatorProcessor(mockAggregatorPersistor.Object, mockContainer.Object, typeof(FakeAggregator));

            // Act
            aggregatorProcessor.ProcessMessage <FakeMessage1>(JsonConvert.SerializeObject(new FakeMessage1(Guid.NewGuid())));

            // Assert
            mockAggregatorPersistor.Verify(x => x.RemoveData(typeof(FakeMessage1).AssemblyQualifiedName, It.IsAny <Guid>()), Times.Once);
        }