[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / Execution / TestWorker.cs
1 // ***********************************************************************
2 // Copyright (c) 2012 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 #define NETCF
30 #if PARALLEL
31 using System;
32 using System.Threading;
33
34 namespace NUnit.Framework.Internal.Execution
35 {
36     /// <summary>
37     /// A TestWorker pulls work items from a queue
38     /// and executes them.
39     /// </summary>
40     public class TestWorker
41     {
42         private static Logger log = InternalTrace.GetLogger("TestWorker");
43
44         private WorkItemQueue _readyQueue;
45         private Thread _workerThread;
46
47         private int _workItemCount = 0;
48
49         private bool _running;
50
51         /// <summary>
52         /// Event signaled immediately before executing a WorkItem
53         /// </summary>
54         public event EventHandler Busy;
55
56         /// <summary>
57         /// Event signaled immediately after executing a WorkItem
58         /// </summary>
59         public event EventHandler Idle;
60
61 #if NETCF
62         /// <summary>
63         /// Construct a new TestWorker.
64         /// </summary>
65         /// <param name="queue">The queue from which to pull work items</param>
66         /// <param name="name">The name of this worker</param>
67         public TestWorker(WorkItemQueue queue, string name)
68 #else
69         /// <summary>
70         /// Construct a new TestWorker.
71         /// </summary>
72         /// <param name="queue">The queue from which to pull work items</param>
73         /// <param name="name">The name of this worker</param>
74         /// <param name="apartmentState">The apartment state to use for running tests</param>
75         public TestWorker(WorkItemQueue queue, string name, ApartmentState apartmentState)
76 #endif
77         {
78             _readyQueue = queue;
79
80             _workerThread = new Thread(new ThreadStart(TestWorkerThreadProc));
81             _workerThread.Name = name;
82 #if !NETCF
83             _workerThread.SetApartmentState(apartmentState);
84 #endif
85         }
86
87         /// <summary>
88         /// The name of this worker - also used for the thread
89         /// </summary>
90         public string Name
91         {
92             get { return _workerThread.Name; }
93         }
94
95         /// <summary>
96         /// Indicates whether the worker thread is running
97         /// </summary>
98         public bool IsAlive
99         {
100 #if NETCF
101             get { return !_workerThread.Join(0); }
102 #else
103             get { return _workerThread.IsAlive; }
104 #endif
105         }
106
107         /// <summary>
108         /// Our ThreadProc, which pulls and runs tests in a loop
109         /// </summary>
110         private WorkItem _currentWorkItem;
111
112         private void TestWorkerThreadProc()
113         {
114             log.Info("{0} starting ", _workerThread.Name);
115
116             _running = true;
117
118             try
119             {
120                 while (_running)
121                 {
122                     _currentWorkItem = _readyQueue.Dequeue();
123                     if (_currentWorkItem == null)
124                         break;
125
126                     log.Info("{0} executing {1}", _workerThread.Name, _currentWorkItem.Test.Name);
127
128                     if (Busy != null)
129                         Busy(this, EventArgs.Empty);
130
131                     _currentWorkItem.WorkerId = Name;
132                     _currentWorkItem.Execute();
133
134                     if (Idle != null)
135                         Idle(this, EventArgs.Empty);
136
137                     ++_workItemCount;
138                 }
139             }
140             finally
141             {
142                 log.Info("{0} stopping - {1} WorkItems processed.", _workerThread.Name, _workItemCount);
143             }
144         }
145
146         /// <summary>
147         /// Start processing work items.
148         /// </summary>
149         public void Start()
150         {
151             _workerThread.Start();
152         }
153
154         private object cancelLock = new object();
155
156         /// <summary>
157         /// Stop the thread, either immediately or after finishing the current WorkItem
158         /// </summary>
159         /// <param name="force">true if the thread should be aborted, false if it should allow the currently running test to complete</param>
160         public void Cancel(bool force)
161         {
162             if (force)
163                 _running = false;
164
165             lock (cancelLock)
166                 if (_workerThread != null && _currentWorkItem != null)
167                 {
168                     _currentWorkItem.Cancel(force);
169                     if (force)
170                         _currentWorkItem = null;
171                 }
172         }
173     }
174 }
175
176 #endif