TestWorker
A TestWorker pulls work items from a queue
and executes them.
using System;
using System.Threading;
namespace NUnit.Framework.Internal.Execution
{
public class TestWorker
{
private static Logger log = InternalTrace.GetLogger("TestWorker");
private WorkItemQueue _readyQueue;
private Thread _workerThread;
private int _workItemCount;
private bool _running;
public string Name => _workerThread.Name;
public bool IsAlive => _workerThread.IsAlive;
public event EventHandler Busy;
public event EventHandler Idle;
public TestWorker(WorkItemQueue queue, string name, ApartmentState apartmentState)
{
_readyQueue = queue;
_workerThread = new Thread(TestWorkerThreadProc);
_workerThread.Name = name;
_workerThread.SetApartmentState(apartmentState);
}
private void TestWorkerThreadProc()
{
log.Info("{0} starting ", _workerThread.Name);
_running = true;
try {
while (_running) {
WorkItem workItem = _readyQueue.Dequeue();
if (workItem == null)
break;
log.Info("{0} executing {1}", _workerThread.Name, workItem.Test.Name);
if (this.Busy != null)
this.Busy(this, EventArgs.Empty);
workItem.Execute();
if (this.Idle != null)
this.Idle(this, EventArgs.Empty);
_workItemCount++;
}
} finally {
log.Info("{0} stopping - {1} WorkItems processed.", _workerThread.Name, _workItemCount);
}
}
public void Start()
{
_workerThread.Start();
}
public void Cancel()
{
_running = false;
if (_workerThread != null && _workerThread.IsAlive)
ThreadUtility.Kill(_workerThread);
}
}
}