PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/DomainAgent.cs

#
C# | 151 lines | 91 code | 21 blank | 39 comment | 2 complexity | 860f68c88369fdf601c6e02507e150c7 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.Reflection;
  9using System.Diagnostics;
 10
 11namespace NUnit.Core
 12{
 13	/// <summary>
 14	/// Represesents an agent that controls running of tests in
 15    /// an application domain.
 16	/// </summary>
 17	public class DomainAgent : TestAgent
 18	{
 19        static Logger log = InternalTrace.GetLogger(typeof(DomainAgent));
 20
 21        /// <summary>
 22        /// Factory method used to create a DomainAgent in an AppDomain.
 23        /// </summary>
 24        /// <param name="targetDomain">The domain in which to create the agent</param>
 25        /// <param name="traceLevel">The level of internal tracing to use</param>
 26        /// <returns>A proxy for the DomainAgent in the other domain</returns>
 27        static public DomainAgent CreateInstance(AppDomain targetDomain)
 28        {
 29#if NET_2_0
 30            System.Runtime.Remoting.ObjectHandle oh = Activator.CreateInstance(
 31                targetDomain,
 32#else
 33			System.Runtime.Remoting.ObjectHandle oh = targetDomain.CreateInstance(
 34#endif
 35 Assembly.GetExecutingAssembly().FullName,
 36                typeof(DomainAgent).FullName,
 37                false, BindingFlags.Default, null, null, null, null, null);
 38
 39            object obj = oh.Unwrap();
 40            return (DomainAgent)obj;
 41        }
 42
 43        private bool isActive;
 44
 45        /// <summary>
 46        /// Constructs a DomainAgent specifying the trace level.
 47        /// </summary>
 48        /// <param name="traceLevel">The level of internal tracing to use</param>
 49		public DomainAgent() : base( Guid.NewGuid() ) { }
 50
 51		#region Public Methods
 52        /// <summary>
 53        /// Creates a TestRunner for use in loading and running
 54        /// tests in this domain. DomainAgent always creates
 55        /// a RemoteTestRunner.
 56        /// </summary>
 57        /// <param name="runnerID">Runner ID to be used</param>
 58        /// <returns>A TestRunner</returns>
 59		public override TestRunner CreateRunner(int runnerID)
 60		{
 61            log.Info("Creating RemoteTestRunner");
 62			return new RemoteTestRunner(runnerID);
 63		}
 64
 65        /// <summary>
 66        /// Starts the agent if it is no aready started.
 67        /// </summary>
 68        /// <returns></returns>
 69        public override bool Start()
 70        {
 71            if (!this.isActive)
 72            {
 73                log.Info("Starting");
 74                this.isActive = true;
 75            }
 76
 77            return true;
 78        }
 79
 80        /// <summary>
 81        /// Stops the agent if it is running
 82        /// </summary>
 83        public override void Stop()
 84        {
 85            if (this.isActive)
 86            {
 87                log.Info("Stopping");
 88                this.isActive = false;
 89            }
 90        }
 91
 92        public AppDomain AppDomain
 93        {
 94            get { return AppDomain.CurrentDomain; }
 95        }
 96		#endregion
 97    }
 98
 99    public class DomainInitializer : MarshalByRefObject
100    {
101        static Logger log;
102
103        /// <summary>
104        /// Factory method used to create a DomainInitializer in an AppDomain.
105        /// </summary>
106        /// <param name="targetDomain">The domain in which to create the agent</param>
107        /// <param name="traceLevel">The level of internal tracing to use</param>
108        /// <returns>A proxy for the DomainAgent in the other domain</returns>
109        static public DomainInitializer CreateInstance(AppDomain targetDomain)
110        {
111#if NET_2_0
112            System.Runtime.Remoting.ObjectHandle oh = Activator.CreateInstanceFrom(
113                targetDomain,
114#else
115			System.Runtime.Remoting.ObjectHandle oh = targetDomain.CreateInstanceFrom(
116#endif
117                typeof(DomainInitializer).Assembly.CodeBase,
118                typeof(DomainInitializer).FullName,
119                false, BindingFlags.Default, null, null, null, null, null);
120
121            object obj = oh.Unwrap();
122            return (DomainInitializer)obj;
123        }
124
125        public void InitializeDomain(int level)
126        {
127            InternalTraceLevel traceLevel = (InternalTraceLevel)level;
128
129            InternalTrace.Initialize("%a_%p.log", traceLevel);
130            log = InternalTrace.GetLogger(typeof(DomainInitializer));
131
132            AppDomain domain = AppDomain.CurrentDomain;
133            
134            log.Info("Initializing domain {0}", domain.FriendlyName);
135            log.Debug("  Base Directory: {0}", domain.BaseDirectory);
136            log.Debug("  Probing Path: {0}", domain.SetupInformation.PrivateBinPath);
137
138            domain.DomainUnload += new EventHandler(OnDomainUnload);
139
140            AssemblyResolver resolver = new AssemblyResolver();
141            resolver.AddDirectory(NUnitConfiguration.NUnitLibDirectory);
142            resolver.AddDirectory(NUnitConfiguration.AddinDirectory);
143        }
144
145        void OnDomainUnload(object sender, EventArgs e)
146        {
147            log.Info("Unloading domain " + AppDomain.CurrentDomain.FriendlyName);
148            InternalTrace.Flush();
149        }
150    }
151}