// Application starting point
        public void Run(bool useSenderServiceKeepConnection)
        {
            _useSenderServiceKeepConnection = useSenderServiceKeepConnection;

            _senderServiceKeepConnection.ServiceDescription = $"service to send message - keep connection created at {DateTime.Now}";
            _senderService.ServiceDescription = "service to send messages";

            var retryQueueDetails = new RetryQueueDetails
            {
                RetentionPeriodInRetryQueueMilliseconds    = _config.BrokerRetentionPeriodInRetryQueueSeconds * 1000,
                RetentionPeriodInRetryQueueMillisecondsMax = _config.BrokerRetentionPeriodInRetryQueueSecondsMax * 1000,
                RetryQueue = _config.BrokerRetryQueue
            };



            _consumerService.StartConsumers("consumers_Tag_A", retryQueueDetails, totalConsumers: 2);
            _consumerService.StartConsumers("consumers_Tag_B", retryQueueDetails, totalConsumers: 2);

            _logger.LogInformation($"Starting consumers...");

            var details = _consumerService.GetConsumerDetails();

            if (details != null)
            {
                foreach (var consumerInfo in details)
                {
                    _logger.LogInformation($"consumer '{consumerInfo.Name}' connected to {consumerInfo.ConnectedToHost}:{consumerInfo.ConnectedToPort} started at {consumerInfo.ConnectionTime:yyyy-MM-dd HH:mm:ss}");
                }
            }
        }
        public void ConsumerService_StartConsumers_InvalidHost_ThrowsException(string invalidHost)
        {
            IBrokerConfigConsumers config = new Entities.BrokerConfigConsumers
            {
                Host = invalidHost,
            };

            _consumerService = GetConsumerService(config);

            Assert.Throws <ArgumentException>(() => _consumerService.StartConsumers("test", null, TotalConsumers));
        }
        public void ConsumerService_ProcessErrors_executesAllErrorDelegates()
        {
            _consumerService = GetConsumerService();

            _consumerService.StartConsumers("test", null, TotalConsumers);

            _rabbitMqHelper.SendMessage(queueName, "error");
            _rabbitMqHelper.SendMessage(queueName, "error");

            Thread.Sleep(100);

            Assert.AreEqual(TotalConsumers, _totalErrors);
        }
        public void ConsumerService_StartConsumers_NamesAreCorrect()
        {
            _consumerService = GetConsumerService();

            _consumerService.StartConsumers("test", null, 2);

            Thread.Sleep(100);

            var startedConsumers = _consumerService.GetConsumerDetails();

            Assert.IsTrue(startedConsumers.Any(y => y.Name == "test_0"));
            Assert.IsTrue(startedConsumers.Any(y => y.Name == "test_1"));
        }
Exemple #5
0
        /// <summary>
        /// Service start - executed when the service starts
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>

        public override Task StartAsync(CancellationToken stoppingToken)
        {
            _rabbitConsumerService.StartConsumers("Service consumer");

            _logger.LogInformation($"Starting consumers...");

            var details = _rabbitConsumerService.GetConsumerDetails();

            if (details != null)
            {
                foreach (var consumerInfo in details)
                {
                    _logger.LogInformation($"consumer '{consumerInfo.Name}' started at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                }
            }
            return(base.StartAsync(stoppingToken));
        }
        public void ConsumerService_StopConsumers_oneConsumer_consumerStopConsuming()
        {
            _consumerService = GetConsumerService();

            _consumerService.StartConsumers("test", null, TotalConsumers);

            Thread.Sleep(100);

            Assert.AreEqual(TotalConsumers, _consumerService.GetTotalRunningConsumers);

            _consumerService.StopConsumers("test_0");

            Thread.Sleep(200);

            Assert.AreEqual(1, _consumerService.GetTotalRunningConsumers);
            Assert.AreEqual(TotalConsumers, _consumerService.GetTotalConsumers);
        }
        public void ConsumerService_Dispose_executesAllDisposeDelegates()
        {
            _consumerService = GetConsumerService();

            _consumerService.StartConsumers("test", null, TotalConsumers);

            _rabbitMqHelper.SendMessage(queueName, "message 1");
            _rabbitMqHelper.SendMessage(queueName, "message 2");

            Thread.Sleep(100);

            _consumerService.Dispose();

            Thread.Sleep(2500);

            Assert.AreEqual(TotalConsumers, _totalStopProcessing, $"_totalStopProcessing: {_totalStopProcessing}");
        }
        public void ConsumerService_ReceiveMessage_CreateQueue_queueDoesnt_existYet_receivesAllMessages()
        {
            _consumerService = GetConsumerService();

            _consumerService.StartConsumers("test", otherQueueName, TotalConsumers, true);

            _rabbitMqHelper.SendMessage(otherQueueName, "message 1");
            _rabbitMqHelper.SendMessage(otherQueueName, "message 2");

            Thread.Sleep(100);

            var res = _rabbitMqHelper.CreateQueueOrGetInfo(otherQueueName);
            var totalMessagesInQueue = res.MessageCount;

            Assert.AreEqual(2, _totalMessagesReceived);
            Assert.AreEqual(0, totalMessagesInQueue);
        }
        public void ConsumerService_ProcessMessage_messageIsReceived(string messageType,
                                                                     int expectedMessageInQueueAfterProcessing)
        {
            _consumerService = GetConsumerService();

            _rabbitMqHelper.SendMessage(queueName, messageType);

            _consumerService.StartConsumers("test", null, TotalConsumers);

            Thread.Sleep(100);

            _consumerService.Dispose();

            var res = _rabbitMqHelper.CreateQueueOrGetInfo(queueName);
            var totalMessagesInQueue = res.MessageCount;

            Assert.AreEqual(expectedMessageInQueueAfterProcessing, totalMessagesInQueue);
        }
        public void ConsumerService_StartConsumers_ValidNumberOfConsumers_useDefaultConsumers_returnsOk()
        {
            _consumerService = GetConsumerService();

            _consumerService.StartConsumers("test");

            Thread.Sleep(300);

            var startedConsumers = _consumerService.GetTotalRunningConsumers;
            var totalConsumers   = _consumerService.GetTotalConsumers;

            var res           = _rabbitMqHelper.CreateQueueOrGetInfo(queueName);
            var consumerCount = res.ConsumerCount;

            Assert.AreEqual(DefaultTotalConsumers, startedConsumers);
            Assert.AreEqual(DefaultTotalConsumers, totalConsumers);
            Assert.AreEqual(DefaultTotalConsumers, consumerCount);
        }
        public void ConsumerService_ProcessMessage_WithLimiter_limiterIsCalled()
        {
            const string messageType   = "ok";
            const int    expectedCalls = 1;

            var limiter = new LimiterHelper(Constants.MessageProcessInstruction.OK);

            _consumerService = GetConsumerService();

            _consumerService.Limiter = limiter;

            _rabbitMqHelper.SendMessage(queueName, messageType);

            _consumerService.StartConsumers("test", null, TotalConsumers);

            Thread.Sleep(100);

            _consumerService.Dispose();

            Assert.AreEqual(expectedCalls, limiter.TotalCalls);
        }
        public void ConsumerService_StopConsumers_AllConsumersStopConsuming()
        {
            _consumerService = GetConsumerService();

            _consumerService.StartConsumers("test", null, TotalConsumers);

            Thread.Sleep(100);

            Assert.AreEqual(TotalConsumers, _consumerService.GetTotalRunningConsumers);

            Thread.Sleep(100);

            _consumerService.StopConsumers();

            Thread.Sleep(100);

            var running = _consumerService.GetTotalRunningConsumers;
            var total   = _consumerService.GetTotalConsumers;

            Assert.AreEqual(0, running);
            Assert.AreEqual(TotalConsumers, total);
        }
        public void ConsumerService_StartConsumers_InValidNumberOfConsumers_ThrowsException()
        {
            _consumerService = GetConsumerService();

            Assert.Throws <ArgumentException>(() => _consumerService.StartConsumers("test", null, 0));
        }