PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/TestRunnerThread.cs

#
C# | 140 lines | 80 code | 24 blank | 36 comment | 6 complexity | 4954dd675343698de1d06ec30c370d5e MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. using System.Threading;
  8. using System.Configuration;
  9. using System.Collections.Specialized;
  10. namespace NUnit.Core
  11. {
  12. /// <summary>
  13. /// TestRunnerThread encapsulates running a test on a thread.
  14. /// It knows how to create the thread based on configuration
  15. /// settings and can cancel abort the test if necessary.
  16. /// </summary>
  17. public class TestRunnerThread
  18. {
  19. #region Private Fields
  20. /// <summary>
  21. /// The Test runner to be used in running tests on the thread
  22. /// </summary>
  23. private TestRunner runner;
  24. /// <summary>
  25. /// The System.Threading.Thread created by the object
  26. /// </summary>
  27. private Thread thread;
  28. /// <summary>
  29. /// The EventListener interface to receive test events
  30. /// </summary>
  31. private NUnit.Core.EventListener listener;
  32. /// <summary>
  33. /// Array of test names for ues by the thread proc
  34. /// </summary>
  35. //private string[] testNames;
  36. private ITestFilter filter;
  37. /// <summary>
  38. /// Array of returned results
  39. /// </summary>
  40. private TestResult[] results;
  41. #endregion
  42. #region Properties
  43. /// <summary>
  44. /// True if the thread is executing
  45. /// </summary>
  46. public bool IsAlive
  47. {
  48. get { return this.thread.IsAlive; }
  49. }
  50. /// <summary>
  51. /// Array of returned results
  52. /// </summary>
  53. public TestResult[] Results
  54. {
  55. get { return results; }
  56. }
  57. #endregion
  58. #region Constructor
  59. public TestRunnerThread( TestRunner runner )
  60. {
  61. this.runner = runner;
  62. this.thread = new Thread( new ThreadStart( TestRunnerThreadProc ) );
  63. thread.IsBackground = true;
  64. thread.Name = "TestRunnerThread";
  65. thread.Priority = NUnitConfiguration.ThreadPriority;
  66. if (NUnitConfiguration.ApartmentState != ApartmentState.Unknown)
  67. #if NET_2_0
  68. thread.SetApartmentState(NUnitConfiguration.ApartmentState);
  69. #else
  70. thread.ApartmentState = NUnitConfiguration.ApartmentState;
  71. #endif
  72. }
  73. #endregion
  74. #region Public Methods
  75. public void Wait()
  76. {
  77. if ( this.thread.IsAlive )
  78. this.thread.Join();
  79. }
  80. public void Cancel()
  81. {
  82. this.thread.Abort(); // Request abort first
  83. // Wake up the thread if necessary
  84. if ( ( this.thread.ThreadState & ThreadState.WaitSleepJoin ) != 0 )
  85. this.thread.Interrupt();
  86. }
  87. public void StartRun( EventListener listener )
  88. {
  89. StartRun( listener, TestFilter.Empty );
  90. }
  91. public void StartRun( EventListener listener, ITestFilter filter )
  92. {
  93. this.listener = listener;
  94. this.filter = filter;
  95. thread.Start();
  96. }
  97. #endregion
  98. #region Thread Proc
  99. /// <summary>
  100. /// The thread proc for our actual test run
  101. /// </summary>
  102. private void TestRunnerThreadProc()
  103. {
  104. try
  105. {
  106. results = new TestResult[] { runner.Run(this.listener, this.filter) };
  107. }
  108. catch (Exception ex)
  109. {
  110. if ( !(ex is ThreadAbortException) )
  111. throw new ApplicationException("Exception in TestRunnerThread", ex);
  112. }
  113. }
  114. #endregion
  115. }
  116. }