dcb11b81555abab2b9e3c99c798daf8d8cdb851b
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / TUnit / TAsyncThreadMgr.cs
1 #define PORTABLE
2 #define TIZEN
3 #define NUNIT_FRAMEWORK
4 #define NUNITLITE
5 #define NET_4_5
6 #define PARALLEL
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12 using System.Reflection;
13 using NUnit.Framework.Internal;
14 using System.Threading;
15 using NUnit.Framework.Internal.Commands;
16
17 namespace NUnit.Framework.TUnit
18 {
19     #region tronghieu.d - added class
20     public class TAsyncThreadMgr
21     {
22         private static TAsyncThreadMgr _instance;
23         private static object lockObject = new object();
24
25         public static TAsyncThreadMgr GetInstance()
26         {
27             lock (lockObject)
28             {
29                 if (_instance == null)
30                 {
31                     _instance = new TAsyncThreadMgr();
32                 }
33             }
34             return _instance;
35         }
36
37         private TAsyncThreadMgr()
38         {
39             this.testCommand = null;
40             this.testMethod = null;
41             this.arguments = null;
42             this.context = null;
43         }
44
45         public void SetData(TestCommand testCommand, TestMethod testMethod, object[] arguments, TestExecutionContext context, bool IsAsyncOperation)
46         {
47             this.testCommand = testCommand;
48             this.testMethod = testMethod;
49             this.arguments = arguments;
50             this.context = context;
51             this.IsAsyncOperation = IsAsyncOperation;
52             this.exception = null;
53         }
54         #region nguyen.vtan setData for setup & teardown to run on main thread
55         public void SetData(SetUpTearDownItem setupteardownItem)
56         {
57             this._setupteardownItem.Add(setupteardownItem);
58         }
59         public void SetTearDownData(SetUpTearDownItem setupteardownItem)
60         {
61             this._teardownItem.Add(setupteardownItem);
62         }
63         public void ClearSetUpTearDown()
64         {
65             _setupteardownItem.Clear();
66             _teardownItem.Clear();
67         }
68         #endregion
69
70
71         private TestCommand testCommand;
72         private TestMethod testMethod;
73         private object[] arguments;
74         private TestExecutionContext context;
75         private bool IsAsyncOperation;
76         private object result;
77         private Exception exception;
78         readonly List<SetUpTearDownItem> _setupteardownItem = new List<SetUpTearDownItem>();
79         readonly List<SetUpTearDownItem> _teardownItem = new List<SetUpTearDownItem>();
80         //ManualResetEvent _singnalOneTimeTearDown;
81
82         private readonly ManualResetEvent _methodExecutionResetEvent = new ManualResetEvent(false);
83
84         public ManualResetEvent GetMethodExecutionResetEvent()
85         {
86             return _methodExecutionResetEvent;
87         }
88
89         /* Invoke async test method in main thread*/
90         public void RunTestMethod()
91         {
92             if (testCommand == null || testMethod == null || context == null)
93             {
94                 return;
95             }
96             TLogger.Write("##### RunTestMethod in TAsyncThreadMgr class #####");
97             if (IsAsyncOperation)
98                 RunAsyncTestMethod();
99             else
100                 RunNonAsyncTestMethod();
101         }
102
103         public void RunAsyncTestMethod()
104         {
105             TLogger.Write("##### RunAsyncTestMethod in TAsyncThreadMgr class #####");
106             try
107             {
108                 result = null;
109                 #region nguyen.vtan add Setup
110                 runSetup();
111                 #endregion
112                 result = Reflect.InvokeMethod(testMethod.Method.MethodInfo, context.TestObject, arguments);
113             }
114             catch (Exception e)
115             {
116                 exception = e;
117                 // Console.WriteLine(e.Message);
118             }
119             finally
120             {
121                 if (result == null)
122                 {
123                     #region nguyen.vtan add Setup
124                     runTearDown();
125                     Thread.Sleep(50);
126                     #endregion
127                     testCommand._testMethodRunComplete.Set();
128                     _methodExecutionResetEvent.Reset();
129                     _methodExecutionResetEvent.WaitOne();
130                     RunTestMethod();
131                 }
132                 else
133                 {
134                     ((Task)result).GetAwaiter().OnCompleted(() =>
135                     {
136                         #region nguyen.vtan add TearDown
137                         runTearDown();
138                         Thread.Sleep(50);
139                         #endregion
140                         testCommand._testMethodRunComplete.Set();
141                         _methodExecutionResetEvent.Reset();
142                         _methodExecutionResetEvent.WaitOne();
143                         RunTestMethod();
144                     });
145                 }
146             }
147         }
148
149         public void RunNonAsyncTestMethod()
150         {
151             TLogger.Write("##### RunNonAsyncTestMethod in TAsyncThreadMgr class #####");
152             try
153             {
154                 runSetup();
155                 result = testMethod.Method.Invoke(context.TestObject, arguments);
156             }
157             catch (Exception ex)
158             {
159                 exception = ex;
160             }
161             #region nguyen.vtan add TearDown
162             runTearDown();
163             Thread.Sleep(50);
164             #endregion
165             testCommand._testMethodRunComplete.Set();
166             _methodExecutionResetEvent.Reset();
167             _methodExecutionResetEvent.WaitOne();
168             RunTestMethod();
169         }
170
171         public object GetResult()
172         {
173             return result;
174         }
175
176         public Exception GetNonAsyncMethodException()
177         {
178             return exception;
179         }
180
181         #region add by nguyen.vtan rewrite setup & teardown method to run on main thread
182         public void runSetup()
183         {
184             TLogger.Write("##### runSetup in TAsyncThreadMgr class #####");
185             foreach (var item in _setupteardownItem)
186             {
187                 if (item?._setUpMethods != null)
188                     foreach (MethodInfo setUpMethod in item._setUpMethods)
189                     {
190                         item.RunSetUpOrTearDownMethod(context, setUpMethod);
191                     }
192             }
193         }
194         public void runTearDown()
195         {
196
197             TLogger.Write("##### runTearDown in TAsyncThreadMgr class #####");
198             if (context?.ExecutionStatus == TestExecutionStatus.AbortRequested)
199             {
200                 return;
201             }
202
203             try
204             {
205                 foreach (var item in _teardownItem)
206                 {
207                     // Even though we are only running one level at a time, we
208                     // run the teardowns in reverse order to provide consistency.
209                     if (item?._tearDownMethods != null)
210                     {
211                         int index = item._tearDownMethods.Count;
212                         while (--index >= 0)
213                         {
214                             item.RunSetUpOrTearDownMethod(context, item._tearDownMethods[index]);
215                         }
216                     }
217                 }
218             }
219             catch (Exception ex)
220             {
221                 context.CurrentResult.RecordTearDownException(ex);
222             }
223         }
224         #endregion
225     }
226     #endregion
227 }