public void SetMinimumWorkerThreadsCountShouldThrowExceptionWhenMinimumThreadsCountIsBiggerThanMaximumCount()
        {
            WorkerThreadPool workerThreadPool = new WorkerThreadPool(2, 3);

            Assert.Throws <ArgumentOutOfRangeException>(() => workerThreadPool.SetMinimumWorkerThreadsCount(4));

            workerThreadPool.Shutdown(true, 500);
        }
        public void SetMinimumWorkerThreadsCountShouldThrowExceptionWhenMinimumThreadsCountIsLessThanZero()
        {
            WorkerThreadPool workerThreadPool = new WorkerThreadPool(0, 1);

            Assert.Throws <ArgumentOutOfRangeException>(() => workerThreadPool.SetMinimumWorkerThreadsCount(-1));

            workerThreadPool.Shutdown(true, 500);
        }
        public void SetMinimumWorkerThreadsCountShouldNotAddWorkerThreadsWhenMinimumThreadsCountIncreasesAndThereIsWorkItemsInTheQueue()
        {
            ManualFinishWorkItemQueueStub manualFinishWorkItemQueueStub = new ManualFinishWorkItemQueueStub();

            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            int exitedWorkerThreadCount       = 0;
            WorkerThreadPool workerThreadPool = new WorkerThreadPool(manualFinishWorkItemQueueStub, 2, 4);

            workerThreadPool.WorkerThreadExiting += (sender, args) =>
            {
                if (Interlocked.Increment(ref exitedWorkerThreadCount) >= 1)
                {
                    manualResetEvent.Set();
                }
            };

            manualFinishWorkItemQueueStub.WaitAllStart(500);

            Assert.AreEqual(4, workerThreadPool.WorkerThreadsCount);
            Assert.AreEqual(4, workerThreadPool.WorkerThreads.Count(x => x.IsBusy));
            Assert.AreEqual(1, manualFinishWorkItemQueueStub.Count);

            workerThreadPool.SetMinimumWorkerThreadsCount(3);

            Assert.AreEqual(4, workerThreadPool.WorkerThreadsCount);
            Assert.AreEqual(4, workerThreadPool.WorkerThreads.Count(x => x.IsBusy));
            Assert.AreEqual(1, manualFinishWorkItemQueueStub.Count);

            manualFinishWorkItemQueueStub.StopAll();

            manualResetEvent.WaitOne();

            Assert.AreEqual(3, workerThreadPool.WorkerThreadsCount);
            Assert.AreEqual(0, workerThreadPool.WorkerThreads.Count(x => x.IsBusy));

            workerThreadPool.Shutdown(true, 500);
        }
        public void SetMinimumWorkerThreadsCountShouldThrowExceptionWhenMinimumThreadsCountIsBiggerThanMaximumCount()
        {
            WorkerThreadPool workerThreadPool = new WorkerThreadPool(2, 3);

            Assert.Throws<ArgumentOutOfRangeException>(() => workerThreadPool.SetMinimumWorkerThreadsCount(4));

            workerThreadPool.Shutdown(true, 500);
        }
        public void SetMinimumWorkerThreadsCountShouldThrowExceptionWhenMinimumThreadsCountIsLessThanZero()
        {
            WorkerThreadPool workerThreadPool = new WorkerThreadPool(0, 1);

            Assert.Throws<ArgumentOutOfRangeException>(() => workerThreadPool.SetMinimumWorkerThreadsCount(-1));

            workerThreadPool.Shutdown(true, 500);
        }
        public void SetMinimumWorkerThreadsCountShouldNotAddWorkerThreadsWhenMinimumThreadsCountIncreasesAndThereIsWorkItemsInTheQueue()
        {
            ManualFinishWorkItemQueueStub manualFinishWorkItemQueueStub = new ManualFinishWorkItemQueueStub();
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());
            manualFinishWorkItemQueueStub.Enqueue(new ManualFinishWorkItem());

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            int exitedWorkerThreadCount = 0;
            WorkerThreadPool workerThreadPool = new WorkerThreadPool(manualFinishWorkItemQueueStub, 2, 4);
            workerThreadPool.WorkerThreadExiting += (sender, args) =>
            {
                if (Interlocked.Increment(ref exitedWorkerThreadCount) >= 1)
                {
                    manualResetEvent.Set();
                }
            };

            manualFinishWorkItemQueueStub.WaitAllStart(500);

            Assert.AreEqual(4, workerThreadPool.WorkerThreadsCount);
            Assert.AreEqual(4, workerThreadPool.WorkerThreads.Count(x => x.IsBusy));
            Assert.AreEqual(1, manualFinishWorkItemQueueStub.Count);

            workerThreadPool.SetMinimumWorkerThreadsCount(3);

            Assert.AreEqual(4, workerThreadPool.WorkerThreadsCount);
            Assert.AreEqual(4, workerThreadPool.WorkerThreads.Count(x => x.IsBusy));
            Assert.AreEqual(1, manualFinishWorkItemQueueStub.Count);

            manualFinishWorkItemQueueStub.StopAll();

            manualResetEvent.WaitOne();

            Assert.AreEqual(3, workerThreadPool.WorkerThreadsCount);
            Assert.AreEqual(0, workerThreadPool.WorkerThreads.Count(x => x.IsBusy));

            workerThreadPool.Shutdown(true, 500);
        }