public void AllItemsAreProcessed()
        {
            // Arrange
            var workerThreads = 10;
            var worker = new BackgroundWorkerQueue(workerThreads);

            var work = new List<StandardWork>();

            for (var i = 0; i < 10; i++)
            {
                work.Add(new StandardWork());
            }

            // Act
            foreach (var w in work)
            {
                worker.Enqueue(w);
            }

            // Assert
            while (true)
            {
                if (PROCESSED_WORK.SetEquals(work))
                {
                    Assert.Pass("All work items were processed.");
                }
            }
        }
        public void StartProcess()
        {
            //Retrieve existing datas from client
            var clientDatas = RetrieveDealershipDatas();

            //Retrieve datas from files
            var files = ServiceFactory.File.RetrieveLinesFromFile();

            var worker = new BackgroundWorkerQueue(10);
            for (var i = 0; i < files.Length; i++)
            {
                var model = new ProcessDataRequestModel(Clients)
                {
                    Size = files.Length,
                    Position = i,
                    Line = files[i]
                };
                var work = new ProcessDataWork(model, clientDatas);
                worker.Enqueue(work);
            }

            //Generate report Top performing
            var topPerformingModel = new ProcessReportRequestModel(Clients, FrontMessage.ReportIdTopPerforming,
                FrontMessage.ReportTitleTopPerforming, worker)
            {
                WherePredicate = x => x.TotalSales > 0,
                OrderByPredicate = x => x.TotalSales,
                Size = 100
            };
            worker.Enqueue(new ProcessReportWork(topPerformingModel, clientDatas));

            //Generate report Low stock
            var lowStockModel = new ProcessReportRequestModel(Clients, FrontMessage.ReportIdLowStock,
                FrontMessage.ReportTitleLowStock, worker)
            {
                WherePredicate = x => x.AvailableStock < 10,
                OrderByPredicate = x => x.AvailableStock
            };
            worker.Enqueue(new ProcessReportWork(lowStockModel, clientDatas));
        }
Esempio n. 3
0
        private void CollectDealershipData(List<string[]> dealershipList, DataHub dataHub)
        {
            dealershipDataSet = new List<DealershipData>();
            var workerThreads = 99;
            using (var worker = new BackgroundWorkerQueue(workerThreads))
            {
                foreach (var dealership in dealershipList)
                {
                    var work = new DataCollector(dealership[0], new Uri(dealership[1]));
                    worker.Enqueue(work);
                }

                dataHub.SendTotal(dealershipList.Count);
                var completed = 0;
                var status = worker.Status();
                do
                {
                    if (status.Failed.Any())
                        worker.ReAddFailed(status.Failed);
                    Thread.Sleep(1000);
                    status = worker.Status();

                    if (Monitor.TryEnter(theLock, 100))
                    {
                        try
                        {
                            completed = dealershipDataSet.Count;
                        }
                        finally
                        {
                            Monitor.Exit(theLock);
                        }
                    }

                    dataHub.SendProgress(completed, status.Processing.Count(), status.Failed.Count());
                } while (status.Backlog.Any() || status.Processing.Any() || status.Failed.Any());
                worker.Stop();
                worker.ClearErrors();
                dataHub.CompleteDataCollection();
            }
        }
        public void QueueIsNotBrokenByExceptions()
        {
            // Arrange
            var workerThreads = 2;
            var worker = new BackgroundWorkerQueue(workerThreads);

            var exceptionThrowingWork = new ExceptionThrowingWork();
            var work = new StandardWork();

            // Act
            worker.Enqueue(exceptionThrowingWork);
            worker.Enqueue(work);

            // wait for a bit
            Thread.Sleep(1000);

            // Assert
            CollectionAssert.AreEquivalent(new List<StandardWork>() { work }, PROCESSED_WORK);
        }
        public void NoItemsAreProcessedAfterStop()
        {
            // Arrange
            var workerThreads = 10;
            var worker = new BackgroundWorkerQueue(workerThreads);

            var work = new List<StandardWork>();

            for (var i = 0; i < 10; i++)
            {
                work.Add(new StandardWork());
            }

            // Act
            worker.Stop();

            foreach (var w in work)
            {
                worker.Enqueue(w);
            }

            // wait for a bit
            Thread.Sleep(500);

            // Assert
            Assert.IsEmpty(PROCESSED_WORK);
        }
        public void WhenWorkFailed()
        {
            // ARRANGE
            var workerThreads = 1;
            var worker = new BackgroundWorkerQueue(workerThreads);

            var onFailedTestWork = new OnFailedTestWork(worker);

            // ACT
            worker.Enqueue(onFailedTestWork);

            // ASSERT
            while (false == onFailedTestWork.Called)
            {
                // wait for called
            }

            Assert.Pass("The OnWorkFailed event fired");
        }
        public void ReaddSpecificErrors()
        {
            // ARRANGE
            var workerThreads = 1;

            var worker = new BackgroundWorkerQueue(workerThreads);

            var exceptionThrowingWork = new ExceptionThrowingWork();

            var longRunningWorkProcessing = new LongRunningWork();
            var longRunningWorkInBacklog = new LongRunningWork();

            worker.Enqueue(exceptionThrowingWork);
            worker.Enqueue(longRunningWorkProcessing);
            worker.Enqueue(longRunningWorkInBacklog);

            // wait for a bit
            Thread.Sleep(500);

            var status = worker.Status();

            // PRECONDITIONS ASSERT
            CollectionAssert.IsNotEmpty(status.Failed);

            // ACT
            worker.ReAddFailed(new List<IWork>() { exceptionThrowingWork });

            // ASSERT
            var actual = worker.Status();

            CollectionAssert.IsEmpty(actual.Failed);
            CollectionAssert.Contains(actual.Processing, longRunningWorkProcessing);
            CollectionAssert.Contains(actual.Backlog, longRunningWorkInBacklog);
            CollectionAssert.Contains(actual.Backlog, exceptionThrowingWork);
        }
        public void ClearErrors()
        {
            // ARRANGE
            var workerThreads = 1;

            var worker = new BackgroundWorkerQueue(workerThreads);

            var exceptionThrowingWork = new ExceptionThrowingWork();

            worker.Enqueue(exceptionThrowingWork);

            // wait for a bit
            Thread.Sleep(500);

            var status = worker.Status();

            // PRECONDITIONS ASSERT
            CollectionAssert.IsNotEmpty(status.Failed);

            // ACT
            worker.ClearErrors();

            // ASSERT
            var actual = worker.Status().Failed;

            CollectionAssert.IsEmpty(actual);
        }
        public void Status()
        {
            // Arrange
            var workerThreads = 2;

            var worker = new BackgroundWorkerQueue(workerThreads);

            var exceptionThrowingWork1 = new ExceptionThrowingWork();
            var exceptionThrowingWork2 = new ExceptionThrowingWork();
            var longRunningWork1 = new LongRunningWork();
            var longRunningWork2 = new LongRunningWork();
            var work1 = new StandardWork();
            var work2 = new StandardWork();

            // Act
            worker.Enqueue(exceptionThrowingWork1);
            worker.Enqueue(exceptionThrowingWork2);
            worker.Enqueue(longRunningWork1);
            worker.Enqueue(longRunningWork2);
            worker.Enqueue(work1);
            worker.Enqueue(work2);

            // wait for a bit
            Thread.Sleep(1000);

            var actual = worker.Status();

            // Assert

            //backlog
            CollectionAssert.AreEquivalent(new List<IWork>() { work1, work2 }, actual.Backlog);

            //failed
            CollectionAssert.AreEquivalent(
                new List<IWork>() { exceptionThrowingWork1, exceptionThrowingWork2 }, actual.Failed);

            //processing
            CollectionAssert.AreEquivalent(new List<IWork>() { longRunningWork1, longRunningWork2 }, actual.Processing);
        }