<PackageReference Include="NUnit" Version="3.7.0" />

WorkShift

public class WorkShift
The dispatcher needs to do different things at different, non-overlapped times. For example, non-parallel tests may not be run at the same time as parallel tests. We model this using the metaphor of a working shift. The WorkShift class associates one or more WorkItemQueues with one or more TestWorkers. Work in the queues is processed until all queues are empty and all workers are idle. Both tests are needed because a worker that is busy may end up adding more work to one of the queues. At that point, the shift is over and another shift may begin. This cycle continues until all the tests have been run.
using System; using System.Collections.Generic; using System.Threading; namespace NUnit.Framework.Internal.Execution { public class WorkShift { private static Logger log = InternalTrace.GetLogger("WorkShift"); private object _syncRoot = new object(); private int _busyCount; private bool _firstStart = true; public string Name { get; } public bool IsActive { get; set; } public IList<WorkItemQueue> Queues { get; } public IList<TestWorker> Workers { get; } public bool HasWork { get { foreach (WorkItemQueue queue in Queues) { if (!queue.IsEmpty) return true; } return false; } } public event EventHandler EndOfShift; public WorkShift(string name) { Name = name; IsActive = false; Queues = new List<WorkItemQueue>(); Workers = new List<TestWorker>(); } public void AddQueue(WorkItemQueue queue) { log.Debug("{0} shift adding queue {1}", Name, queue.Name); Queues.Add(queue); if (IsActive) queue.Start(); } public void Assign(TestWorker worker) { log.Debug("{0} shift assigned worker {1}", Name, worker.Name); Workers.Add(worker); } public void Start() { log.Info("{0} shift starting", Name); IsActive = true; if (_firstStart) StartWorkers(); foreach (WorkItemQueue queue in Queues) { queue.Start(); } _firstStart = false; } private void StartWorkers() { foreach (TestWorker worker in Workers) { worker.Busy += delegate { Interlocked.Increment(ref _busyCount); }; worker.Idle += delegate { if (Interlocked.Decrement(ref _busyCount) == 0) { lock (_syncRoot) { if (_busyCount == 0 && !HasWork) EndShift(); } } }; worker.Start(); } } public void EndShift() { log.Info("{0} shift ending", Name); IsActive = false; foreach (WorkItemQueue queue in Queues) { queue.Pause(); } this.EndOfShift(this, EventArgs.Empty); } public void ShutDown() { IsActive = false; foreach (WorkItemQueue queue in Queues) { queue.Stop(); } } public void Cancel(bool force) { if (force) IsActive = false; foreach (TestWorker worker in Workers) { worker.Cancel(force); } } } }