1 // ***********************************************************************
2 // Copyright (c) 2012-2014 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
31 using System.Collections.Generic;
32 using System.Threading;
34 namespace NUnit.Framework.Internal.Execution
37 /// The dispatcher needs to do different things at different,
38 /// non-overlapped times. For example, non-parallel tests may
39 /// not be run at the same time as parallel tests. We model
40 /// this using the metaphor of a working shift. The WorkShift
41 /// class associates one or more WorkItemQueues with one or
44 /// Work in the queues is processed until all queues are empty
45 /// and all workers are idle. Both tests are needed because a
46 /// worker that is busy may end up adding more work to one of
47 /// the queues. At that point, the shift is over and another
48 /// shift may begin. This cycle continues until all the tests
51 public class WorkShift
53 private static Logger log = InternalTrace.GetLogger("WorkShift");
55 private object _syncRoot = new object();
56 private int _busyCount = 0;
58 // Shift name - used for logging
62 /// Construct a WorkShift
64 public WorkShift(string name)
68 this.IsActive = false;
69 this.Queues = new List<WorkItemQueue>();
70 this.Workers = new List<TestWorker>();
73 #region Public Events and Properties
76 /// Event that fires when the shift has ended
78 public event EventHandler EndOfShift;
81 /// Gets a flag indicating whether the shift is currently active
83 public bool IsActive { get; private set; }
86 /// Gets a list of the queues associated with this shift.
88 /// <remarks>Used for testing</remarks>
89 public IList<WorkItemQueue> Queues { get; private set; }
92 /// Gets the list of workers associated with this shift.
94 public IList<TestWorker> Workers { get; private set; }
97 /// Gets a bool indicating whether this shift has any work to do
103 foreach (var q in Queues)
113 #region Public Methods
116 /// Add a WorkItemQueue to the shift, starting it if the
117 /// shift is currently active.
119 public void AddQueue(WorkItemQueue queue)
121 log.Debug("{0} shift adding queue {1}", _name, queue.Name);
130 /// Assign a worker to the shift.
132 /// <param name="worker"></param>
133 public void Assign(TestWorker worker)
135 log.Debug("{0} shift assigned worker {1}", _name, worker.Name);
139 worker.Busy += (s, ea) => Interlocked.Increment(ref _busyCount);
140 worker.Idle += (s, ea) =>
142 // Quick check first using Interlocked.Decrement
143 if (Interlocked.Decrement(ref _busyCount) == 0)
146 // Check busy count again under the lock
147 if (_busyCount == 0 && !HasWork)
156 /// Start or restart processing for the shift
160 log.Info("{0} shift starting", _name);
162 this.IsActive = true;
164 foreach (var q in Queues)
169 /// End the shift, pausing all queues and raising
170 /// the EndOfShift event.
172 public void EndShift()
174 log.Info("{0} shift ending", _name);
176 this.IsActive = false;
179 foreach (var q in Queues)
182 // Signal the dispatcher that shift ended
183 if (EndOfShift != null)
184 EndOfShift(this, EventArgs.Empty);
188 /// Shut down the shift.
190 public void ShutDown()
192 this.IsActive = false;
194 foreach (var q in Queues)
199 /// Cancel (abort or stop) the shift without completing all work
201 /// <param name="force">true if the WorkShift should be aborted, false if it should allow its currently running tests to complete</param>
202 public void Cancel(bool force)
205 this.IsActive = false;
207 foreach (var w in Workers)