3 #define NUNIT_FRAMEWORK
8 using System.Collections.Generic;
11 using System.Threading.Tasks;
12 using System.Reflection;
13 using NUnit.Framework.Internal;
14 using System.Threading;
15 using NUnit.Framework.Internal.Commands;
17 namespace NUnit.Framework.TUnit
19 #region tronghieu.d - added class
20 public class TAsyncThreadMgr
22 private static TAsyncThreadMgr _instance;
23 private static object lockObject = new object();
25 public static TAsyncThreadMgr GetInstance()
29 if (_instance == null)
31 _instance = new TAsyncThreadMgr();
37 private TAsyncThreadMgr()
39 this.testCommand = null;
40 this.testMethod = null;
41 this.arguments = null;
45 public void SetData(TestCommand testCommand, TestMethod testMethod, object[] arguments, TestExecutionContext context, bool IsAsyncOperation)
47 this.testCommand = testCommand;
48 this.testMethod = testMethod;
49 this.arguments = arguments;
50 this.context = context;
51 this.IsAsyncOperation = IsAsyncOperation;
52 this.exception = null;
54 #region nguyen.vtan setData for setup & teardown to run on main thread
55 public void SetData(SetUpTearDownItem setupteardownItem)
57 this._setupteardownItem.Add(setupteardownItem);
59 public void SetTearDownData(SetUpTearDownItem setupteardownItem)
61 this._teardownItem.Add(setupteardownItem);
63 public void ClearSetUpTearDown()
65 _setupteardownItem.Clear();
66 _teardownItem.Clear();
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;
82 private readonly ManualResetEvent _methodExecutionResetEvent = new ManualResetEvent(false);
84 public ManualResetEvent GetMethodExecutionResetEvent()
86 return _methodExecutionResetEvent;
89 /* Invoke async test method in main thread*/
90 public void RunTestMethod()
92 if (testCommand == null || testMethod == null || context == null)
96 TLogger.Write("##### RunTestMethod in TAsyncThreadMgr class #####");
100 RunNonAsyncTestMethod();
103 public void RunAsyncTestMethod()
105 TLogger.Write("##### RunAsyncTestMethod in TAsyncThreadMgr class #####");
109 #region nguyen.vtan add Setup
112 result = Reflect.InvokeMethod(testMethod.Method.MethodInfo, context.TestObject, arguments);
117 // Console.WriteLine(e.Message);
123 #region nguyen.vtan add Setup
127 testCommand._testMethodRunComplete.Set();
128 _methodExecutionResetEvent.Reset();
129 _methodExecutionResetEvent.WaitOne();
134 ((Task)result).GetAwaiter().OnCompleted(() =>
136 #region nguyen.vtan add TearDown
140 testCommand._testMethodRunComplete.Set();
141 _methodExecutionResetEvent.Reset();
142 _methodExecutionResetEvent.WaitOne();
149 public void RunNonAsyncTestMethod()
151 TLogger.Write("##### RunNonAsyncTestMethod in TAsyncThreadMgr class #####");
155 result = testMethod.Method.Invoke(context.TestObject, arguments);
161 #region nguyen.vtan add TearDown
165 testCommand._testMethodRunComplete.Set();
166 _methodExecutionResetEvent.Reset();
167 _methodExecutionResetEvent.WaitOne();
171 public object GetResult()
176 public Exception GetNonAsyncMethodException()
181 #region add by nguyen.vtan rewrite setup & teardown method to run on main thread
182 public void runSetup()
184 TLogger.Write("##### runSetup in TAsyncThreadMgr class #####");
185 foreach (var item in _setupteardownItem)
187 if (item?._setUpMethods != null)
188 foreach (MethodInfo setUpMethod in item._setUpMethods)
190 item.RunSetUpOrTearDownMethod(context, setUpMethod);
194 public void runTearDown()
197 TLogger.Write("##### runTearDown in TAsyncThreadMgr class #####");
198 if (context?.ExecutionStatus == TestExecutionStatus.AbortRequested)
205 foreach (var item in _teardownItem)
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)
211 int index = item._tearDownMethods.Count;
214 item.RunSetUpOrTearDownMethod(context, item._tearDownMethods[index]);
221 context.CurrentResult.RecordTearDownException(ex);