[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / Execution / WorkShift.cs
1 // ***********************************************************************
2 // Copyright (c) 2012-2014 Charlie Poole
3 //
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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 #if PARALLEL
30 using System;
31 using System.Collections.Generic;
32 using System.Threading;
33
34 namespace NUnit.Framework.Internal.Execution
35 {
36     /// <summary>
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
42     /// more TestWorkers.
43     ///
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
49     /// have been run.
50     /// </summary>
51     public class WorkShift
52     {
53         private static Logger log = InternalTrace.GetLogger("WorkShift");
54
55         private object _syncRoot = new object();
56         private int _busyCount = 0;
57
58         // Shift name - used for logging
59         private string _name;
60
61         /// <summary>
62         /// Construct a WorkShift
63         /// </summary>
64         public WorkShift(string name)
65         {
66             _name = name;
67
68             this.IsActive = false;
69             this.Queues = new List<WorkItemQueue>();
70             this.Workers = new List<TestWorker>();
71         }
72
73         #region Public Events and Properties
74
75         /// <summary>
76         /// Event that fires when the shift has ended
77         /// </summary>
78         public event EventHandler EndOfShift;
79
80         /// <summary>
81         /// Gets a flag indicating whether the shift is currently active
82         /// </summary>
83         public bool IsActive { get; private set; }
84
85         /// <summary>
86         /// Gets a list of the queues associated with this shift.
87         /// </summary>
88         /// <remarks>Used for testing</remarks>
89         public IList<WorkItemQueue> Queues { get; private set; }
90
91         /// <summary>
92         /// Gets the list of workers associated with this shift.
93         /// </summary>
94         public IList<TestWorker> Workers { get; private set; }
95
96         /// <summary>
97         /// Gets a bool indicating whether this shift has any work to do
98         /// </summary>
99         public bool HasWork
100         {
101             get
102             {
103                 foreach (var q in Queues)
104                     if (!q.IsEmpty)
105                         return true;
106
107                 return false;
108             }
109         }
110
111         #endregion
112
113         #region Public Methods
114
115         /// <summary>
116         /// Add a WorkItemQueue to the shift, starting it if the
117         /// shift is currently active.
118         /// </summary>
119         public void AddQueue(WorkItemQueue queue)
120         {
121             log.Debug("{0} shift adding queue {1}", _name, queue.Name);
122
123             Queues.Add(queue);
124
125             if (this.IsActive)
126                 queue.Start();
127         }
128
129         /// <summary>
130         /// Assign a worker to the shift.
131         /// </summary>
132         /// <param name="worker"></param>
133         public void Assign(TestWorker worker)
134         {
135             log.Debug("{0} shift assigned worker {1}", _name, worker.Name);
136
137             Workers.Add(worker);
138
139             worker.Busy += (s, ea) => Interlocked.Increment(ref _busyCount);
140             worker.Idle += (s, ea) =>
141             {
142                 // Quick check first using Interlocked.Decrement
143                 if (Interlocked.Decrement(ref _busyCount) == 0)
144                     lock (_syncRoot)
145                     {
146                         // Check busy count again under the lock
147                         if (_busyCount == 0 && !HasWork)
148                             this.EndShift();
149                     }
150             };
151
152             worker.Start();
153         }
154
155         /// <summary>
156         /// Start or restart processing for the shift
157         /// </summary>
158         public void Start()
159         {
160             log.Info("{0} shift starting", _name);
161
162             this.IsActive = true;
163
164             foreach (var q in Queues)
165                 q.Start();
166         }
167
168         /// <summary>
169         /// End the shift, pausing all queues and raising
170         /// the EndOfShift event.
171         /// </summary>
172         public void EndShift()
173         {
174             log.Info("{0} shift ending", _name);
175
176             this.IsActive = false;
177
178             // Pause all queues
179             foreach (var q in Queues)
180                 q.Pause();
181
182             // Signal the dispatcher that shift ended
183             if (EndOfShift != null)
184                 EndOfShift(this, EventArgs.Empty);
185         }
186
187         /// <summary>
188         /// Shut down the shift.
189         /// </summary>
190         public void ShutDown()
191         {
192             this.IsActive = false;
193
194             foreach (var q in Queues)
195                 q.Stop();
196         }
197
198         /// <summary>
199         /// Cancel (abort or stop) the shift without completing all work
200         /// </summary>
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)
203         {
204             if (force)
205                 this.IsActive = false;
206
207             foreach (var w in Workers)
208                 w.Cancel(force);
209         }
210
211         #endregion
212     }
213 }
214
215 #endif