PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/DomainAgent.cs

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