PageRenderTime 36ms CodeModel.GetById 20ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/src/NUnit/util/RemoteTestAgent.cs

#
C# | 127 lines | 86 code | 22 blank | 19 comment | 0 complexity | 31c91afa0b941a633bb00cbbc2e30a1a MD5 | raw file
  1// ****************************************************************
  2// Copyright 2008, 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
  7using System;
  8using System.Threading;
  9using System.Collections;
 10using System.IO;
 11using System.Reflection;
 12using System.Runtime.Remoting;
 13using System.Runtime.Remoting.Channels;
 14using System.Runtime.Remoting.Channels.Tcp;
 15using NUnit.Core;
 16
 17namespace NUnit.Util
 18{
 19	/// <summary>
 20	/// RemoteTestAgent represents a remote agent executing in another process
 21	/// and communicating with NUnit by TCP. Although it is similar to a
 22	/// TestServer, it does not publish a Uri at which clients may connect 
 23	/// to it. Rather, it reports back to the sponsoring TestAgency upon 
 24	/// startup so that the agency may in turn provide it to clients for use.
 25	/// </summary>
 26	public class RemoteTestAgent : TestAgent
 27	{
 28		static Logger log = InternalTrace.GetLogger(typeof(RemoteTestAgent));
 29
 30		#region Fields
 31
 32        private ManualResetEvent stopSignal = new ManualResetEvent(false);
 33		
 34		#endregion
 35
 36		#region Constructor
 37		/// <summary>
 38		/// Construct a RemoteTestAgent
 39		/// </summary>
 40		public RemoteTestAgent( Guid agentId, TestAgency agency )
 41            : base(agentId, agency) { }
 42		#endregion
 43
 44		#region Properties
 45		public int ProcessId
 46		{
 47			get { return System.Diagnostics.Process.GetCurrentProcess().Id; }
 48		}
 49		#endregion
 50
 51		#region Public Methods
 52		public override TestRunner CreateRunner(int runnerID)
 53		{
 54			return new AgentRunner(runnerID);
 55		}
 56
 57        public override bool Start()
 58		{
 59			log.Info("Agent starting");
 60
 61			try
 62			{
 63				this.Agency.Register( this );
 64				log.Debug( "Registered with TestAgency" );
 65			}
 66			catch( Exception ex )
 67			{
 68				log.Error( "RemoteTestAgent: Failed to register with TestAgency", ex );
 69                return false;
 70			}
 71
 72            return true;
 73		}
 74
 75        public override void Stop()
 76		{
 77			log.Info( "Stopping" );
 78            // This causes an error in the client because the agent 
 79            // database is not thread-safe.
 80            //if ( agency != null )
 81            //    agency.ReportStatus(this.ProcessId, AgentStatus.Stopping);
 82
 83
 84            stopSignal.Set();
 85		}
 86
 87		public void WaitForStop()
 88		{
 89            stopSignal.WaitOne();
 90		}
 91		#endregion
 92
 93        #region Nested AgentRunner class
 94        class AgentRunner : ProxyTestRunner
 95        {
 96            private ITestRunnerFactory factory;
 97
 98            public AgentRunner(int runnerID)
 99                : base(runnerID) 
100            {
101                this.factory = new InProcessTestRunnerFactory();
102            }
103
104            public override bool Load(TestPackage package)
105            {
106                this.TestRunner = factory.MakeTestRunner(package);
107                
108                return base.Load(package);
109            }
110			
111			public override IList AssemblyInfo 
112			{
113				get 
114				{
115					IList result = base.AssemblyInfo;
116					string name = Path.GetFileName(Assembly.GetEntryAssembly().Location);
117					
118					foreach( TestAssemblyInfo info in result )
119						info.ModuleName = name;
120					
121					return result;
122				}
123			}
124        }
125        #endregion
126    }
127}