public void RunConsumer <TTransportInit>(string queueName, string connectionString, bool addInterceptors,
                                                 ILogProvider logProvider,
                                                 int runTime, int messageCount,
                                                 int workerCount, int timeOut,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, string updateTime,
                                                 string route, bool enableChaos)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }
                var processedCount = new IncrementWrapper();
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos)
                    )
                {
                    using (
                        var queue =
                            creator.CreateConsumer(queueName,
                                                   connectionString))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, route);
                        queue.Configuration.MessageExpiration.Enabled     = true;
                        queue.Configuration.MessageExpiration.MonitorTime = TimeSpan.FromSeconds(8);
                        var waitForFinish = new ManualResetEventSlim(false);
                        waitForFinish.Reset();
                        //start looking for work
                        queue.Start <TMessage>((message, notifications) =>
                        {
                            MessageHandlingShared.HandleFakeMessages <TMessage>(null, runTime, processedCount, messageCount,
                                                                                waitForFinish);
                        });

                        for (var i = 0; i < timeOut; i++)
                        {
                            if (VerifyMetrics.GetExpiredMessageCount(metrics.GetCurrentMetrics()) == messageCount)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                        }
                    }

                    Assert.Equal(0, processedCount.ProcessedCount);
                    VerifyMetrics.VerifyProcessedCount(queueName, metrics.GetCurrentMetrics(), 0);
                    VerifyMetrics.VerifyExpiredMessageCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                    LoggerShared.CheckForErrors(queueName);
                }
            }
        }
        public void RunConsumer <TTransportInit>(string queueName,
                                                 string connectionString,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogProvider logProvider,
                                                 int timeOut,
                                                 int runTime,
                                                 long messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 Guid id, bool enableChaos)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos)
                    )
                {
                    using (
                        var queue =
                            creator.CreateMethodConsumer(queueName,
                                                         connectionString))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);
                        queue.Start();
                        var counter = 0;
                        while (counter < timeOut)
                        {
                            if (MethodIncrementWrapper.Count(id) >= messageCount)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                            counter++;
                        }

                        //wait for queues to commit records
                        Thread.Sleep(3000);
                    }
                    Assert.Equal(messageCount, MethodIncrementWrapper.Count(id));
                    VerifyMetrics.VerifyProcessedCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                    VerifyMetrics.VerifyRollBackCount(queueName, metrics.GetCurrentMetrics(), messageCount, 1, 0);
                    LoggerShared.CheckForErrors(queueName);
                }
            }
        }
Esempio n. 3
0
        public void RunConsumer <TTransportInit>(string queueName, string connectionString, bool addInterceptors,
                                                 ILogProvider logProvider,
                                                 int workerCount, int timeOut, int messageCount,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, string updateTime, string route, bool enableChaos)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                var processedCount = new IncrementWrapper();
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                             metrics, false, enableChaos)
                    )
                {
                    bool rollBacks;
                    using (
                        var queue =
                            creator.CreateConsumer(queueName,
                                                   connectionString))
                    {
                        rollBacks = queue.Configuration.TransportConfiguration.MessageRollbackSupported;

                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, route);
                        SharedSetup.SetupDefaultErrorRetry(queue.Configuration);

                        var waitForFinish = new ManualResetEventSlim(false);
                        waitForFinish.Reset();

                        //start looking for work
                        queue.Start <TMessage>((message, notifications) =>
                        {
                            MessageHandlingShared.HandleFakeMessagesError(processedCount, waitForFinish,
                                                                          messageCount, message);
                        });

                        waitForFinish.Wait(timeOut * 1000);

                        //wait 3 more seconds before starting to shutdown
                        Thread.Sleep(3000);
                    }

                    if (rollBacks)
                    {
                        VerifyMetrics.VerifyRollBackCount(queueName, metrics.GetCurrentMetrics(), messageCount, 2, 2);
                    }
                }
            }
        }
Esempio n. 4
0
        private void RunConsumerInternal(QueueConnection queueConnection, bool addInterceptors,
                                         ILogger logProvider,
                                         int runTime, int messageCount,
                                         int workerCount, int timeOut, IDisposable queueBad,
                                         TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, Guid id, string updateTime, bool enableChaos, ICreationScope scope)
        {
            using (var trace = SharedSetup.CreateTrace("consumer-cancel"))
            {
                using (var metrics = new Metrics.Metrics(queueConnection.Queue))
                {
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics, false, enableChaos, scope, trace.Source)
                        )
                    {
                        using (
                            var queue =
                                creator.CreateMethodConsumer(queueConnection, x => x.RegisterNonScopedSingleton(scope).RegisterNonScopedSingleton(trace.Source)))
                        {
                            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                                  heartBeatMonitorTime, updateTime, null);
                            queue.Start();

                            var time = runTime * 1000 / 2;
                            Thread.Sleep(time);
                            queueBad.Dispose();
                            _badQueueContainer.Dispose();

                            var counter     = 0;
                            var counterLess = timeOut / 2;
                            while (counter < counterLess)
                            {
                                if (MethodIncrementWrapper.Count(id) >= messageCount)
                                {
                                    break;
                                }

                                Thread.Sleep(1000);
                                counter++;
                            }

                            //wait for commits in transport...
                            Thread.Sleep(3000);
                        }

                        var count = MethodIncrementWrapper.Count(id);
                        Assert.Equal(messageCount, count);
                        VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                           messageCount);
                        LoggerShared.CheckForErrors(queueConnection.Queue);
                    }
                }
            }
        }
Esempio n. 5
0
        public void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogger logProvider,
                                                 int timeOut,
                                                 long messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var trace = SharedSetup.CreateTrace("consumer-poison"))
            {
                using (var metrics = new Metrics.Metrics(queueConnection.Queue))
                {
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics,
                                                                                 true, enableChaos, scope, trace.Source))
                    {
                        using (
                            var queue =
                                creator.CreateMethodConsumer(queueConnection, x => x.RegisterNonScopedSingleton(scope).RegisterNonScopedSingleton(trace.Source)))
                        {
                            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                                  heartBeatMonitorTime, updateTime, null);

                            var waitForFinish = new ManualResetEventSlim(false);
                            waitForFinish.Reset();
                            queue.Start();
                            for (var i = 0; i < timeOut; i++)
                            {
                                if (VerifyMetrics.GetPoisonMessageCount(metrics.GetCurrentMetrics()) == messageCount)
                                {
                                    break;
                                }

                                Thread.Sleep(1000);
                            }
                        }

                        VerifyMetrics.VerifyPoisonMessageCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                               messageCount);
                    }
                }
            }
        }
Esempio n. 6
0
        public void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogger logProvider,
                                                 int timeOut,
                                                 int runTime,
                                                 long messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 string route, bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueConnection.Queue))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                var processedCount          = new IncrementWrapper();
                var haveIProcessedYouBefore = new ConcurrentDictionary <string, int>();
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos, scope)
                    )
                {
                    using (
                        var queue =
                            creator.CreateConsumer(queueConnection))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, route);
                        var waitForFinish = new ManualResetEventSlim(false);
                        waitForFinish.Reset();

                        //start looking for work
                        queue.Start <TMessage>((message, notifications) =>
                        {
                            MessageHandlingShared.HandleFakeMessagesRollback(message, runTime, processedCount,
                                                                             messageCount,
                                                                             waitForFinish, haveIProcessedYouBefore);
                        });

                        waitForFinish.Wait(timeOut * 1000);
                    }
                    Assert.Equal(messageCount, processedCount.ProcessedCount);
                    VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(), messageCount);
                    VerifyMetrics.VerifyRollBackCount(queueConnection.Queue, metrics.GetCurrentMetrics(), messageCount, 1, 0);
                    LoggerShared.CheckForErrors(queueConnection.Queue);
                    haveIProcessedYouBefore.Clear();
                }
            }
        }
        public void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogger logProvider,
                                                 int timeOut,
                                                 long messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 string route,
                                                 bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueConnection.Queue))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics,
                                                                             true, enableChaos, scope))
                {
                    using (
                        var queue =
                            creator.CreateConsumer(queueConnection))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, route);

                        var waitForFinish = new ManualResetEventSlim(false);
                        waitForFinish.Reset();

                        //start looking for work
                        queue.Start <TMessage>((message, notifications) =>
                        {
                            MessageHandlingShared.HandleFakeMessageNoOp();
                        });

                        for (var i = 0; i < timeOut; i++)
                        {
                            if (VerifyMetrics.GetPoisonMessageCount(metrics.GetCurrentMetrics()) == messageCount)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                        }
                    }
                    VerifyMetrics.VerifyPoisonMessageCount(queueConnection.Queue, metrics.GetCurrentMetrics(), messageCount);
                }
            }
        }
        void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                          bool addInterceptors,
                                          ILogger logProvider,
                                          int runTime,
                                          int messageCount,
                                          int timeOut,
                                          int readerCount,
                                          TimeSpan heartBeatTime,
                                          TimeSpan heartBeatMonitorTime,
                                          Guid id,
                                          string updateTime,
                                          bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueConnection.Queue))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos, scope)
                    )
                {
                    using (
                        var queue =
                            creator
                            .CreateConsumerMethodQueueScheduler(
                                queueConnection, Factory))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);
                        queue.Start();
                        var counter = 0;
                        while (counter < timeOut)
                        {
                            if (MethodIncrementWrapper.Count(id) >= messageCount)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                            counter++;
                        }
                    }

                    Assert.Equal(messageCount, MethodIncrementWrapper.Count(id));
                    VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(), messageCount);
                    LoggerShared.CheckForErrors(queueConnection.Queue);
                }
            }
        }
        public void RunConsumer <TTransportInit>(QueueConnection queueConnection, bool addInterceptors,
                                                 ILogger logProvider,
                                                 int runTime, int messageCount,
                                                 int workerCount, int timeOut,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 Guid id, bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            using (var trace = SharedSetup.CreateTrace("consumer-expired"))
            {
                if (enableChaos)
                {
                    timeOut *= 2;
                }

                using (var metrics = new Metrics.Metrics(queueConnection.Queue))
                {
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics, false, enableChaos, scope, trace.Source)
                        )
                    {
                        using (
                            var queue =
                                creator.CreateMethodConsumer(queueConnection, x => x.RegisterNonScopedSingleton(scope).RegisterNonScopedSingleton(trace.Source)))
                        {
                            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                                  heartBeatMonitorTime, updateTime, null);
                            queue.Configuration.MessageExpiration.Enabled     = true;
                            queue.Configuration.MessageExpiration.MonitorTime = TimeSpan.FromSeconds(8);
                            queue.Start();
                            for (var i = 0; i < timeOut; i++)
                            {
                                if (VerifyMetrics.GetExpiredMessageCount(metrics.GetCurrentMetrics()) == messageCount)
                                {
                                    break;
                                }

                                Thread.Sleep(1000);
                            }
                        }

                        Assert.Equal(0, MethodIncrementWrapper.Count(id));
                        VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(), 0);
                        VerifyMetrics.VerifyExpiredMessageCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                                messageCount);
                        LoggerShared.CheckForErrors(queueConnection.Queue);
                    }
                }
            }
        }
        public void RunConsumer <TTransportInit>(string queueName, string connectionString, bool addInterceptors,
                                                 ILogProvider logProvider,
                                                 int workerCount, int timeOut, int messageCount,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, Guid id, string updateTime, bool enableChaos)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos)
                    )
                {
                    bool rollbacks;
                    using (
                        var queue =
                            creator.CreateMethodConsumer(queueName,
                                                         connectionString))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);
                        SharedSetup.SetupDefaultErrorRetry(queue.Configuration);
                        rollbacks = queue.Configuration.TransportConfiguration.MessageRollbackSupported;
                        queue.Start();

                        var counter = 0;
                        while (counter < timeOut)
                        {
                            if (MethodIncrementWrapper.Count(id) >= messageCount * 3)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                            counter++;
                        }

                        //wait 3 more seconds before starting to shutdown
                        Thread.Sleep(3000);
                    }

                    if (rollbacks)
                    {
                        VerifyMetrics.VerifyRollBackCount(queueName, metrics.GetCurrentMetrics(), messageCount, 2, 2);
                    }
                }
            }
        }
Esempio n. 11
0
        private IConsumerMethodQueue CreateConsumerInternalThread(ICreationScope scope)
        {
            _badQueueContainer = SharedSetup.CreateCreator <TTransportInit>(_badQueueAdditions);

            var queue =
                _badQueueContainer.CreateMethodConsumer(_queueConnection, x => x.RegisterNonScopedSingleton(scope));

            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, _workerCount, _heartBeatTime, _heartBeatMonitorTime, _updatetime, null);
            return(queue);
        }
        private void RunConsumerInternal(QueueConnection queueConnection, bool addInterceptors,
                                         ILogger logProvider,
                                         int runTime, int messageCount,
                                         int workerCount, int timeOut, IDisposable queueBad,
                                         TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, string updateTime, string route, bool enableChaos, ICreationScope scope)
        {
            using (var trace = SharedSetup.CreateTrace("consumer-cancel"))
            {
                using (var metrics = new Metrics.Metrics(queueConnection.Queue))
                {
                    var processedCount         = new IncrementWrapper();
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics, false, enableChaos, scope, trace.Source)
                        )
                    {
                        using (
                            var queue =
                                creator.CreateConsumer(queueConnection))
                        {
                            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                                  heartBeatMonitorTime, updateTime, route);
                            var waitForFinish = new ManualResetEventSlim(false);
                            waitForFinish.Reset();
                            //start looking for work
                            queue.Start <TMessage>((message, notifications) =>
                            {
                                MessageHandlingShared.HandleFakeMessages <TMessage>(null, runTime, processedCount,
                                                                                    messageCount,
                                                                                    waitForFinish);
                            });

                            var time = runTime * 1000 / 2;
                            waitForFinish.Wait(time);

                            queueBad.Dispose();
                            _badQueueContainer.Dispose();

                            waitForFinish.Wait(timeOut * 1000 - time);
                        }

                        Assert.Equal(messageCount, processedCount.ProcessedCount);
                        VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                           messageCount);
                        LoggerShared.CheckForErrors(queueConnection.Queue);
                    }
                }
            }
        }
        private IConsumerMethodQueue CreateConsumerInternalThread()
        {
            _badQueueContainer = SharedSetup.CreateCreator <TTransportInit>(_badQueueAdditions);

            var queue =
                _badQueueContainer.CreateMethodConsumer(_queueName,
                                                        _connectionString);

            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, _workerCount, _heartBeatTime, _heartBeatMonitorTime, _updatetime, null);
            return(queue);
        }
Esempio n. 14
0
        private IConsumerQueue CreateConsumerInternalThread(string updateTime, string route)
        {
            _badQueueContainer = SharedSetup.CreateCreator <TTransportInit>(_badQueueAdditions);

            var queue =
                _badQueueContainer.CreateConsumer(_queueConnection);

            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, _workerCount, _heartBeatTime,
                                                  _heartBeatMonitorTime, updateTime, route);
            return(queue);
        }
Esempio n. 15
0
        public void RunConsumer <TTransportInit>(string queueName,
                                                 string connectionString,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogProvider logProvider,
                                                 int timeOut,
                                                 long messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updateTime)
            where TTransportInit : ITransportInit, new()
        {
            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics,
                                                                             true))
                {
                    using (
                        var queue =
                            creator.CreateMethodConsumer(queueName,
                                                         connectionString))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);

                        var waitForFinish = new ManualResetEventSlim(false);
                        waitForFinish.Reset();
                        queue.Start();
                        for (var i = 0; i < timeOut; i++)
                        {
                            if (VerifyMetrics.GetPoisonMessageCount(metrics.GetCurrentMetrics()) == messageCount)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                        }
                    }
                    VerifyMetrics.VerifyPoisonMessageCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                }
            }
        }
Esempio n. 16
0
        public void RunConsumer <TTransportInit>(string queueName, string connectionString, bool addInterceptors,
                                                 ILogProvider logProvider,
                                                 int runTime, int messageCount,
                                                 int workerCount, int timeOut,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, string updateTime)
            where TTransportInit : ITransportInit, new()
        {
            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                var processedCount = new IncrementWrapper();
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics)
                    )
                {
                    using (
                        var queue =
                            creator.CreateConsumer(queueName,
                                                   connectionString))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);
                        var waitForFinish = new ManualResetEventSlim(false);
                        waitForFinish.Reset();
                        //start looking for work
                        queue.Start <TMessage>((message, notifications) =>
                        {
                            MessageHandlingShared.HandleFakeMessages(message, runTime, processedCount, messageCount,
                                                                     waitForFinish);
                        });

                        waitForFinish.Wait(timeOut * 1000);
                    }

                    Assert.Null(processedCount.IdError);
                    Assert.Equal(messageCount, processedCount.ProcessedCount);
                    VerifyMetrics.VerifyProcessedCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                    LoggerShared.CheckForErrors(queueName);
                }
            }
        }
Esempio n. 17
0
        private void RunRpcReceive(string queueName, string connectionString,
                                   ILogProvider logProvider,
                                   // ReSharper disable once UnusedParameter.Local
                                   int runTime, int messageCount,
                                   int workerCount, int timeOut,
                                   TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, string updateTime, Guid id)
        {
            using (var metrics = new Metrics.Metrics(queueName))
            {
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(InterceptorAdding.Yes, logProvider, metrics)
                    )
                {
                    using (
                        var queue =
                            creator.CreateMethodConsumer(queueName, connectionString))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);
                        queue.Configuration.TransportConfiguration.QueueDelayBehavior.Clear();
                        queue.Configuration.TransportConfiguration.QueueDelayBehavior.Add(TimeSpan.FromMilliseconds(100));
                        queue.Configuration.Worker.SingleWorkerWhenNoWorkFound = false;
                        queue.Start();
                        var counter = 0;
                        while (counter < timeOut)
                        {
                            if (MethodIncrementWrapper.Count(id) >= messageCount)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                            counter++;
                        }
                    }

                    Assert.Equal(messageCount, MethodIncrementWrapper.Count(id));
                    VerifyMetrics.VerifyProcessedCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                }
            }
        }
Esempio n. 18
0
        private void RunRpcReceive(string queueName, string connectionString,
                                   ILogProvider logProvider,
                                   int runTime, IncrementWrapper processedCount, int messageCount, ManualResetEventSlim waitForFinish,
                                   int workerCount, int timeOut,
                                   TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime, string updateTime)
        {
            using (var metrics = new Metrics.Metrics(queueName))
            {
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(InterceptorAdding.Yes, logProvider, metrics)
                    )
                {
                    using (
                        var queue =
                            creator.CreateConsumer(queueName, connectionString))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, workerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);
                        queue.Configuration.TransportConfiguration.QueueDelayBehavior.Clear();
                        queue.Configuration.TransportConfiguration.QueueDelayBehavior.Add(TimeSpan.FromMilliseconds(100));
                        queue.Configuration.Worker.SingleWorkerWhenNoWorkFound = false;

                        waitForFinish.Reset();

                        //start looking for work
                        queue.Start <TTMessage>((message, notifications) =>
                        {
                            HandleFakeMessages(message, notifications, runTime, processedCount, messageCount,
                                               waitForFinish);
                        });

                        waitForFinish.Wait(timeOut * 1000);
                    }

                    Assert.Equal(messageCount, processedCount.ProcessedCount);
                    VerifyMetrics.VerifyProcessedCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                }
            }
        }
Esempio n. 19
0
        public void RunConsumer <TTransportInit>(string queueName, string connectionString, bool addInterceptors,
                                                 ILogProvider logProvider,
                                                 int messageCount, int workerCount, int timeOut,
                                                 int queueSize, int readerCount,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime,
                                                 Guid id, string updateTime, bool enableChaos)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos)
                    )
                {
                    using (var schedulerCreator =
                               new SchedulerContainer(
                                   // ReSharper disable once AccessToDisposedClosure
                                   serviceRegister => serviceRegister.Register(() => metrics, LifeStyles.Singleton)))
                    {
                        bool rollback;
                        using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                        {
                            taskScheduler.Configuration.MaximumThreads = workerCount;
                            taskScheduler.Configuration.MaxQueueSize   = queueSize;

                            taskScheduler.Start();
                            var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                            using (
                                var queue =
                                    creator
                                    .CreateConsumerMethodQueueScheduler(
                                        queueName, connectionString, taskFactory))
                            {
                                SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                                      heartBeatMonitorTime, updateTime, null);
                                SharedSetup.SetupDefaultErrorRetry(queue.Configuration);
                                rollback = queue.Configuration.TransportConfiguration.MessageRollbackSupported;
                                queue.Start();
                                var counter = 0;
                                while (counter < timeOut)
                                {
                                    if (MethodIncrementWrapper.Count(id) >= messageCount * 3)
                                    {
                                        break;
                                    }
                                    Thread.Sleep(1000);
                                    counter++;
                                }

                                //wait 3 more seconds before starting to shutdown
                                Thread.Sleep(3000);
                            }
                        }
                        if (rollback)
                        {
                            VerifyMetrics.VerifyRollBackCount(queueName, metrics.GetCurrentMetrics(), messageCount, 2, 2);
                        }
                    }
                }
            }
        }
Esempio n. 20
0
        public void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogger logProvider,
                                                 int timeOut,
                                                 int readerCount,
                                                 int queueSize,
                                                 long messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 string route,
                                                 bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueConnection.Queue))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics,
                                                                             true, enableChaos, scope))
                {
                    using (var schedulerCreator = new SchedulerContainer())
                    {
                        using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                        {
                            taskScheduler.Configuration.MaximumThreads = workerCount;
                            taskScheduler.Configuration.MaxQueueSize   = queueSize;

                            taskScheduler.Start();
                            var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                            using (
                                var queue =
                                    creator
                                    .CreateConsumerQueueScheduler(
                                        queueConnection, taskFactory))
                            {
                                SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                                      heartBeatMonitorTime, updateTime, route);

                                //start looking for work
                                queue.Start <TMessage>((message, notifications) =>
                                {
                                    MessageHandlingShared.HandleFakeMessageNoOp();
                                });

                                for (var i = 0; i < timeOut; i++)
                                {
                                    if (VerifyMetrics.GetPoisonMessageCount(metrics.GetCurrentMetrics()) == messageCount)
                                    {
                                        break;
                                    }
                                    Thread.Sleep(1000);
                                }

                                //wait for last error to be saved if needed.
                                Thread.Sleep(3000);
                            }
                        }
                        VerifyMetrics.VerifyPoisonMessageCount(queueConnection.Queue, metrics.GetCurrentMetrics(), messageCount);
                    }
                }
            }
        }
        void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                          bool addInterceptors,
                                          ILogger logProvider,
                                          int runTime,
                                          int messageCount,
                                          int timeOut,
                                          int readerCount,
                                          TimeSpan heartBeatTime,
                                          TimeSpan heartBeatMonitorTime,
                                          string updateTime,
                                          bool enableChaos,
                                          ICreationScope scope,
                                          string route,
                                          Action <QueueConsumerConfiguration> setQueueOptions = null)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            var metricName = queueConnection.Queue;

            if (!string.IsNullOrEmpty(route))
            {
                metricName = queueConnection.Queue + "|-|" + route;
            }

            using (var trace = SharedSetup.CreateTrace("consumer"))
            {
                using (var metrics = new Metrics.Metrics(metricName))
                {
                    var processedCount         = new IncrementWrapper();
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics, false, enableChaos, scope, trace.Source)
                        )
                    {
                        using (
                            var queue =
                                creator
                                .CreateConsumerQueueScheduler(
                                    queueConnection, Factory))
                        {
                            SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                                  heartBeatMonitorTime, updateTime, route);

                            if (!string.IsNullOrWhiteSpace(route))
                            {
                                queue.Configuration.Routes.Add(route);
                            }

                            setQueueOptions?.Invoke(queue.Configuration);

                            var waitForFinish = new ManualResetEventSlim(false);
                            waitForFinish.Reset();

                            //start looking for work
                            queue.Start <TMessage>((message, notifications) =>
                            {
                                MessageHandlingShared.HandleFakeMessages(message, runTime, processedCount, messageCount,
                                                                         waitForFinish);
                            });

                            waitForFinish.Wait(timeOut * 1000);
                        }

                        Assert.Null(processedCount.IdError);
                        Assert.Equal(messageCount, processedCount.ProcessedCount);
                        VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                           messageCount);
                        LoggerShared.CheckForErrors(queueConnection.Queue);
                    }
                }
            }
        }
        public void RunConsumer <TTransportInit>(string queueName,
                                                 string connectionString,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogProvider logProvider,
                                                 int timeOut,
                                                 int readerCount,
                                                 int queueSize,
                                                 long messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updatetime)
            where TTransportInit : ITransportInit, new()
        {
            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics,
                                                                             true))
                {
                    using (var schedulerCreator = new SchedulerContainer())
                    {
                        using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                        {
                            taskScheduler.Configuration.MaximumThreads = workerCount;
                            taskScheduler.Configuration.MaxQueueSize   = queueSize;

                            taskScheduler.Start();
                            var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                            using (
                                var queue =
                                    creator
                                    .CreateConsumerMethodQueueScheduler(
                                        queueName, connectionString, taskFactory))
                            {
                                SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                                      heartBeatMonitorTime, updatetime, null);
                                queue.Start();
                                for (var i = 0; i < timeOut; i++)
                                {
                                    if (VerifyMetrics.GetPoisonMessageCount(metrics.GetCurrentMetrics()) == messageCount)
                                    {
                                        break;
                                    }
                                    Thread.Sleep(1000);
                                }

                                //wait for last error to be saved if needed.
                                Thread.Sleep(3000);
                            }
                        }
                        VerifyMetrics.VerifyPoisonMessageCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                    }
                }
            }
        }
Esempio n. 23
0
        public void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogger logProvider,
                                                 int timeOut,
                                                 int readerCount,
                                                 int queueSize,
                                                 int runTime,
                                                 int messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 string route, bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            var processedCount          = new IncrementWrapper();
            var haveIProcessedYouBefore = new ConcurrentDictionary <string, int>();

            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var trace = SharedSetup.CreateTrace("consumer-rollback"))
            {
                using (var metrics = new Metrics.Metrics(queueConnection.Queue))
                {
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics, false, enableChaos, scope, trace.Source)
                        )
                    {
                        using (var schedulerCreator = new SchedulerContainer((x) => x.RegisterNonScopedSingleton(trace.Source)))
                        {
                            using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                            {
                                taskScheduler.Configuration.MaximumThreads = workerCount;

                                taskScheduler.Start();
                                var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                                using (
                                    var queue =
                                        creator
                                        .CreateConsumerQueueScheduler(
                                            queueConnection, taskFactory))
                                {
                                    SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount,
                                                                          heartBeatTime,
                                                                          heartBeatMonitorTime, updateTime, route);

                                    var waitForFinish = new ManualResetEventSlim(false);
                                    waitForFinish.Reset();

                                    //start looking for work
                                    queue.Start <TMessage>((message, notifications) =>
                                    {
                                        MessageHandlingShared.HandleFakeMessagesRollback(message, runTime,
                                                                                         processedCount,
                                                                                         messageCount, waitForFinish, haveIProcessedYouBefore);
                                    });

                                    waitForFinish.Wait(timeOut * 1000);
                                }
                            }

                            Assert.Equal(messageCount, processedCount.ProcessedCount);
                            VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                               messageCount);
                            VerifyMetrics.VerifyRollBackCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                              messageCount, 1, 0);
                        }
                    }

                    haveIProcessedYouBefore.Clear();
                }
            }
        }
Esempio n. 24
0
        public void RunConsumer <TTransportInit>(QueueConnection queueConnection,
                                                 bool addInterceptors,
                                                 int workerCount,
                                                 ILogger logProvider,
                                                 int timeOut,
                                                 int readerCount,
                                                 int queueSize,
                                                 int runTime,
                                                 int messageCount,
                                                 TimeSpan heartBeatTime,
                                                 TimeSpan heartBeatMonitorTime,
                                                 Guid id,
                                                 string updateTime,
                                                 bool enableChaos, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var trace = SharedSetup.CreateTrace("consumer-rollback"))
            {
                using (var metrics = new Metrics.Metrics(queueConnection.Queue))
                {
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics, false, enableChaos, scope, trace.Source)
                        )
                    {
                        using (var schedulerCreator = new SchedulerContainer((x) => x.RegisterNonScopedSingleton(trace.Source)))
                        {
                            using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                            {
                                taskScheduler.Configuration.MaximumThreads = workerCount;

                                taskScheduler.Start();
                                var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                                using (
                                    var queue =
                                        creator
                                        .CreateConsumerMethodQueueScheduler(
                                            queueConnection, taskFactory))
                                {
                                    SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount,
                                                                          heartBeatTime,
                                                                          heartBeatMonitorTime, updateTime, null);
                                    queue.Start();
                                    var counter = 0;
                                    while (counter < timeOut)
                                    {
                                        if (MethodIncrementWrapper.Count(id) >= messageCount)
                                        {
                                            break;
                                        }

                                        Thread.Sleep(1000);
                                        counter++;
                                    }

                                    //wait for queues to commit records
                                    Thread.Sleep(3000);
                                }
                            }

                            Assert.Equal(messageCount, MethodIncrementWrapper.Count(id));
                            VerifyMetrics.VerifyProcessedCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                               messageCount);
                            VerifyMetrics.VerifyRollBackCount(queueConnection.Queue, metrics.GetCurrentMetrics(),
                                                              messageCount, 1, 0);
                        }
                    }
                }
            }
        }
        void RunConsumer <TTransportInit>(string queueName,
                                          string connectionString,
                                          bool addInterceptors,
                                          ILogProvider logProvider,
                                          int runTime,
                                          int messageCount,
                                          int timeOut,
                                          int readerCount,
                                          TimeSpan heartBeatTime,
                                          TimeSpan heartBeatMonitorTime,
                                          string updateTime,
                                          List <string> routes = null)
            where TTransportInit : ITransportInit, new()
        {
            var metricName = queueName;

            if (routes != null)
            {
                metricName = routes.Aggregate(metricName, (current, route) => current + route + "|-|");
            }

            using (var metrics = new Metrics.Metrics(metricName))
            {
                var processedCount         = new IncrementWrapper();
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics)
                    )
                {
                    using (
                        var queue =
                            creator
                            .CreateConsumerQueueScheduler(
                                queueName, connectionString, Factory))
                    {
                        SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                              heartBeatMonitorTime, updateTime, null);

                        if (routes != null)
                        {
                            queue.Configuration.Routes.AddRange(routes);
                        }

                        var waitForFinish = new ManualResetEventSlim(false);
                        waitForFinish.Reset();

                        //start looking for work
                        queue.Start <TMessage>((message, notifications) =>
                        {
                            if (routes != null && routes.Count > 0)
                            {
                                MessageHandlingShared.HandleFakeMessages(message, runTime, processedCount, messageCount * routes.Count,
                                                                         waitForFinish);
                            }
                            else
                            {
                                MessageHandlingShared.HandleFakeMessages(message, runTime, processedCount, messageCount,
                                                                         waitForFinish);
                            }
                        });

                        waitForFinish.Wait(timeOut * 1000);
                    }

                    Assert.Null(processedCount.IdError);
                    if (routes != null && routes.Count > 0)
                    {
                        Assert.Equal(messageCount * routes.Count, processedCount.ProcessedCount);
                        VerifyMetrics.VerifyProcessedCount(queueName, metrics.GetCurrentMetrics(), messageCount * routes.Count);
                    }
                    else
                    {
                        Assert.Equal(messageCount, processedCount.ProcessedCount);
                        VerifyMetrics.VerifyProcessedCount(queueName, metrics.GetCurrentMetrics(), messageCount);
                    }
                    LoggerShared.CheckForErrors(queueName);
                }
            }
        }
Esempio n. 26
0
        public void RunConsumer <TTransportInit>(string queueName, string connectionString, bool addInterceptors,
                                                 ILogProvider logProvider,
                                                 int messageCount, int workerCount, int timeOut,
                                                 int queueSize, int readerCount,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 string route,
                                                 bool enableChaos)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                var processedCount = new IncrementWrapper();
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos)
                    )
                {
                    using (var schedulerCreator =
                               new SchedulerContainer(
                                   // ReSharper disable once AccessToDisposedClosure
                                   serviceRegister => serviceRegister.Register(() => metrics, LifeStyles.Singleton), options => SharedSetup.SetOptions(options, enableChaos)))
                    {
                        bool rollBacks;
                        using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                        {
                            taskScheduler.Configuration.MaximumThreads = workerCount;
                            taskScheduler.Configuration.MaxQueueSize   = queueSize;

                            taskScheduler.Start();
                            var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                            using (
                                var queue =
                                    creator
                                    .CreateConsumerQueueScheduler(
                                        queueName, connectionString, taskFactory))
                            {
                                rollBacks = queue.Configuration.TransportConfiguration.MessageRollbackSupported;
                                SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                                      heartBeatMonitorTime, updateTime, route);
                                SharedSetup.SetupDefaultErrorRetry(queue.Configuration);

                                var waitForFinish = new ManualResetEventSlim(false);
                                waitForFinish.Reset();

                                //start looking for work
                                queue.Start <TMessage>((message, notifications) =>
                                {
                                    MessageHandlingShared.HandleFakeMessagesError(processedCount, waitForFinish,
                                                                                  messageCount, message);
                                });

                                waitForFinish.Wait(timeOut * 1000);

                                //wait for last error to be saved if needed.
                                Thread.Sleep(3000);
                            }
                        }

                        if (rollBacks)
                        {
                            VerifyMetrics.VerifyRollBackCount(queueName, metrics.GetCurrentMetrics(), messageCount, 2, 2);
                        }
                    }
                }
            }
        }