Example #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);
        }
Example #2
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);
        }
Example #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);
        }
        public void TimerShouldGetMessagesFromAggregatorAndExecuteHandler()
        {
            // Arrange
            var mockPersistor = new Mock <IAggregatorPersistor>();
            var mockContainer = new Mock <IBusContainer>();
            var mockLogger    = new Mock <ILogger>();

            var timer = new AggregatorProcessor(mockPersistor.Object, mockContainer.Object, typeof(FakeAggregator), mockLogger.Object);

            mockPersistor.Setup(x => x.Count(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(1);
            var aggregator = new FakeAggregator();
            var message    = new FakeMessage1(Guid.NewGuid());

            mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(aggregator);
            mockPersistor.Setup(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(new List <object> {
                message
            });

            // Act
            timer.StartTimer <FakeMessage1>(new TimeSpan(0, 0, 0, 0, 50));
            Thread.Sleep(90);

            // Assert
            mockPersistor.Verify(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName), Times.Once);
            Assert.Equal(1, aggregator.Messages.Count);
            Assert.Equal(message, aggregator.Messages.First());
            timer.Dispose();
        }
        public void TimerShouldRemoveAllProcessedMessagesFromPersistor()
        {
            // Arrange
            var mockPersistor = new Mock <IAggregatorPersistor>();
            var mockContainer = new Mock <IBusContainer>();
            var mockLogger    = new Mock <ILogger>();

            var timer = new AggregatorProcessor(mockPersistor.Object, mockContainer.Object, typeof(FakeAggregator), mockLogger.Object);

            mockPersistor.Setup(x => x.Count(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(1);
            var aggregator = new FakeAggregator();
            var message    = new FakeMessage1(Guid.NewGuid());

            mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(aggregator);
            mockPersistor.Setup(x => x.GetData(typeof(FakeMessage1).AssemblyQualifiedName)).Returns(new List <object> {
                message
            });

            // Act
            timer.StartTimer <FakeMessage1>(new TimeSpan(0, 0, 0, 0, 50));
            Thread.Sleep(100);

            // Assert
            mockPersistor.Verify(x => x.RemoveData(typeof(FakeMessage1).AssemblyQualifiedName, message.CorrelationId), Times.Once);

            timer.Dispose();
        }
        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);
        }