PageRenderTime 43ms CodeModel.GetById 26ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/TestRunnerThread.cs

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