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 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(); }