public void DoWorkShouldExitWorkerThreadWhenWorkerThreadManagerShouldExitWorkerThreadReturnsTrueAndWorkItemQueueIsEmpty()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create();

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue   workItemQueue    = Substitute.For <IWorkItemQueue>();

            workItemQueue.Dequeue().Returns((IWorkItem)null);

            bool         exitedBecaseWorkItemQueueIsEmpty = false;
            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);

            workerThreadManager.WorkerThreadExiting += (sender, args) =>
            {
                if (args.WorkerThreadExitReason == WorkerThreadExitReason.WorkItemQueueIsEmpty)
                {
                    exitedBecaseWorkItemQueueIsEmpty = true;
                    manualResetEvent.Set();
                }
            };
            workerThreadManager.ShouldExitWorkerThread(workerThread, false).Returns(false);
            workerThreadManager.ShouldExitWorkerThread(workerThread, true).Returns(true);

            workerThread.Start();

            manualResetEvent.WaitOne(500);
            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            workItemQueue.Received(1).Dequeue();
            Assert.AreEqual(true, exitedBecaseWorkItemQueueIsEmpty);
        }
        public void DoWorkShouldNotCallWorkItemDequeueWhenWorkerThreadManagerShouldExitWorkerThreadReturnsTrue()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create();

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue   workItemQueue    = Substitute.For <IWorkItemQueue>();
            WorkerThread     workerThread     = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);

            workerThreadManager.WorkerThreadExiting += (sender, args) =>
            {
                if (args.WorkerThreadExitReason == WorkerThreadExitReason.MaximumThreadCountExceeded)
                {
                    manualResetEvent.Set();
                }
            };
            workerThreadManager.ShouldExitWorkerThread(workerThread, false).ReturnsForAnyArgs(true);
            workerThread.Start();

            manualResetEvent.WaitOne(500);

            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            workItemQueue.DidNotReceive().Dequeue();
        }
        public void StartShouldStartThread()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create();

            IWorkItemQueue workItemQueue = Substitute.For <IWorkItemQueue>();

            workItemQueue.Dequeue().Returns((IWorkItem)null);

            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);

            workerThreadManager.ShouldExitWorkerThread(workerThread, false).Returns(false);
            workerThread.Start();

            Wait.While(() => !workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(true, workerThread.Thread.IsAlive);

            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);
        }
        public void DoWorkShouldCallWorkItemDequeueWhenWorkerThreadManagerShouldExitWorkerThreadReturnsFalse()
        {
            WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create();

            ManualResetEvent manualResetEvent = new ManualResetEvent(false);
            IWorkItemQueue   workItemQueue    = Substitute.For <IWorkItemQueue>();

            workItemQueue.When(x => x.Dequeue()).Do(x => { manualResetEvent.Set(); });

            WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false);

            workerThreadManager.ShouldExitWorkerThread(workerThread, true).Returns(false);
            workerThread.StopWhenWorkItemQueueIsEmpty(true);
            workerThread.Start();

            manualResetEvent.WaitOne(500);
            workerThread.Stop();

            Wait.While(() => workerThread.Thread.IsAlive, 300);

            Assert.AreEqual(false, workerThread.Thread.IsAlive);

            workItemQueue.Received(1).Dequeue();
        }