PageRenderTime 32ms CodeModel.GetById 8ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/RemoteTestRunner.cs

#
C# | 148 lines | 99 code | 26 blank | 23 comment | 3 complexity | f80ddc3d9ae739d2695d86441f5fb91e MD5 | raw file
  1// ****************************************************************
  2// Copyright 2007, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org.
  5// ****************************************************************
  6
  7namespace NUnit.Core
  8{
  9	using System;
 10    using System.Reflection;
 11	using System.Collections;
 12	using System.Diagnostics;
 13
 14	/// <summary>
 15	/// RemoteTestRunner is tailored for use as the initial runner to
 16	/// receive control in a remote domain. It provides isolation for the return
 17	/// value by using a ThreadedTestRunner and for the events through use of
 18	/// an EventPump.
 19	/// </summary>
 20	public class RemoteTestRunner : ProxyTestRunner
 21	{
 22		/// <summary>
 23		/// Returns a RemoteTestRunner in the target domain. This method
 24		/// is used in the domain that wants to get a reference to 
 25		/// a RemoteTestRunnner and not in the test domain itself.
 26		/// </summary>
 27		/// <param name="targetDomain">AppDomain in which to create the runner</param>
 28		/// <param name="ID">Id for the new runner to use</param>
 29		/// <returns></returns>
 30        public static RemoteTestRunner CreateInstance(AppDomain targetDomain, int ID)
 31        {
 32#if NET_2_0
 33            System.Runtime.Remoting.ObjectHandle oh = Activator.CreateInstance(
 34                targetDomain,
 35#else
 36			System.Runtime.Remoting.ObjectHandle oh = targetDomain.CreateInstance(
 37#endif
 38                Assembly.GetExecutingAssembly().FullName,
 39                typeof(RemoteTestRunner).FullName,
 40                false, BindingFlags.Default, null, new object[] { ID }, null, null, null);
 41
 42            object obj = oh.Unwrap();
 43            return (RemoteTestRunner)obj;
 44        }
 45
 46		static Logger log = InternalTrace.GetLogger("RemoteTestRunner");
 47
 48		#region Constructors
 49		public RemoteTestRunner() : this( 0 ) { }
 50
 51		public RemoteTestRunner( int runnerID ) : base( runnerID ) { }
 52		#endregion
 53
 54		#region Method Overrides
 55		public override bool Load(TestPackage package)
 56		{
 57			log.Info("Loading Test Package " + package.Name );
 58
 59			// Initialize ExtensionHost if not already done
 60			if ( !CoreExtensions.Host.Initialized )
 61				CoreExtensions.Host.InitializeService();
 62
 63			// Delayed creation of downstream runner allows us to
 64			// use a different runner type based on the package
 65			bool useThreadedRunner = package.GetSetting( "UseThreadedRunner", true );
 66			
 67			TestRunner runner = new SimpleTestRunner( this.runnerID );
 68			if ( useThreadedRunner )
 69				runner = new ThreadedTestRunner( runner );
 70
 71			this.TestRunner = runner;
 72
 73			if( base.Load (package) )
 74			{
 75				log.Info("Loaded package successfully" );
 76				return true;
 77			}
 78			else
 79			{
 80				log.Info("Package load failed" );
 81				return false;
 82			}
 83		}
 84
 85        public override void Unload()
 86        {
 87            log.Info("Unloading test package");
 88            base.Unload();
 89        }
 90
 91        public override TestResult Run(EventListener listener)
 92		{
 93			return Run( listener, TestFilter.Empty );
 94		}
 95
 96		public override TestResult Run( EventListener listener, ITestFilter filter )
 97		{
 98            log.Debug("Run");
 99
100            QueuingEventListener queue = new QueuingEventListener();
101
102			StartTextCapture( queue );
103
104			using( EventPump pump = new EventPump( listener, queue.Events, true ) )
105			{
106				pump.Start();
107				return base.Run( queue, filter );
108			}
109		}
110
111		public override void BeginRun( EventListener listener )
112		{
113			BeginRun( listener, TestFilter.Empty );
114		}
115
116		public override void BeginRun( EventListener listener, ITestFilter filter )
117		{
118            log.Debug("BeginRun");
119
120			QueuingEventListener queue = new QueuingEventListener();
121
122			StartTextCapture( queue );
123
124			EventPump pump = new EventPump( listener, queue.Events, true);
125			pump.Start(); // Will run till RunFinished is received
126			// TODO: Make sure the thread is cleaned up if we abort the run
127		
128			base.BeginRun( queue, filter );
129		}
130
131		private void StartTextCapture( EventListener queue )
132		{
133            TestExecutionContext.CurrentContext.Out = new EventListenerTextWriter(queue, TestOutputType.Out);
134            TestExecutionContext.CurrentContext.Error = new EventListenerTextWriter(queue, TestOutputType.Error);
135            TestExecutionContext.CurrentContext.TraceWriter = new EventListenerTextWriter(queue, TestOutputType.Trace);
136            TestExecutionContext.CurrentContext.Tracing = true;
137            TestExecutionContext.CurrentContext.LogWriter = new EventListenerTextWriter(queue, TestOutputType.Log);
138            TestExecutionContext.CurrentContext.Logging = true;
139		}
140		#endregion
141
142		private void CurrentDomain_DomainUnload(object sender, EventArgs e)
143		{
144			log.Debug(AppDomain.CurrentDomain.FriendlyName + " unloaded");
145			InternalTrace.Flush();
146		}
147	}
148}