public int Run()
        {
            var threads     = new Thread[_noOfThreads];
            var threadDatas = new ThreadRunner[_noOfThreads];

            for (var i = 0; i < _noOfThreads; i++)
            {
                var threadRunner = new ThreadRunner(DefaultTestingThreadReportingPeriod, _loggerFactory.CreateLogger <ThreadRunner>());
                threads[i]     = new Thread(threadRunner.RunLoop);
                threadDatas[i] = threadRunner;
                threads[i].Start();
            }

            var monitoringThread     = new Thread(MonitoringThread);
            var monitoringThreadData = new MonitoringThreadData(threadDatas);

            monitoringThread.Start(monitoringThreadData);

            Thread.Sleep(_timeToRun);
            _logger.LogInformation("Exiting worker threads");
            foreach (var threadData in threadDatas)
            {
                threadData.ShouldStop = true;
            }

            Thread.Sleep(TimeSpan.FromSeconds(60));
            foreach (var thread in threads)
            {
                thread.Join(TimeSpan.FromSeconds(1));
            }

            foreach (var thread in threads)
            {
                if (thread.IsAlive)
                {
                    thread.Interrupt();
                }
            }

            _logger.LogInformation("Exiting monitoring thread");
            monitoringThreadData.WaitHandle.Set();
            monitoringThread.Join();

            var failRun = ReportRunResult(threadDatas);

            return(failRun ? 1 : 0);
        }
        public int Run()
        {
            var threads     = new Thread[_noOfThreads];
            var threadDatas = new ThreadRunner[_noOfThreads];

            for (var i = 0; i < _noOfThreads; i++)
            {
                var threadRunner = new ThreadRunner(DefaultTestingThreadReportingPeriod, _loggerFactory.CreateLogger <ThreadRunner>());
                threads[i]     = new Thread(threadRunner.RunLoop);
                threadDatas[i] = threadRunner;
                threads[i].Start();
            }

            var monitoringThread     = new Thread(MonitoringThread);
            var monitoringThreadData = new MonitoringThreadData(threadDatas);

            monitoringThread.Start(monitoringThreadData);

            Thread.Sleep(_timeToRun);
            _logger.LogInformation("Exiting worker threads");
            foreach (var threadData in threadDatas)
            {
                threadData.ShouldStop = true;
            }

            _logger.LogInformation("Exiting monitoring thread");
            monitoringThreadData.WaitHandle.Set();
            monitoringThread.Join();

            Thread.Sleep(DefaultTestingThreadReportingPeriod.Multiply(2));

            foreach (var thread in threads)
            {
                if (thread.IsAlive)
                {
                    _logger.LogWarning($"Worker thread {thread.ManagedThreadId} still alive");
                }
            }

            var aliveThreads = threads.Any(x => x.IsAlive);
            var failRun      = ReportRunResult(threadDatas);

            return(failRun || aliveThreads ? 1 : 0);
        }
Exemple #3
0
        public int Run()
        {
            var threads     = new Thread[_noOfThreads];
            var threadDatas = new ThreadRunner[_noOfThreads];

            for (var i = 0; i < _noOfThreads; i++)
            {
                var threadRunner = new ThreadRunner(DefaultTestingThreadReportingPeriod, _loggerFactory.CreateLogger <ThreadRunner>());
                threads[i]     = new Thread(threadRunner.RunLoop);
                threadDatas[i] = threadRunner;
                threads[i].Start();
            }

            var monitoringThread     = new Thread(MonitoringThread);
            var monitoringThreadData = new MonitoringThreadData(threadDatas);

            monitoringThread.Start(monitoringThreadData);

            Thread.Sleep(_timeToRun);
            _logger.LogInformation("Exiting worker threads");
            foreach (var threadData in threadDatas)
            {
                threadData.ShouldStop = true;
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            _logger.LogInformation("Exiting monitoring thread");
            monitoringThreadData.WaitHandle.Set();
            monitoringThread.Join();

            var noOfRuns = threadDatas.Sum(x => x.Count);

            _logger.LogInformation(string.Format("Number of test runs = {0} on {1} threads, no of exceptions: {2}", noOfRuns,
                                                 _noOfThreads, Exceptions.Count));
            return(Exceptions.Count);
        }
Exemple #4
0
        private void DumpStats(MonitoringThreadData monitoringThreadData)
        {
            var logMessage = new StringBuilder();

            logMessage.Append("Monitoring thread [threadId:noOfRuns:lastUpdateSecondsAgo:possibleHanging]:");
            foreach (var threadRunner in monitoringThreadData.ThreadRunners)
            {
                int      threadId;
                int      count;
                DateTime lastDate;
                lock (threadRunner)
                {
                    threadId = threadRunner.ThreadId;
                    count    = threadRunner.Count;
                    lastDate = threadRunner.LastPingDate;
                }
                var lastUpdateSecondsAgo = (int)(DateTime.Now - lastDate).TotalSeconds;
                var possibleHanging      = (lastUpdateSecondsAgo - 2 * DefaultTestingThreadReportingPeriod.TotalSeconds) > 0;
                logMessage.AppendFormat("[{0}-{1}-{2}-{3}]", threadId, count, lastUpdateSecondsAgo, possibleHanging ? "!!!!!!" : "_");
            }
            _logger.LogInformation(logMessage.ToString());
        }